5

This has been solved. As pointed by James in the comment, the link that does not work is not a direct link to an image.


I'm learning python through a ipython notebook online. In the ipython notebook, an image from web is displayed using IPython.display.image. I tried it on my laptop and it works. However, when I try to display other images from web, it does not work.

The scripts that work is:

from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300, height=300)

The scripts that do not work (but show a broken image without any other outputs) is:

from IPython.display import Image                        
url='https://en.wikipedia.org/wiki/Iris_setosa#/media/File:Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300, height=300)

I saw two other related questions but they still do not answer my questions.

Display an image from URL

How to display an image from web?

Thank you very much!

Community
  • 1
  • 1
yuqian
  • 257
  • 3
  • 10
  • 5
    The second URL is not a direct link to the image URL, but rather displays a webpage with a wrapper around the image. – James Nov 17 '16 at 02:36
  • 2
    @James, thank you so much! Now I understand. I should click the image in the link I provided to get the direct link to the image URL. Then it works! – yuqian Nov 17 '16 at 02:44
  • 1
    No problem. If you ever have trouble getting the image URL, you can always right-click on the image and copy the image location or inspect the element to find where the URL was called from. – James Nov 17 '16 at 02:48

2 Answers2

8

First you have to get image. simply you can do it using requests

import requests
import IPython.display as Disp
url = 'https://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Disp.Image(requests.get(url).content)
gbmhunter
  • 1,747
  • 3
  • 23
  • 24
0

This is now available without requests in IPython using url keyword:

from IPython import Image

Image(url="https://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg")
Elias
  • 114
  • 1
  • 4