Pillow does not seem to work inside a function, but works fine on interpreter
<p>The function below, part of a bigger class works fine on all the images except this one -> <a href="http://www.worldbank.org/content/dam/wbr/About/Pres/jyk-hs-offical.png" rel="nofollow">http://www.worldbank.org/content/dam/wbr/About/Pres/jyk-hs-offical.png</a></p>

<pre><code>def _fetch_image_size(self, image_url):
    size = None
    if '.svg' not in image_url:
        response = requests.get(image_url, headers=self._headers)
        if response.status_code == 200:
            response.raw.decode_content = True
            try:
                image = Image.open(io.BytesIO(response.content))
                size = image.size
            except (IOError, OSError) as error:
                print error
                print image_url
        response.close()
    return size
</code></pre>

<p>when the above function is called as part of the class object, it raises me this error.</p>

<blockquote>
  <p>cannot identify image file &lt;_io.BytesIO object at 0x1062e1b30></p>
</blockquote>

<p>however on the command line interpreter, when i do this</p>

<pre><code>import io, requests
from PIL import Image

response = requests.get('http://www.worldbank.org/content/dam/wbr/About/Pres/jyk-hs-offical.png')
response.raw.decode_content = True
image = Image.open(io.BytesIO(response.content))
print image.size
</code></pre>

<p>the output is, </p>

<pre><code>(220, 220)
</code></pre>

<p>i am unable to figure out why is it happening?</p>

<p>sorry to bother you all. it appears that i need to sanitize my url in my class and look for blankspaces. Nothing wrong with PIL here.</p>

<p>here is how i pin pointed the faulty code, if anybody is interested.</p>

<p>thankyou for your response. I have diagnosed the problem. pasting the problematic code from my class here.</p>

<pre><code>def _extract_image_urls(self, soup):
    """
    extracts all the &lt;img src=''&gt; tags

    Args:
        soup (obj): the BeautifulSoup object

    Returns:
        url (str): string for url
    """
    for img in soup.findAll("img", src=True):
        yield urlparse.urljoin(self._url, img["src"])
</code></pre>

<p>```</p>

<p>this above code gets me all the urls i need to pass onto my <code>_fetch_image_size()</code> function.</p>

<p>i modified my <code>_fetch_image_size</code> function to have this</p>

<pre><code>.....
response = requests.get(image_url, headers=self._headers)
print 'Request URL: {url}'.format(url=image_url)
print 'Response URL: {url}'.format(url=response.url)
.....
</code></pre>

<p>and here is the response.</p>

<blockquote>
  <p>s = LinkScraper('<a href="http://www.worldbank.org/en/about/president/about-the-office/bio" rel="nofollow">http://www.worldbank.org/en/about/president/about-the-office/bio</a>')
  Request URL: <a href="http://www.worldbank.org/content/dam/wbr/img/mobile-menu-lines.png" rel="nofollow">http://www.worldbank.org/content/dam/wbr/img/mobile-menu-lines.png</a>
  Response URL: <a href="http://www.worldbank.org/content/dam/wbr/img/mobile-menu-lines.png" rel="nofollow">http://www.worldbank.org/content/dam/wbr/img/mobile-menu-lines.png</a></p>
  
  <p>Request URL: <a href="http://www.worldbank.org/etc/designs/wbr/clientlibs/img/icon-search-black.png" rel="nofollow">http://www.worldbank.org/etc/designs/wbr/clientlibs/img/icon-search-black.png</a>
  Response URL: <a href="http://www.worldbank.org/etc/designs/wbr/clientlibs/img/icon-search-black.png" rel="nofollow">http://www.worldbank.org/etc/designs/wbr/clientlibs/img/icon-search-black.png</a></p>
  
  <p>Request URL: <a href="http://www.worldbank.org/content/dam/wbr/About/Pres/jyk-hs-offical.png" rel="nofollow">http://www.worldbank.org/content/dam/wbr/About/Pres/jyk-hs-offical.png</a>
  Response URL: <a href="http://www.worldbank.org/404_response.htm" rel="nofollow">http://www.worldbank.org/404_response.htm</a>
  Error: cannot identify image file &lt;_io.BytesIO object at 0x112414830></p>
</blockquote>

<p>the response that is being passed into the PIL.Image function is an http response and not an image at all!. Nothing wrong with PIL here. I need to sanitize my urls to look for blank spaces here maybe. </p>

<p>thank you @martineau for taking the time. Much appreciated.</p>

<p>//mouse.</p>

python pillow