3

I am new to python. But I got a task and I need to Displaying/getting Images from an URL. I have been using Jupyter notebook with python to try to do this.

import sys
print(sys.version)
3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]

I was trying to do it as in this post but none of the answers work.

With

import urllib, cStringIO

file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)

I get:

ImportError                               Traceback (most recent call last)
<ipython-input-33-da63c9426dad> in <module>()
      1 url='http://images.mid-day.com/images/2017/feb/15-Justin-Bieber.jpg'
      2 print(url)
----> 3 import urllib, cStringIO
      4 
      5 file = cStringIO.StringIO(urllib.urlopen(URL).read())

ImportError: No module named 'cStringIO'

With:

from PIL import Image
import requests
from io import BytesIO

response = requests.get(url)
img = Image.open(BytesIO(response.content))

I get:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-168cd6221ea3> in <module>()
      1 #response = requests.get("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg")
----> 2 response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8')
      3 img = Image.open(StringIO(response.content))

AttributeError: 'Response' object has no attribute 'read'

With:

from PIL import Image
import requests
from StringIO import StringIO

response = requests.get(url)
img = Image.open(StringIO(response.content))

I get:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-37-5716207ad35f> in <module>()
      3 from PIL import Image
      4 import requests
----> 5 from StringIO import StringIO
      6 
      7 response = requests.get(url)

ImportError: No module named 'StringIO'

Etc....

I thought it was going to be an easy task, but so far I haven't been able to find an answer. I really hope someone can help me

サルバドル
  • 379
  • 1
  • 6
  • 12

2 Answers2

8

This worked for me

from PIL import Image
import requests
from io import BytesIO

url = "https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.show()

You are getting an error because you used the line response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8') instead. I'd switch back to using response = requests.get(url)

Additionally, for your error: ImportError: No module named 'cStringIO', you are using python3. StringIO and cStringIO from python 2 were removed in python 3. Use from io import StringIO instead. See StringIO in Python3 for more details.

alexbhandari
  • 1,310
  • 12
  • 21
5

This might be duplicated with https://stackoverflow.com/a/46954931/4010864.

For your third option with PIL you can try this:

from PIL import Image
import requests
import matplotlib.pyplot as plt

response = requests.get(url, stream=True)
img = Image.open(response.raw)

plt.imshow(img)
plt.show()
bluenex
  • 349
  • 3
  • 11
  • Thank you very much. Was trying to figure this out for 30 mins and your code solved my problem . . Works for both python 2 and python 3 – Nic Scozzaro Apr 08 '21 at 16:56