Showing an image from console in Python

后端 未结 10 548
感动是毒
感动是毒 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:44

    You cannot display images in a console window. You need a graphical toolkit such as Tkinter, PyGTK, PyQt, PyKDE, wxPython, PyObjC or PyFLTK. There are plenty of tutorial how to create siomple windows and loading images iun python.

    0 讨论(0)
  • 2020-12-02 20:47

    I made a simple tool that will display an image given a filename or image object or url.
    It's crude, but it'll do in a hurry.

    Installation:

     $ pip install simple-imshow
    

    Usage:

    from simshow import simshow
    simshow('some_local_file.jpg')  # display from local file
    simshow('http://mathandy.com/escher_sphere.png')  # display from url
    
    0 讨论(0)
  • 2020-12-02 20:48

    In Xterm-compatible terminals, you can show the image directly in the terminal. See my answer to "PPM image to ASCII art in Python"

    ImageMagick's "logo:" image in Xterm (show picture in new tab for full size viewing)

    0 讨论(0)
  • 2020-12-02 20:48

    You can also using the Python module Ipython, which in addition to displaying an image in the Spyder console can embed images in Jupyter notebook. In Spyder, the image will be displayed in full size, not scaled to fit the console.

    from IPython.display import Image, display
    display(Image(filename="mypic.png"))
    
    0 讨论(0)
  • 2020-12-02 20:54

    In a new window using Pillow/PIL

    Install Pillow (or PIL), e.g.:

    $ pip install pillow
    

    Now you can

    from PIL import Image
    with Image.open('path/to/file.jpg') as img:
        img.show()
    

    Using native apps

    Other common alternatives include running xdg-open or starting the browser with the image path:

    import webbrowser
    webbrowser.open('path/to/file.jpg')
    

    Inline a Linux console

    If you really want to show the image inline in the console and not as a new window, you may do that but only in a Linux console using fbi see ask Ubuntu or else use ASCII-art like CACA.

    0 讨论(0)
  • 2020-12-02 20:54

    Why not just display it in the user's web browser?

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