0

I am trying to display an image from a Url however i am not sure how to do so. Below is my attempt:

imageFile = "http://photo.elsoar.com/wp-content/images/Personal-computer.jpg"
image1 =PhotoImage(open(imageFile))
image1.grid(row=1, column=5)

This just produces an error

Traceback (most recent call last):
  File "C:\Users\Derren\Desktop\Online Shopper.py", line 131, in <module>
image1 =PhotoImage(open(imageFile))

IOError: [Errno 22] invalid mode ('r') or filename: 'http://photo.elsoar.com/wp-content/images/Personal-computer.jpg' This is the image i want to have http://photo.elsoar.com/wp-content/images/Personal-computer.jpg

However it is essential that it is resourced from the internet and not local files

  • Possible duplicate of [How do I read image data from a URL in Python?](http://stackoverflow.com/questions/7391945/how-do-i-read-image-data-from-a-url-in-python) – teknoboy May 19 '17 at 09:21
  • you should use `urllib` (as described in http://stackoverflow.com/questions/7391945/how-do-i-read-image-data-from-a-url-in-python) or use a similar approach – teknoboy May 19 '17 at 09:22
  • This returns Traceback (most recent call last): File "C:\Users\Derren\Desktop\Online Shopper.py", line 132, in img = Image.open(imgfile) AttributeError: class Image has no attribute 'open' – Derren Healy May 19 '17 at 09:30

2 Answers2

0

The function open() only works on locals files. You can see doc here part 7.2. If you want to work with an online image, you can use this :

from PIL import Image
import requests
from io import BytesIO

response = requests.get("url")
img = Image.open(BytesIO(response.content))
img.save("my_img.png") #with the good filename extension
Ethoytaryn
  • 39
  • 4
0

If you particularly want a PhotoImage based solution that this should work:

import urllib

f = urllib.urlopen("http://photo.elsoar.com/wp-content/images/Personal-computer.jpg")
data=f.read()
root = Tkinter.Tk()
image1 =PhotoImage(data)
yeniv
  • 1,589
  • 11
  • 25
  • How do i add this to a GUI,the grid function doesn't work – Derren Healy May 19 '17 at 10:49
  • f = urllib.urlopen("http://photo.elsoar.com/wp-content/images/Personal-computer.jpg") data=f.read() image1 =PhotoImage(data) image1.grid(row=1,column=1) – Derren Healy May 19 '17 at 16:46
  • Traceback (most recent call last): File "C:\Users\Derren\Desktop\Online Shopper.py", line 134, in image1.grid(row=1,column=1) AttributeError: PhotoImage instance has no attribute 'grid' – Derren Healy May 19 '17 at 16:46