-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelpin3.py
169 lines (121 loc) · 5.21 KB
/
labelpin3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#! /usr/bin/env python
"""
Installation instructions: Place this script in your path and make it
executable with "chmod +x labelpin" (without the quotes).
For usage instructions, see below.
----
Version 1.1 of 2012/6/25
By Nathan Dunfield : http://dunfield.info
Available from : https://faculty.math.illinois.edu/~nmd/software/index.html
----
Version 1.2 of 2017/8/25
By Oleg Soloviev : oleg.soloviev@gmail.com
Added bounding box calculation for PDF files
Versions for Python 2 and Python 3
----
This code is in the public domain.
"""
help_str = r"""Usage: labelpin file
where file is an ".eps" or ".pdf" file.
A window will appear with your image. Simply click on it to generate
the label locations, which are output with the associated LaTeX code
in the terminal window. When you're done, just close the window and
copy everything into your LaTeX file.
The resulting LaTeX code is intended for use with the "pinlabel" package:
http://www.ctan.org/tex-archive/help/Catalogue/entries/pinlabel.html
Requirements:
* Python with Tkinter.
* GhostScript.
On OS X, the is first built in and the others are included with the
MacTeX-2010 distribution: http://www.tug.org/mactex/
On Linux, are all available as standard packages, with the first
called "python-tk" on Ubuntu/Debian and "tkinter" on
Fedora/Mandriva/PCLinuxOS.
On Windows, make a hardlink to your GhostScript executable with (type in command line the following line)
mklink /j gs.exe "C:\Program Files\gs\gs9.20\bin\gswin64c.exe"
and place it in some directory in the path.
To use, run in command line
python lablepinX.py file.ext
where X is your Python version and ext is either pdf or eps
"""
pinlabel_prefix_text = r"""% Click on the window that appeared to generate the label
% locations. When you're done, close the window and then
% copy everything into your LaTeX file.
\begin{figure}[htb]
\labellist
\small\hair 2pt"""
pinlabel_suffix_text = r"""\endlabellist
\centering
\includegraphics[scale=1.0]{%s}
\caption{ }
\label{fig:label}
\end{figure}"""
import os, sys, re, tempfile, string, tkinter
import subprocess
from optparse import OptionParser
def file_extension(file_name):
return os.path.splitext(file_name)[-1].lower()
def is_postscript_file(file_name):
return os.path.splitext(file_name)[-1].lower() in ['.eps','.pdf']
def convert_file_to_bitmap(in_file, out_file, pixels_per_pt=1):
# If it's PostScript-based, use GhostScript to convert to a bitmap.
if is_postscript_file(in_file):
os.system("gs -sDEVICE=ppmraw -dEPSCrop -dNOPAUSE -dBATCH -dSAFER -q " +
"-dAlignToPixels=0 -dTextAlphaBits=4 -dUseCropBox -dGraphicsAlphaBits=4 " +
"-r%d -sOutputFile=%s %s" % (72*pixels_per_pt, out_file, in_file))
else:
# Use ImageMagick and pray.
os.system("convert %s %s" % (in_file, out_file))
def get_bounding_box(in_file):
if is_postscript_file(in_file): # now we can get correct bounding box also for PDF
data = subprocess.check_output("gs -dBATCH -dSAFER -dNOPAUSE -dUseCropBox -sDEVICE=bbox %s" % (in_file),stderr=subprocess.STDOUT,universal_newlines=True)
else:
data = ''
box = re.search("%%BoundingBox:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)", data)
return [int(x) for x in box.groups()[:2]] if box else [0,0]
def label_file(file_to_label):
# Start Tk
root = tkinter.Tk()
# Load the image file
base_name = os.path.splitext(file_to_label)[0]
image_file = tempfile.mktemp() + ".ppm"
pixels_per_pt = 2 if is_postscript_file(file_to_label) else 1
convert_file_to_bitmap(file_to_label, image_file, pixels_per_pt)
tkpi = tkinter.PhotoImage(file=image_file)
bbx, bby = get_bounding_box(file_to_label)
W, H = tkpi.width(), tkpi.height()
w, h = W/pixels_per_pt, H/pixels_per_pt
# Print the inital TeX code
print( pinlabel_prefix_text )
# Start Tk
root.geometry('+%d+%d' % (100,100))
root.geometry('%dx%d' % (W, H))
image_canvas = tkinter.Canvas(root, width=W, height=H, bd=0, highlightthickness=0)
image_canvas.create_image( (0,0), anchor="nw", image=tkpi)
image_canvas.circle_count = 0
def process_click(event):
x, y = event.x, event.y
r = 6
print(" \pinlabel {$%s$} [ ] at %d %d" % (string.ascii_letters[image_canvas.circle_count % len(string.ascii_letters)],
x/pixels_per_pt + bbx, h - y/pixels_per_pt + bby))
image_canvas.create_oval(x-r, y-r, x+r, y+r,outline="black", fill="red")
image_canvas.circle_count += 1
image_canvas.bind("<Button>", process_click)
image_canvas.place(x=0, y=0)
image_canvas.pack()
root.title(base_name)
root.mainloop()
# Print the rest of the TeX code and clean up
print(( pinlabel_suffix_text % base_name ))
os.remove(image_file)
if __name__ == "__main__":
parser = OptionParser()
parser.set_usage(help_str)
options, args = parser.parse_args()
if len(args) == 0:
print("Sorry, no file specified.")
for file_to_label in args:
if not os.path.exists(file_to_label):
print("Sorry, the file you specified (%s) does not exist." % file_to_label)
else:
label_file(file_to_label)