-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimg4latex.py
222 lines (198 loc) · 6.62 KB
/
img4latex.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# file: img4latex.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2014-2018 R.F. Smith <[email protected]>.
# SPDX-License-Identifier: MIT
# Created: 2014-12-05T01:26:59+01:00
# Last modified: 2022-02-25T12:29:09+0100
"""Create a suitable LaTeX figure environment for image files."""
import argparse
import configparser
import logging
import os
import subprocess as sp
import sys
__version__ = "2022.02.25"
def main():
"""
Entry point for img4latex.
"""
args = setup()
for filename in args.file:
if not os.path.exists(filename):
logging.error(f'file "{filename}" does not exist.')
continue
if filename.endswith((".ps", ".PS", ".eps", ".EPS", ".pdf", ".PDF")):
bbox = getpdfbb(filename)
bbwidth = float(bbox[2]) - float(bbox[0])
bbheight = float(bbox[3]) - float(bbox[1])
hscale = 1.0
vscale = 1.0
if bbwidth > args.width:
hscale = args.width / bbwidth
if bbheight > args.height:
vscale = args.height / bbheight
logging.info(f"hscale: {hscale:.3f}, vscale: {vscale:.3f}")
scale = min([hscale, vscale])
if scale < 0.999:
fs = "[viewport={} {} {} {},clip,scale={s:.3f}]"
opts = fs.format(*bbox, s=scale)
else:
fs = "[viewport={} {} {} {},clip]"
opts = fs.format(*bbox)
elif filename.endswith((".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG")):
width, height = getpicsize(filename)
opts = None
hscale = args.width / width
vscale = args.height / height
logging.info(f"hscale: {hscale:.3f}, vscale: {vscale:.3f}")
scale = min([hscale, vscale])
if scale < 0.999:
opts = f"[scale={scale:.3f}]"
else:
logging.error(f'file "{filename}" has an unrecognized format. Skipping...')
continue
output_figure(filename, opts)
print()
def setup():
"""Process command-line arguments. Read config file."""
after = """
The width and height arguments can also be set in a configuration file
called ~/.img4latexrc. It should look like this:
[size]
width = 125
height = 280
Command-line arguments override settings in the configuration file.
Otherwise, the defaults apply.
"""
raw = argparse.RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=raw, epilog=after
)
parser.add_argument(
"-w",
"--width",
type=float,
default=160,
help="width of the text block in mm. (default=160)",
)
parser.add_argument(
"-t",
"--height",
type=float,
default=270,
help="height of the text block in mm. (default=270)",
)
parser.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("file", nargs="*")
cfg = from_config()
args = parser.parse_args(sys.argv[1:], namespace=cfg)
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%% %(levelname)s: %(message)s",
)
if cfg is None:
logging.info("configuration file not found")
else:
logging.info(f"from config: {cfg}")
logging.debug(f"command line arguments = {sys.argv}")
logging.debug(f"parsed arguments = {args}")
args.width *= 72 / 25.4 # convert to points
args.height *= 72 / 25.4 # convert to points
if not args.file:
parser.print_help()
sys.exit(0)
return args
def from_config():
"""
Read configuration data.
Returns:
An Argparse.Namespace object
"""
values = argparse.Namespace()
d = vars(values)
config = configparser.ConfigParser()
cfgname = os.environ["HOME"] + os.sep + ".img4latexrc"
if not config.read(cfgname):
return None
for name in ["width", "height"]:
if name in config["size"]:
d[name] = float(config["size"][name])
return values
def getpdfbb(fn):
"""
Get the BoundingBox of a PostScript or PDF file.
Arguments:
fn: Name of the file to get the BoundingBox from.
Returns:
A tuple of strings in the form (llx lly urx ury), where ll means
lower left and ur means upper right.
"""
gsopts = [
"gs",
"-q",
"-dFirstPage=1",
"-dLastPage=1",
"-dNOPAUSE",
"-dBATCH",
"-sDEVICE=bbox",
fn,
]
gsres = sp.run(gsopts, stdout=sp.PIPE, stderr=sp.STDOUT, text=True, check=True)
bbs = [ln for ln in gsres.stdout.splitlines() if ln.startswith("%%BoundingBox")][0]
return bbs.split(" ")[1:]
def getpicsize(fn):
"""
Get the width and height of a bitmapped file.
Arguments:
fn: Name of the file to check.
Returns:
Width, hight of the image in points.
"""
args = ["identify", "-verbose", fn]
rv = sp.run(args, stdout=sp.PIPE, stderr=sp.STDOUT, text=True, check=True)
data = {}
for ln in rv.stdout.splitlines():
k, v = ln.strip().split(":", 1)
data[k] = v.strip()
if data["Units"] != "Undefined" and "Resolution" in data:
res = float(data["Resolution"].split("x")[0])
else:
res = 72 # default for includegraphics.
logging.debug("resolution={} {}".format(res, data["Units"]))
geom = data["Geometry"].split("+")[0].split("x")
xsize, ysize = int(geom[0]), int(geom[1])
logging.debug(f"x={xsize} px, y={ysize} px")
factor = {"PixelsPerInch": 72, "PixelsPerCentimeter": 28.35, "Undefined": 72}
m = factor[data["Units"]] / res
x, y = xsize * m, ysize * m
logging.debug(f"scaled x={x} pt, y={y} pt")
return (x, y)
def output_figure(fn, options=None):
r"""
Print the LaTeX code for the figure.
Arguments:
fn: name of the file.
options: options to add to the \includegraphics command.
"""
fb = fn.rpartition(".")[0]
fbnodir = fb[fn.rfind("/") + 1 :]
print()
print(r"\begin{figure}[!htbp]")
if options:
print(r" \centerline{\includegraphics" + options + "%")
print(r" {{{}}}}}".format(fb))
else:
print(r" \centerline{{\includegraphics{{{}}}}}".format(fb))
label = fbnodir.replace(" ", "-").replace("_", "-")
print(r" \caption{{\label{{fig:{}}}{}}}".format(label, label))
print(r"\end{figure}")
if __name__ == "__main__":
main()