#
# Fix up IMG tags in given documents to contain correct sizes. No longer
# requires the identify(1) program from the ImageMagick suite if you have
-# a decent file(1) and rdjpgcom(1). To fix up sizes for non-local images
-# it requires the httpget(1) script.
+# a decent file(1) and rdjpgcom(1).
#
# Works with Python 1.5.2
#
# try commands in least to most expensive order, and notice when a command
# returns not to try it again.
#
-import sys, os, getopt, filecmp, string, re, urllib, commands
+import sys, os, getopt, cmp, string, re, urllib, commands
-version = "2.0, 11 July 2001";
+version = "2.1, 26 July 2001";
splash = """imgsizer version %s, Eric S. Raymond <esr@thyrsus.com>
See <http://www.tuxedo.org/~esr/software.html> for updates."""
-n, --no-overwrite
- Don't overwrite existing width and heigbt tags if both are present.
+ Don't overwrite existing width and height tags if both are present.
"""
if ch == '':
return
outfp.write(lead)
- if lead.find('>') > -1:
+ if string.find(lead, '>') > -1:
continue
if not lead in ("img", "IMG"):
continue
return (sizes.group(1), sizes.group(2))
elif status == 127:
rdjpgcom = 0
- # Next try identify(1), more expensive nut bulletproof
+ # Next try identify(1), more expensive but bulletproof
if magick:
(status, output) = commands.getstatusoutput("identify " + filename)
if status == 0:
if sizes:
return (sizes.group(1), sizes.group(2))
elif status == 127:
+ sys.stderr.write("imgsizer: giving up on ImageMagick\n")
magick = 0
# All attempts failed
- sys.stderr.write("imgsizer: couldn't analyze " + src)
+ sys.stderr.write("imgsizer: couldn't analyze %s\n" % src)
source = re.compile('SRC="?([^" \t\n]*)"?', re.I)
awidth = re.compile(' *WIDTH="?[0-9]*"?', re.I)
root = "."
out = "imgsizer-out$$"
-dir = "." # NOTE: if you are doing <yourfile , make sure that pwd is correct!
+dir = "." # NOTE: if you are doing <yourfile make sure that pwd is correct!
# Optimization latches -- if an attempt to invoke a command returns 127
# "not found" there will turn off and that command won't be tried again.
sizefix(sys.stdin, sys.stdout)
else:
for file in arguments:
- base = os.path.basename(file)
- dir = os.path.dirname(file)
try:
infp = open(file)
except:
raise SystemExit, 1
tempfile = file + ".~imgsizer-%d~" % os.getpid()
try:
- outfp = open(tempfile)
+ outfp = open(tempfile, "w")
except OSError:
print "imgsizer: can't open tempfile"
raise SystemExit, 1
sizefix(infp, outfp)
- if filecmp.cmp(file, tempfile):
+ if cmp.cmp(file, tempfile):
os.remove(tempfile)
else:
try: