How to import png images Python

25,166

You can display an image from file in a Jupyter Notebook as follows:

from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(url=img)

where img = 'fig31_Drosophila.jpg' is the path and filename of the image you want. (here, the image is in the same folder as the main script)

alternatively:

from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(filename=img)

You can specify optional args (for width and height for instance:

from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(url=img, width=100, height=100)
Share:
25,166

Related videos on Youtube

MichaelRSF
Author by

MichaelRSF

Hi, I'm Michael, an entrepeneur and Udemy Instructor with over 15,000 students. DISCOUNT COUPON You can get the Practical Python 3 for Beginners (2018) for a discount price of £9.99 from the link to the coupon: https://www.udemy.com/learn-practical-python-3-for-beginners-2018/?couponCode=PRO_PY2018 Click on Link for Discounted Course for £9.99 The course contains sections on: Installing Python and Jupyter Notebook Basics (Variables, Strings, Formatting) Data Structures (Lists, Tuples, Dictionaries, Sets) Control Flow & Loops (For loops, While Loops, Zip, Enum, Break, Pass, Continue) Comprehension Functions (Functions, Decorators, Random Module, Scope, Args, Kwargs) Handle Errors (Try, Except, Finally) Files (TXT Files, Pickle Files, OS Module) Mini-Project (Scissors, Paper, Rock game) Generators OOP (Object Orientated Programming) Numpy Pandas Matplotlib I'll also be adding new sections regularly. Plus projects that you could use for university, work or to show off to potential employers. The course also has zip files for each section, with both ipython files and PDF versions of the lectures. If you change your mind, there is the 30-day money back guarantee. I hope to see you in the course =) YOUTUBE My YouTube channel, Holistic Python. Subscribe for regular, weekly content. Click on Link to My YouTube channel, Holistic Coding

Updated on August 29, 2020

Comments

  • MichaelRSF
    MichaelRSF over 3 years

    I've tried to import a png file in Python 3.6 with Jupyter Notebook with no success. I've seen some examples that don't work, at least not anymore, i.e.

     import os,sys
     import Image
     jpgfile = Image.open("picture.jpg")
    

    There is no module called Image that I can install with either: conda install Image or pip install Image

    Any simple solution would be greatly appreciated!

    • Reblochon Masque
      Reblochon Masque over 6 years
    • Reblochon Masque
      Reblochon Masque over 6 years
    • MichaelRSF
      MichaelRSF over 6 years
      Thanks for the response, Reblochon. However, I'm trying to import images using Python in any IDE, I just happen to be using Jupyter Notebook. I've looked at the links, and they were only for importing images from websites. I'd like to import a saved png file from a folder. And not using Markdown. I'm surprised trying to find the solution is so ambigious :/
  • MichaelRSF
    MichaelRSF over 6 years
    Thank you, much appreciated Reblochon! I managed to finally get it to work. I kept getting a broken image output. I had to make sure the file path was correct. from IPython.display import Image, img = "C:/Users/Michael/Call_Options_Graphv_1.jpg", Image(url=img)