Thanks for the explanation. As your and others have suggested, I think limiting the size of the source file seems like a reasonable idea.<div><br></div><div>-andy <br><br><div class="gmail_quote">On Sun, May 30, 2010 at 5:59 AM, Fredrik Lundh <span dir="ltr"><<a href="mailto:fredrik@pythonware.com">fredrik@pythonware.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">The file has mode=RGB size=20400x28079, so you'd need about 2291246400<br>
bytes to load it all into memory at once, and twice that to do e.g.<br>
color conversion (which creates a second image memory), so it's a bit<br>
on the big side, at least for a 32-bit environment.<br>
<br>
Note that PIL doesn't actually read the pixels until you do something<br>
that needs them, so you can do a sanity check before you proceed; e.g.<br>
<br>
im = Image.open(...)<br>
bytes = im.size[0] * im.size[1]<br>
if im.mode not in ("1", "L", "P"):<br>
bytes = bytes * 4 # 32-bit storage<br>
if bytes > some threshold:<br>
raise IOError("sorry, to big for us")<br>
# do operation that actually needs the pixels<br>
<br>
</F><br>
<div><div></div><div class="h5"><br>
On Sun, May 30, 2010 at 2:55 AM, Andy McCurdy <<a href="mailto:sedrik@gmail.com">sedrik@gmail.com</a>> wrote:<br>
> Hi,<br>
> We've been using PIL for the last 2 years to resize our users' uploaded<br>
> images and have been extremely pleased. Thanks for all your effort.<br>
> I noticed earlier today that we were experiencing issues of servers running<br>
> out of memory. It looks like the problem might be within PIL. I've put<br>
> together a few lines of code below that produce the same behavior. This is<br>
> the first time we've seen anything like this, and it seems to be a problem<br>
> with a specific image file (URL included in the code below) being converted<br>
> to RGB or resized. This is the only image file I've encountered that<br>
> produces this behavior. And no, I'm not quite sure why a user felt the need<br>
> to upload a 17M JPG... :)<br>
> I've tried both PIL 1.1.6 and 1.1.7, and the same behavior occurs on both<br>
> Ubuntu 8.04 and OS X 10.5.<br>
> A bug fix would be great, but given the infrequency of seeing this problem,<br>
> I'd settle for a way to detect whether the image I'm working with will cause<br>
> PIL issues so that I can avoid it.<br>
> Thanks!<br>
> -andy<br>
> ##### Example Code #####<br>
> import StringIO<br>
> import urllib2<br>
> from PIL import Image<br>
> image_data =<br>
> urllib2.urlopen('<a href="http://media.giantbomb.com/uploads/8/84310/1291762-img016.jpg').read(" target="_blank">http://media.giantbomb.com/uploads/8/84310/1291762-img016.jpg').read(</a>)<br>
> io = StringIO.StringIO(image_data)<br>
> img = Image.open(io)<br>
> # either of the following two lines causes Python to consume > 2G of memory<br>
> and not return.<br>
> converted_img = img.convert('RGB')<br>
> # or<br>
> resized_img = img.resize((200, 150), Image.ANTIALIAS)<br>
</div></div>> _______________________________________________<br>
> Image-SIG maillist - <a href="mailto:Image-SIG@python.org">Image-SIG@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/image-sig" target="_blank">http://mail.python.org/mailman/listinfo/image-sig</a><br>
><br>
><br>
</blockquote></div><br></div>