Skip to content
← Back to Community
Adding Images In Python
Profile icon
MisterMr

How do you add images in Python?

Answered by OldWizard209 [earned 5 cycles]
View Answer
Voters
Profile icon
TaskerN
Profile icon
Krutova
Profile icon
fnemaz
Profile icon
MisterMr
Comments
hotnewtop
Profile icon
OldWizard209

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:

image

Hope this helped :P :D :) :}

Profile icon
ashmolekangaroo

@OldWizard209 for amongus.png, where do you get that? do you have to make a new file in replit? pleases explain

Profile icon
OldWizard209

yea just upload the amongus file @ashmolekangaroo

Profile icon
LukeMahon

it did not work for me. @OldWizard209

Profile icon
ashmolekangaroo

yeah how do i upload the file, im a noob btw @OldWizard209

Profile icon
scratchproo

thank you oldwizard209