Image size...

30 views
Skip to first unread message

Mike

unread,
May 29, 2008, 12:49:42 PM5/29/08
to Google App Engine
Having the image library is great. Unfortunately, it doesn't seem to
give us any access to the image attributes (e.g. height and width).
PIL has access to this data, without reading the file or using the c
libraries. Can the height/width attributes be added to the app engine
Image object?

If a user uploads an image, I want to resize it if it is bigger than
500px in any dimension. I'm struggling to see a way to do that with
the current API.

-Mike

tj9991

unread,
May 29, 2008, 1:29:33 PM5/29/08
to Google App Engine
def getImageInfo(data):
data = str(data)
size = len(data)
height = -1
width = -1
content_type = ''

# handle GIFs
if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'):
# Check to see if content_type is correct
content_type = 'image/gif'
w, h = struct.unpack("<HH", data[6:10])
width = int(w)
height = int(h)

# See PNG 2. Edition spec (http://www.w3.org/TR/PNG/)
# Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
# and finally the 4-byte width, height
elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n')
and (data[12:16] == 'IHDR')):
content_type = 'image/png'
w, h = struct.unpack(">LL", data[16:24])
width = int(w)
height = int(h)

# Maybe this is for an older PNG version.
elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'):
# Check to see if we have the right content type
content_type = 'image/png'
w, h = struct.unpack(">LL", data[8:16])
width = int(w)
height = int(h)

# handle JPEGs
elif (size >= 2) and data.startswith('\377\330'):
content_type = 'image/jpeg'
jpeg = StringIO(data)
jpeg.read(2)
b = jpeg.read(1)
try:
while (b and ord(b) != 0xDA):
while (ord(b) != 0xFF): b = jpeg.read
while (ord(b) == 0xFF): b = jpeg.read(1)
if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
jpeg.read(3)
h, w = struct.unpack(">HH", jpeg.read(4))
break
else:
jpeg.read(int(struct.unpack(">H", jpeg.read(2))
[0])-2)
b = jpeg.read(1)
width = int(w)
height = int(h)
except struct.error:
pass
except ValueError:
pass

return content_type, width, height

Duncan

unread,
May 29, 2008, 1:35:16 PM5/29/08
to Google App Engine

Mike

unread,
May 29, 2008, 7:21:11 PM5/29/08
to Google App Engine
Yep. That is the route I was going, just gross to picture all these
developers embedding this code into their apps when it is already in
the library that Google is using for the image processing. Oh well.

tj9991

unread,
May 30, 2008, 12:34:00 AM5/30/08
to Google App Engine
If you would have taken two seconds to read my post, Duncan, you would
have seen that I already pasted the full code for that function.

On May 29, 10:35 am, Duncan <kupu...@googlemail.com> wrote:
> On May 29, 5:49 pm, Mike <mgi...@gmail.com> wrote:
>
> > Having the image library is great.  Unfortunately, it doesn't seem to
> > give us any access to the image attributes (e.g. height and width).
> > PIL has access to this data, without reading the file or using the c
> > libraries.  Can the height/width attributes be added to the app engine
> > Image object?
>
> > If a user uploads an image, I want to resize it if it is bigger than
> > 500px in any dimension.  I'm struggling to see a way to do that with
> > the current API.
>
> I agree it's a pain, but you can check always extract the size just
> using Python. Seehttp://www.google.com/codesearch?hl=en&q=+getImageInfo+show:RjgT7H1iB...

Ian Lewis

unread,
May 30, 2008, 12:46:42 AM5/30/08
to google-a...@googlegroups.com
It's only 6 minutes difference. Give the guy a break. Besides his mail showed up in my inbox before yours though yours has an earlier timestamp.

Ian

2008/5/30 tj9991 <tsl...@gmail.com>:

Barry Hunter

unread,
May 30, 2008, 6:57:00 AM5/30/08
to google-a...@googlegroups.com

Google doesnt use PIL if that is what you mean. The real appengine
uses the same backend as PicasaWeb apparently.

(yes the dev server wraps PIL, but then the two systems wouldnt be
compatible, if only offered teh function locally)

> >
>

--
Barry

- www.nearby.org.uk - www.geograph.org.uk -

Reply all
Reply to author
Forward
Message has been deleted
0 new messages