Display a .png image from python on mint-15 linux

23,792

if you just want to display it, you may use matplotlib:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('file-name.png')
plt.imshow(img)
plt.show()
Share:
23,792
Chris Rigano
Author by

Chris Rigano

Updated on July 14, 2022

Comments

  • Chris Rigano
    Chris Rigano almost 2 years

    I am trying to display a .png file I constructed using the following.

    import pydot, StringIO
    dot_data = StringIO.StringIO() 
    tree.export_graphviz( clf, out_file = dot_data,    
    feature_names =['age', 'sex', 'first_class', 'second_class', 'third_class'])
    graph = pydot.graph_from_dot_data( dot_data.getvalue())
    graph.write_png('titanic.png') 
    from IPython.core.display import Image
    Image( filename ='titanic.png')
    

    I tried the following but neither errors nor .png are displayed:

    from PIL import Image
    image = Image.open("titanic.png")
    image.show()
    
  • Chris Rigano
    Chris Rigano over 10 years
    Thanks behzad.nouri, but, all I get is a black screen <module 'DtreeObj2' from 'DtreeObj2.py'> >>> DtreeObj2.construct() >>> ... no import errors or other, just no picture I have image viewer resident and that is what opens my .prn when I manually click on it.
  • noumenal
    noumenal almost 9 years
    plt.show() is missing after plt.imshow( img ). Otherwise the pop-up window will not show.