select an image to show with dialog [duplicate] - python

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

Related

Unable to put transparent PNG over a normal image Python Tkinter

So I have 2 images, 1 image is supposed to be the background and the other 1 is just an image of a character.
I am able to put one image over the other but the image has white borders even though it's a PNG file.
This is how it looks like:
This is how I want it to look like:
Here are the two separte images:
https://imgur.com/a/SmE5lgC
Sorry that I didnt post the images directly but I can not since I do no have 10 reputation points.
I've tried converting it to RGBA but same thing happened.
from tkinter import *
from PIL import Image
root = Tk()
root.title("Game")
background = PhotoImage(file="back.png")
backgroundlabel = Label(root, image=background)
backgroundlabel.pack()
character = PhotoImage(file="hero.png")
characterlabel = Label(root, image=character)
characterlabel.place(x=0,y=0)
root.mainloop()
You just need to use the canvas widget in Tkinter. Only the canvas widget supports transparency. This has to do with how Tkinter draws the display. As of right now, your code is just overlaying two images. Tkinter does not know how to compose them with transparency unless you use the canvas widget.
See the following code:
from tkinter import *
from PIL import Image
root = Tk()
root.title("Game")
frame = Frame(root)
frame.pack()
canvas = Canvas(frame, bg="black", width=700, height=400)
canvas.pack()
background = PhotoImage(file="background.png")
canvas.create_image(350,200,image=background)
character = PhotoImage(file="hero.png")
canvas.create_image(30,30,image=character)
root.mainloop()
All I did was download the images you provided. I did not modify the images. So, the bottom line is that you just need to use the canvas widget.
VoilĂ !
Note: The question asked is a duplicate of How do I make Tkinter support PNG transparency?

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

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

Image not showing in Tkinter Window [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 4 years ago.
I am trying to display an image in a window. I tried it two methods, using classes and single snippet.
I am confused why this is showing correct output:
from Tkinter import *
from PIL import ImageTk, Image
root = Tk()
picture="path/image.jpg"
image = Image.open(picture).resize((350, 350), Image.ANTIALIAS)
print(image)
pic = ImageTk.PhotoImage(image)
panel = Label(root, image = pic)
panel.grid(sticky="news")
root.mainloop()
but not the below one?
from Tkinter import *
from PIL import ImageTk, Image
class DisplayImage():
def __init__(self, root):
self.root = root
def stoneImg(self, picture="path/default_image.png"):
image = Image.open(picture).resize((350, 350), Image.ANTIALIAS)
pic = ImageTk.PhotoImage(image)
panel = Label(self.root, image=pic)
panel.grid(sticky="news")
if __name__ == '__main__':
root = Tk()
DisplayImage(root).stoneImg()
root.mainloop()
The difference is that in your second example, the picture was referred to only by a local variable, which went away at the end of the function. Garbage collection works a bit weirdly in Tkinter, since all of the GUI-related objects exist in the embedded Tcl interpreter, outside of Python's control.
The simple solution is to add a line like panel.image = pic, so that a reference to the image exists for as long as the widget itself does.

Resize pgm image using Tkinter only [duplicate]

This question already has answers here:
Image resize under PhotoImage
(5 answers)
Closed 6 years ago.
I have an image that I display and I would like to resize (enlarge, actually) that image. Here's my code, using other SO questions, but I get no result - my image still has the same size. Resizing the button also does not change the image size.
I've tried the answers from this SO question: Image resize under PhotoImage but they won't work.
from Tkinter import *
root = Tk()
root.withdraw()
def cleanUp():
root.destroy()
def openWebsite():
print 'Will try to implement opening the website here.'
window = Toplevel(root)
window.protocol('WM_DELETE_WINDOW', cleanUp)
photo = PhotoImage(file="Header.pgm")
photo.zoom(2)
button = Button(window, image=photo, command=openWebsite)
button.pack()
root.mainloop()
PhotoImage.zoom() returns a new image, it does not modify the original image. Try rebinding photo like this:
photo = photo.zoom(2)
From the help:
zoom(self, x, y='') method of Tkinter.PhotoImage instance
Return a new PhotoImage with the same image as this widget
but zoom it with X and Y.

displaying the selected image using tkinter

I want to know whether I can display an image from the path I have selected? like, I have a path for example: c:\user\desktop\33.jpg, and I want to take only that jpg file and I have to display that image using label or something. If it is possible, I want to know how?
Thanks in advance!
Here is a sample code for what you are asking:
from Tkinter import Label,Tk
from PIL import Image, ImageTk
import tkFileDialog
root = Tk()
path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
im = Image.open(path)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(root,image = tkimage)
myvar.image = tkimage
myvar.pack()
root.mainloop()
You will be wanting to add a button for calling the askopenfilename because right now its calling it the moment the program begins.
Also you might wanna add more file extensions to filetypes

Categories