]> thyrsus.com Git - imgsizer.git/commitdiff
After PIL change.
authorEric S. Raymond <esr@thyrsus.com>
Thu, 5 Aug 2004 22:24:45 +0000 (22:24 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 5 Aug 2004 22:24:45 +0000 (22:24 +0000)
imgsizer

index 50336fd169270ccc695fa55fde4ca93965c20660..3aa227413e985fee4f903b416e78af50bde37ac3 100755 (executable)
--- a/imgsizer
+++ b/imgsizer
 #    Verify and merge Lucien Saviot's patch to produce XHTML from XHTML input.
 #    Also his change to handle spurious lin e breaks produced by Dave Raggett's
 #    tidy(1) utility.
+#
+# Modified by Andrew Gwozdziewycz <gwozdzie@lucas.cis.temple.edu>, 17 June 2004
+#
+#    Added support for the Python Imaging Library to determine size in case of
+#    failure from file(1), rdjpgcom(1) and identify(1).
 
 import sys, os, getopt, string, re, urllib, commands
 
@@ -77,10 +82,10 @@ try:
 except ImportError:
     import cmp
 
-version = "2.5, 14 November 2003";
+version = "2.7, 05 Aug 2004";
 
 splash = """imgsizer version %s, Eric S. Raymond <esr@thyrsus.com>
-See <http://www.tuxedo.org/~esr/software.html> for updates."""
+See <http://www.catb.org/~esr/software.html> for updates."""
 
 usage = """Usage: imgsizer [OPTIONS] [HTML File]
 
@@ -155,7 +160,7 @@ rdjpg_match = re.compile (r" ([0-9]+)w *\* *([0-9]+)h")
 
 def imgsize(src):
     "Return the image size in pixels for a given image source."
-    global magick, rdjpgcom
+    global magick, rdjpgcom, pythonimage
     try:
         (filename, headers) = urllib.urlretrieve(src)
     except IOError:
@@ -187,6 +192,18 @@ def imgsize(src):
         elif status == 127:
             sys.stderr.write("imgsizer: giving up on ImageMagick\n")
             magick = 0
+    # if that fails, try at _LAST_ resort Python Imaging Library
+    # open doesn't actually load all the data, so it shouldn't be too expensive
+    if pythonimage:
+        try:
+           import Image
+           pyimg = Image.open(filename)
+           return pyimg.size
+        except (ImportError, IOError):
+           sys.stderr.write("imgsizer: giving up on Python Imaging Library\n")
+           pythonimage = 0
+           pass
+
     # All attempts failed
     sys.stderr.write("imgsizer: couldn't analyze %s\n" % src)
 
@@ -233,8 +250,9 @@ 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.
-magick = 1     # using ImageMagick by default
-rdjpgcom = 1   # using rdjpcom by default
+magick = 0     # using ImageMagick by default
+rdjpgcom = 0   # using rdjpcom by default
+pythonimage = 1 # use python imaging library
 
 # Collect options from the environment first, then the command line
 options = os.environ.get("IMGSIZER")