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">&lt;<a href="mailto:fredrik@pythonware.com">fredrik@pythonware.com</a>&gt;</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&#39;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&#39;s a bit<br>
on the big side, at least for a 32-bit environment.<br>
<br>
Note that PIL doesn&#39;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 (&quot;1&quot;, &quot;L&quot;, &quot;P&quot;):<br>
        bytes = bytes * 4  # 32-bit storage<br>
    if bytes &gt; some threshold:<br>
        raise IOError(&quot;sorry, to big for us&quot;)<br>
    # do operation that actually needs the pixels<br>
<br>
&lt;/F&gt;<br>
<div><div></div><div class="h5"><br>
On Sun, May 30, 2010 at 2:55 AM, Andy McCurdy &lt;<a href="mailto:sedrik@gmail.com">sedrik@gmail.com</a>&gt; wrote:<br>
&gt; Hi,<br>
&gt; We&#39;ve been using PIL for the last 2 years to resize our users&#39; uploaded<br>
&gt; images and have been extremely pleased. Thanks for all your effort.<br>
&gt; I noticed earlier today that we were experiencing issues of servers running<br>
&gt; out of memory. It looks like the problem might be within PIL. I&#39;ve put<br>
&gt; together a few lines of code below that produce the same behavior. This is<br>
&gt; the first time we&#39;ve seen anything like this, and it seems to be a problem<br>
&gt; with a specific image file (URL included in the code below) being converted<br>
&gt; to RGB or resized. This is the only image file I&#39;ve encountered that<br>
&gt; produces this behavior. And no, I&#39;m not quite sure why a user felt the need<br>
&gt; to upload a 17M JPG... :)<br>
&gt; I&#39;ve tried both PIL 1.1.6 and 1.1.7, and the same behavior occurs on both<br>
&gt; Ubuntu 8.04 and OS X 10.5.<br>
&gt; A bug fix would be great, but given the infrequency of seeing this problem,<br>
&gt; I&#39;d settle for a way to detect whether the image I&#39;m working with will cause<br>
&gt; PIL issues so that I can avoid it.<br>
&gt; Thanks!<br>
&gt; -andy<br>
&gt; ##### Example Code #####<br>
&gt; import StringIO<br>
&gt; import urllib2<br>
&gt; from PIL import Image<br>
&gt; image_data =<br>
&gt; urllib2.urlopen(&#39;<a href="http://media.giantbomb.com/uploads/8/84310/1291762-img016.jpg&#39;).read(" target="_blank">http://media.giantbomb.com/uploads/8/84310/1291762-img016.jpg&#39;).read(</a>)<br>
&gt; io = StringIO.StringIO(image_data)<br>
&gt; img = Image.open(io)<br>
&gt; # either of the following two lines causes Python to consume &gt; 2G of memory<br>
&gt; and not return.<br>
&gt; converted_img = img.convert(&#39;RGB&#39;)<br>
&gt; # or<br>
&gt; resized_img = img.resize((200, 150), Image.ANTIALIAS)<br>
</div></div>&gt; _______________________________________________<br>
&gt; Image-SIG maillist  -  <a href="mailto:Image-SIG@python.org">Image-SIG@python.org</a><br>
&gt; <a href="http://mail.python.org/mailman/listinfo/image-sig" target="_blank">http://mail.python.org/mailman/listinfo/image-sig</a><br>
&gt;<br>
&gt;<br>
</blockquote></div><br></div>