On 6 16 , 6 27 , Adam Teale <[EMAIL PROTECTED]> wrote:
> hey guys
>
> Is there a builtin/standard install method in python for retrieving or
> finding out an image's dimensions?
Sorry, after i review these code in http://www.pycode.com/modules/?id=32,
i found some(not just a few) *BUGS* in it.
I must apologize to you for this.
I have rewrite the moudle.
please have a try.
"""Recognize image file formats and size based on their first few
bytes."""
# Perl Image::Size module clone
# see more http://search.cpan.org/author/RJRAY/Image-Size-3.01/lib/Image/Size.pm
# rewrited by jigloo([EMAIL PROTECTED])
# GPL-2 license
__all__ = ["what", "imgsz", "size"]
import os # for os.path os.error sys.stderr
import StringIO# for StringIO.StringIO
import struct # for unpack
import re # for regex
# jpegsize: gets the width and height (in pixels) of a jpeg file
#
def jpegsize(stream):
(x, y, error) = (None, None, "could not determine JPEG size")
# Dummy read to skip header ID
stream.read(2)
while True:
# Extract the segment header.
(marker, code, length) = struct.unpack("!BBH", stream.read(4))
# Verify that it's a valid segment.
if marker != 0xFF:
# Was it there?
error = "JPEG marker not found"
break
elif code >= 0xC0 and code <= 0xC3:
# Segments that contain size info
(y, x) = struct.unpack("!xHH", stream.read(5))
error = "no error"
break
else:
# Dummy read to skip over data
stream.read(length - 2)
return ("JPEG", x, y, error)
# bmpsize: size a Windows-ish BitMaP image
#
def bmpsize(stream):
(x, y, error) = (None, None, "Unable to determine size of BMP data")
# Dummy read to skip header data
stream.read(18)
(x, y) = struct.unpack(" 0 and y > 0:
error = "no error"
return ("BMP", x, y, error)
# pngsize : gets the width & height (in pixels) of a png file
# cor this program is on the cutting edge of technology! (pity it's
blunt!)
#
def pngsize(stream):
(x, y, error) = (None, None, "could not determine PNG size")
# Dummy read to skip header data
stream.read(12)
if stream.read(4) == "IHDR":
(x, y) = struct.unpack("!LL", stream.read(8))
error = "no error"
return ("PNG", x, y, error)
# gifsize : Subroutine gets the size of the specified GIF
#
# Default behavior for GIFs is to return the "screen" size
GIF_BEHAVIOR = 0
#
def gifsize(stream):
if GIF_BEHAVIOR > 2:
return ("GIF", 0, 0, "Out-of-range value for GIF_BEHAVIOR: %d" %
GIF_BEHAVIOR)
# Skip over the identifying string, since we already know this is a
GIF
type = stream.read(6)
buf = stream.read(5)
if len(buf) != 5:
return ("GIF", 0, 0, "Invalid/Corrupted GIF (bad header)")
(sw, sh, x) = struct.unpack(" num_dirent:
break
tag = struct.unpack(be+"H", ifd[:2])[0] # ...and decode its tag
type = struct.unpack(be+"H", ifd[2:2+2])[0] # ...and the data
type
# Check the type for sanity.
if type > len(packspec) or packspec[type] is None:
continue
if tag == 0x0100: # ImageWidth (x)
# Decode the value
x = struct.unpack(packspec[type], ifd[8:4+8])[0]
elif tag == 0x0101: # ImageLength (y)
# Decode the value
y = struct.unpack(packspec[type], ifd[8:4+8])[0]
# Decide if we were successful or not
if x and y:
error = "no error"
else:
error = ""
if x is None:
error = "ImageWidth "
if y is None:
if error != "":
error = error + "and "
error = error + "ImageWidth "
error = error + "tag(s) could not be found"
return ("TIFF", x, y, error)
# psdsize : determine the size of a PhotoShop save-file (*.PSD)
#
def psdsize(stream):
(x, y, error) = (None, None, "could not determine PSD size")
stream.read(14)
(y, x)