views:

636

answers:

3

I have a function in which I'm trying to resize a photo twice from request.FILES['image']. I'm using the image.thumbnail() with the Parser as well. This works fine when I create one thumbnail, but in my view if I repeat the exact same thing again, it fails in the parser via IOError cannot parse image. I'm very confused. I've created StringIO files in memory instead of using Django's UploadedFile object as-is and it still does the same thing. Any help is much appreciated.

Suppose I wanted to do the following twice (with two different thumbnailing sizes) all without retrieving the URL twice:

import urllib2
from PIL import Image, ImageFile, ImageEnhance

# create Image instance
file = urllib2.urlopen(r'http://animals.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/animals/images/primary/kemps-ridley-sea-turtle.jpg')
parser = ImageFile.Parser()
while True:
    s = file.read(1024)
    if not s:
        break
    parser.feed(s)
image = parser.close()

# make thumbnail
size = (75, 75)
image.thumbnail(size, Image.ANTIALIAS)
background = Image.new('RGBA', size, (255, 255, 255, 0))
background.paste(
    image,
    ((size[0] - image.size[0]) / 2, (size[1] - image.size[1]) / 2))

background.save('copy.jpg')

For instance:

image = parser.close()
image2 = parser.close() # Obviously this doens't work
image2 = image # Obviously this doesn't either but you get what I need to do here
# Do 2 thumbnails with only one original source.

... other code ommitted ...

image.save('copy.jpg')
image2.save('copy.jpg')
A: 

I'm assuming it's failing on the image = parser.close() line with an IOError. so there's probably something wrong with the way ImageFile is getting the image data. Have you tried making reading from a local file instead?

If the parser managed to decode an image, it returns an Image object. Otherwise, this method raises an IOError exception.

Source.

Dominic Rodger
+1  A: 

If this works once, as you say, the image you retrieved is just fine. There are at least two different ways to get multiple thumbnails out of single PIL images.

  1. You can use PIL's resize method, which will return a resized copy of the original. You just have to calculate the dimensions you'll need if you want to keep the proportions intact.
  2. Use Image.copy() to get a copy of the image.

Like this:

original = parser.close()
...

thumb1 = original.copy()
size = (75,75)
thumb1.thumbnail(size, Image.ANTIALIAS)
...

thumb2 = original.copy()
thumbnail2 = original.resize(size2, Image.ANTIALIAS)
...

This way, the original will not be altered and you can get as many copies as you need.

af
Thanks, image.copy() was exactly what I needed. I'm too ridiculous to scroll down and notice that method in the docs, so I was trying to clone the UploadedFile object myself and reuse it :)
orokusaki
+1  A: 

A simpler solution than copying the original image is to instead reset the file pointer between calls to thumbnail(...) like so:

original.seek(0)
Ben
Oh, nice! That will save a lot of memory too right?
orokusaki