Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying/getting Images from an URL in Python

Tags:

python

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

like image 413
サルバドル Avatar asked Jun 11 '26 19:06

サルバドル


2 Answers

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.

like image 96
alexbhandari Avatar answered Jun 14 '26 08:06

alexbhandari


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()
like image 44
bluenex Avatar answered Jun 14 '26 07:06

bluenex