Showing an image from console in Python

后端 未结 10 549
感动是毒
感动是毒 2020-12-02 20:42

What is the easiest way to show a .jpg or .gif image from Python console?

I\'ve got a Python console program that is checking a data set wh

相关标签:
10条回答
  • 2020-12-02 20:57

    Using the awesome Pillow library:

    >>> from PIL import Image                                                                                
    >>> img = Image.open('test.png')
    >>> img.show() 
    

    This will open the image in your default image viewer.

    0 讨论(0)
  • 2020-12-02 21:02

    If you would like to show it in a new window, you could use Tkinter + PIL library, like so:

    import tkinter as tk
    from PIL import ImageTk, Image
    
    def show_imge(path):
        image_window = tk.Tk()
        img = ImageTk.PhotoImage(Image.open(path))
        panel = tk.Label(image_window, image=img)
        panel.pack(side="bottom", fill="both", expand="yes")
        image_window.mainloop()
    

    This is a modified example that can be found all over the web.

    0 讨论(0)
  • 2020-12-02 21:08

    Since you are probably running Windows (from looking at your tags), this would be the easiest way to open and show an image file from the console without installing extra stuff like PIL.

    import os
    os.system('start pic.png')
    
    0 讨论(0)
  • 2020-12-02 21:11

    Or simply execute the image through the shell, as in

    import subprocess
    subprocess.call([ fname ], shell=True)
    

    and whatever program is installed to handle images will be launched.

    0 讨论(0)
提交回复
热议问题