Ask coding questions

← Back to all posts
Adding Images In Python
MisterMr (1)

How do you add images in Python?

Answered by OldWizard209 (1528) [earned 5 cycles]
View Answer
Comments
hotnewtop
OldWizard209 (1528)

You can't add images directly in Python because it is a console-based language and you can't add images on the console directly. So rather, you will have to use Python's Tkinter Framework.

Lemme quickly overview how can you get an image to show in Tkinter.
Import Tkinter :

from tkinter import *      

Create the window using Tk class:

root = Tk()

Create a cavas and pass in the width, height parameters:

canvas = Canvas(root, width = 700, height = 700)      
canvas.pack()

Add an image file, only works with pngs:

img = PhotoImage(file="amongus.png")      
canvas.create_image(20,20, anchor=NW, image=img)      
mainloop() 

And run the window using mainloop(). Now, you will have your image displayed on a UI interface:

Hope this helped :P :D :) :}