How do i add an image to the python GUI? [duplicate] - python

This question already has answers here:
How to add an image in Tkinter?
(10 answers)
Closed 4 years ago.
The below code is a section of my code which is supposed to be a stock management system. I have encountered several problems which i would appreciate any help for.
from tkinter import*
from tkinter import Tk, StringVar, ttk
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
import random
import datetime
import time;
import csv
opencsv=open('RED.csv','a')
Data=[]
LH=Label(LowerHeading, font=('arial',12,'bold'), text ="Update", bd = 10, width = 15, anchor = 'w')
LH.grid(row=1,column=0)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
start.mainloop()
I Would like to add an image so for example if IDO1 is clicked by the user an image of a dress would be shown
The link didn't address my problem. I gained an error that said NO module named PIL

You can have a look into PhotoImage Class.
Inserting images is simple.
import Tkinter as tk
from PIL import ImageTk, Image
path = 'C:/xxxx/xxxx.jpg'
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
Hope this helps.
Do have a look at Using Images in Labels section of Tkinter Labels page.
You can read Tkinter's PhotoImage and Label widgets for customizations.
UPDATE -
For installing 'PIL' from pillow -
PIL is from package pillow. use -
sudo pip install pillow
Then from PIL import ImageTk will not give errors.
If pip is not installed then install it by using-
sudo apt-get install python-pip

Related

select an image to show with dialog [duplicate]

This question already has an answer here:
displaying the selected image using tkinter
(1 answer)
Closed 4 years ago.
How do I select an image to show using dialog? I only can show an image when I know it's exact path. But I want to be like that dialog when you upload your photo to social networks or something. Now I have this code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
img = Image.open(r"images\photo1.jpg")
show_img = ImageTk.PhotoImage(img)
panel = Label(root, image=show_img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
You use the Tkinter FileDialog, let the user choose the image and use that location as string for your image http://effbot.org/tkinterbook/tkinter-file-dialogs.htm

Image in TreeView not showing Tkinter

I am making a TreeView in Tkinter Python 3.4 I have added a chrome logo but the image never appears.
treeview=ttk.Treeview(frame3)
chromelogo=PhotoImage(file="./Images/minor-logo.png")
chromelogo=chromelogo.subsample(10,10)
treeview.pack(fill=BOTH,expand=True)
treeview.insert('','0','Chrome',text='Chrome', image=chromelogo)
treeview.insert('Chrome','0',"shit",text="shit",image=chromelogo)
treeview.insert('','1','Chrome3',text='Chrome3')
The link for chrome logo: http://logos-download.com/wp-content/uploads/2016/05/Chrome_icon_bright.png
Photoimage doesn't let you open images rather that a gif type if you want to open an image rather than gif in tkinter try the PIL module. you could use any method to install the PIL module, i like the command line
python -m pip install pillow
since you want to your image to feet in the tree view use this to resize and insert it
from tkinter import *
from tkinter import ttk
from PIL import ImageTk,Image
win=Tk()
chromelogo=Image.open("./Images/minor-logo.png")#open the image using PIL
imwidth=10#the new width you want
#the next three lines of codes are used to keep the aspect ration of the image
wpersent=(imwidth/float(chromelogo.size[0]))
hsize=int(float(chromelogo.size[1])*float(wpersent))#size[1] means the height and the size[0] means the width you can read more about this in th PIL documentation
chromelogo=ImageTk.PhotoImage(chromelogo.resize((imwidth,hsize),Image.ANTIALIAS))# set the width and put it back in the chromelogo variable
treeview=ttk.Treeview(win)
treeview.pack(fill=BOTH,expand=False)
treeview.insert('','0','Chrome',text='Chrome', image=chromelogo)
treeview.image = chromelogo#this one is for telling tkinter not to count the image as garbage
treeview.grid(row=0,rowspan=2,columnspan=2,padx=220,sticky=N+W,pady=20)
chromelogo2=ImageTk.PhotoImage(Image.open("./Images/minor-logo.png"))
treeview.insert('Chrome','0',"shit",text="shit",image=chromelogo2)#here you can also insert the unresized logo so you could see it as big as it is
treeview.insert('','1','Chrome3',text='Chrome3')
win.mainloop()
Simply converting "chromelogo" into a class variable by using the self keyword should fix the problem:
treeview=ttk.Treeview(frame3)
self.chromelogo=PhotoImage(file="./Images/minor-logo.png")
self.chromelogo=self.chromelogo.subsample(10,10)
treeview.pack(fill=BOTH,expand=True)
treeview.insert('','0','Chrome',text='Chrome', image=self.chromelogo)
treeview.insert('Chrome','0',"shit",text="shit",image=self.chromelogo)
treeview.insert('','1','Chrome3',text='Chrome3')

Ipython notebook dead kernel from image insert in Tkinter window using PIL

I have tried for several days to get an image displaying using the following code.
import Tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
path = "Aaron.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
The code is originally from this link.
How do I insert a JPEG image into a python Tkinter window?
When I run the code with my own path I get the following error messages
Python quit unexpectedly warning
and then:
The kernel appears to have died. It will restart automatically.
System info
Mac OS X Version 10.7.5
Anaconda
ipython-notebook version 3.2.1
Python 2.7.10-0
PIL 1.1.7
pillow 2.9.0
This is my first question on stackoverflow so please excuse any formatting errors. I will try to correct if there is an issue. I've also tried many version of code that is similar with the same result. This only happens with this specific code.

Python 2.7 How to add images on the canvas with Tkinter

I need to add an image to the canvas. I have tried countless amounts of things, and finally decided to make a question on here.
This is what I have imported
from Tkinter import *
import tkFont
from PIL import ImageTk, Image
And this is the line of code I'm trying to add to import an image from the same folder the main file is in.
c.create_image(100,100, anchor=N, image = ghost.jpg)
I've also tried putting ""s around 'ghost.jpg' and it says the Image does not exist then. Without the quotes it says "global name 'ghost' does not exist."
Can anyone help?
Canvas.create_image's image argument
should be a PhotoImage or BitmapImage, or a
compatible object (such as the PIL's PhotoImage). The application must
keep a reference to the image object.
from Tkinter import *
"""python 2.7 =Tkinter"""
from PIL import Image, ImageTk
app = Tk()
temp=Image.open("photo.jpg")
temp = temp.save("photo.ppm","ppm")
photo = PhotoImage(file = "photo.ppm")
imagepanel=Label(app,image = photo)
imagepanel.grid()
app.mainloop()
This is a bit of code I wrote in python 2.7 with Tkinter and PIL to import a jpg file from a given directory, this doesn't pop up off the screen.
You should replace photo with the file name (and directory) and app with the relevant variable you set.

Capturing x,y Coordinates with Python PIL

I want to display an image to the user with PIL and when the user clicks anywhere on this image, I want a def onmousedown(x,y) to be called. I will do some extra stuff in this function. How can I do this in PIL?
Thanks,
PIL won't do it alone -- PIL is an image manipulation library with no User Interfaces - it does have a showmethod, which does open an external program which displays the image, but does not communicate back with the Python process.
Therefore, in order to be able to get a user to interact with an image, one does have to build a GUI program using one of the consolidated toolkits for use with Python - the better known ones are Tkinter, GTK and Qt4. Tkinter is interesting because it comes pre-installed with Windows Python installs, and therefore is more easily available for users of that system. Windows users would have to separately download and install gtk or qt libraries to be able to use your program if you decide to use on of the other toolkits.
Here is a minimalist example of a Tkinter application with a clickable image:
import Tkinter
from PIL import Image, ImageTk
from sys import argv
window = Tkinter.Tk(className="bla")
image = Image.open(argv[1] if len(argv) >=2 else "bla2.png")
canvas = Tkinter.Canvas(window, width=image.size[0], height=image.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(image.size[0]//2, image.size[1]//2, image=image_tk)
def callback(event):
print "clicked at: ", event.x, event.y
canvas.bind("<Button-1>", callback)
Tkinter.mainloop()
Here is another related post
How to display picture and get mouse click coordinate on it
On Ubuntu to install
sudo apt-get install python python-tk idle python-pmw python-imaging python-imaging-tk
Then it all works.
I added a resize to #jsbueno's solution and fixed one import issue.
import Tkinter
from PIL import ImageDraw, Image, ImageTk
import sys
window = Tkinter.Tk(className="bla")
image = Image.open(sys.argv[1] if len(sys.argv) >=2 else "bla2.png")
image = image.resize((1000, 800), Image.ANTIALIAS)
canvas = Tkinter.Canvas(window, width=image.size[0], height=image.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(image.size[0]//2, image.size[1]//2, image=image_tk)
def callback(event):
print "clicked at: ", event.x, event.y
canvas.bind("<Button-1>", callback)
Tkinter.mainloop()

Categories