From: W. Trevor King Date: Thu, 12 Jan 2012 18:23:50 +0000 (-0500) Subject: Convert scale_click.sh to scale_click.py and make clickloc scripts executable. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=17eb6e9d9568638fbecef437b1ee2f0b1e9c5b1d;p=blog.git Convert scale_click.sh to scale_click.py and make clickloc scripts executable. --- diff --git a/posts/clickloc.mdwn b/posts/clickloc.mdwn index 1383f38..abe6f18 100644 --- a/posts/clickloc.mdwn +++ b/posts/clickloc.mdwn @@ -2,14 +2,14 @@ The [[clickloc.tk]] micro-app just opens an image file and prints out the pixel coordinates of any mouse clicks upon it. I use it to get -rough numbers from journal figures. You can use [[scale_click.sh]] to +rough numbers from journal figures. You can use [[scale_click.py]] to convert the output to units of your choice, if you use your first four clicks to mark out the coordinate system (xmin,anything), (xmax,anything), (anything,ymin), and (anything,ymax). $ pdfimages article.pdf fig $ clickloc.tk fig-000.ppm > fig-000.pixels - $ scale_click.sh 0 10 5 20 fig-000.pixels > fig-000.data + $ scale_click.py 0 10 5 20 fig-000.pixels > fig-000.data Take a look at [[plotpick]] for grabbing points from raw datafiles (which is more accurate and easier than reverse engineering images). diff --git a/posts/clickloc/clickloc.tk b/posts/clickloc/clickloc.tk old mode 100644 new mode 100755 diff --git a/posts/clickloc/scale_click.py b/posts/clickloc/scale_click.py new file mode 100755 index 0000000..bc0074d --- /dev/null +++ b/posts/clickloc/scale_click.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# +# Copyright (C) 2010-2012 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see +# . + + +"""Convert `clickloc.tk` pixel coordinates to physical coordinates +""" + +from math import exp, log + + +__version__ = '0.2' + + +if __name__ == '__main__': + from argparse import ArgumentParser + + parser = ArgumentParser(description=__doc__, version=__version__) + parser.add_argument( + '-x', '--logx', default=False, action='store_const', const=True, + help='Use a log scale for the physical abscissa') + parser.add_argument( + '-y', '--logy', default=False, action='store_const', const=True, + help='Use a log scale for the physical ordinate') + parser.add_argument( + dest='xmin', type=float, + help='Physical coordinate for the xmin-pixel pair') + parser.add_argument( + dest='xmax', type=float, + help='Physical coordinate for the xmax-pixel pair') + parser.add_argument( + dest='ymin', type=float, + help='Physical coordinate for the ymin-pixel pair') + parser.add_argument( + dest='ymax', type=float, + help='Physical coordinate for the ymax-pixel pair') + parser.add_argument( + dest='input', + help='File containing pixel coordinates') + args = parser.parse_args() + + with open(args.input, 'r') as f: + # reference points in pixels from the first 4 clicks + xmin_p = float(f.readline().split()[0]) + xmax_p = float(f.readline().split()[0]) + ymin_p = float(f.readline().split()[1]) + ymax_p = float(f.readline().split()[1]) + loc = locals() + for key in ['xmin', 'xmax', 'ymin', 'ymax']: + loc[key] = getattr(args, key) + if args.logx: + print(('# Xp -> exp((Xp-{xmin_p})/({xmax_p}-{xmin_p})' + '*log({xmax}/{xmin}) + log({xmin}))').format(**loc)) + else: + print(('# Xp -> ({xmax}-{xmin})/({xmax_p}-{xmin_p})' + '*(Xp-{xmin_p}) + {xmin}').format(**loc)) + if args.logy: + print(('# Yp -> exp((Yp-{ymin_p})/({ymax_p}-{ymin_p})' + '*log({ymax}/{ymin}) + log({ymin}))').format(**loc)) + else: + print(('# Yp -> ({ymax}-{ymin})/({ymax_p}-{ymin_p}' + '*(Yp-{ymin_p}) + {ymin}').format(**loc)) + + for line in f: + x_p,y_p = [float(x) for x in line.split()] + if args.logx: + x = exp( + (x_p - xmin_p)/(xmax_p - xmin_p)*log(xmax/xmin) + + log(xmin)) + else: + x = (xmax - xmin)/(xmax_p - xmin_p) * (x_p - xmin_p) + xmin + if args.logy: + y = exp( + (y_p - ymin_p)/(ymax_p - ymin_p)*log(ymax/ymin) + + log(ymin)) + else: + y = (ymax - ymin)/(ymax_p - ymin_p) * (y_p - ymin_p) + ymin + print('{}\t{}'.format(x, y)) diff --git a/posts/clickloc/scale_click.sh b/posts/clickloc/scale_click.sh deleted file mode 100644 index ee51550..0000000 --- a/posts/clickloc/scale_click.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -if [ $# -ne 5 ] -then - echo "usage: scale_click.sh xmin xmax ymin ymax file" - exit 1 -fi - -# reference points in the final units from the command line -XMIN=$1 -XMAX=$2 -YMIN=$3 -YMAX=$4 -FILE=$5 - -# reference points in pixels from the first 4 clicks -XMINP=`sed -n '1p' $FILE | cut -f1` -XMAXP=`sed -n '2p' $FILE | cut -f1` -YMINP=`sed -n '3p' $FILE | cut -f2` -YMAXP=`sed -n '4p' $FILE | cut -f2` - -echo "# Xp -> ($XMAX-$XMIN)/($XMAXP-$XMINP)*(Xp-$XMINP) + $XMIN" -echo "# Yp -> ($YMAX-$YMIN)/($YMAXP-$YMINP)*(Yp-$YMINP) + $YMIN" - -awk "{if(NR > 4){print ($XMAX-$XMIN)/($XMAXP-$XMINP)*(\$1-$XMINP) + $XMIN, \ - ($YMAX-$YMIN)/($YMAXP-$YMINP)*(\$2-$YMINP) + $YMIN}}" \ - $FILE - -exit 0