I am trying to load 12 pictures from my directory of images by making a grid.
when I use this code, I get the same picture in each button.
I need a different photo in each button. Kinda brand new to Python.... sorry!
import Tkinter as tk
import glob
from PIL import Image, ImageTk
root = tk.Tk()
for pl in glob.glob("C:/Users/Tony/Pictures/*.jpg"):
im = Image.open(pl)
im.thumbnail((180, 140), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(im)
for r in range(3):
for c in range(4):
tk.Button(root, image=photo).grid(row=r,column=c)
root.mainloop()
Thanks
Your loops aren't quite right, you probably end up with the last image on the all them? Heres what you are doing:
For each image
paste image in each grid point (repeat for next image)
I would just make an array of all the filenames:
pl = glob.glob("*.jpg")
i = 0
for r in range(3):
for c in range(4):
im = Image.open(pl[i])
i += 1
This lets you loop over your grid and use each photo only once
Related
I am working on a python messaging application using tkinter and i need a loading screen animation but none of them where fitting with my purpose so i Tried to rotate a image in python continusly which will look like a animation.
This is the image i want to ratate for some time in tkinter
The application code
i only need a simple tkinter window with the rotating screen
I tried google and i got this code to rote a image
from PIL import Image # Import Image class from the library.
image = Image.open("file.jpg") # Load the image.
rotated_image = image.rotate(180) # Rotate the image by 180 degrees.
rotated_image.save("file_rotated.jpg")
So i tried using this code like this:-
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
c = Canvas(root, width=700, height=700)
c.pack()
while True:
img = ImageTk.PhotoImage(Image.open(r"Loading_Icon.png"))
c.create_image(100, 100, image=img, anchor=NW)
image = Image.open(Loading_Icon.png") # Load the image.
rotated_image = image.rotate(30)
os.remove("Loading_Icon.png")
rotated_image.save("Loding_Icon.png")
root.mainloop()
c.delete("all")
Look at this:
from PIL import Image, ImageTk
import tkinter as tk
# Load the original image
pil_img = Image.open("pawn.black.png")
def loading_loop(i=0):
global tk_img
print(f"Loop {i}")
# If the prgram has loaded, stop the loop
if i == 13: # You can replace this with your loading condition
return
# Rotate the original image
rotated_pil_img = pil_img.rotate(30*i)
tk_img = ImageTk.PhotoImage(rotated_pil_img)
# put the rotated image inside the canvas
canvas.delete("all")
canvas.create_image(0, 0, image=tk_img, anchor="nw")
# Call `loading_loop(i+1)` after 200 milliseconds
root.after(200, loading_loop, i+1)
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
# start the tkinter loop
loading_loop()
root.mainloop()
It loads the original image then it start a tkinter loop which runs loading_loop every 200 milliseconds. Inside loading_loop, I rotate the image 30*i degrees, where i is the iteration number. Then I put the image inside the canvas.
Notice how I only call .mainloop() once and there is no while True loop. Those are best practises for when using tkinter.
It's best to create only 1 image inside the canvas and just reconfigure it but that will make the code longer.
I have been trying to display several images (in png) on tkinter. All of them have a transparent background, but for some reason jailer.png (the handcuffs) has been displaying with a black background (see screenshot), while every other image works as it should. Why ?
Here's my code (if create_a_label_with_an_image is redudant, it's because i had a lot of problem wth garbage disposal, if anyone knows the trick, pls tell me !) :
import tkinter as tk
from tkinter import ttk
from os import walk
from PIL import Image, ImageTk
class Gui:
def __init__(self):
self.frames={"main":tk.Tk()} #every frame will be added in this dictionnary
self.l_widgets={} #all widgets
def create_notebook(self, name, config_args={}, grid_args={}, frame="main"):
self.l_widgets[name] = (ttk.Notebook(self.frames[frame], **config_args),0)
self.l_widgets[name][0].grid(**grid_args)
def create_a_label_with_an_image(self, name, filename, size=(None, None), config_args={}, grid_args={}, frame="main"):
image_temp=Image.open(filename)
if size!=(None,None):
image_temp.thumbnail(size)
image=ImageTk.PhotoImage(image_temp)
self.l_widgets[name] = (ttk.Label(self.frames[frame]),image) #Image is stored at several places to hope it will not get erased
self.l_widgets[name][0].config(**config_args, image=image)
self.l_widgets[name][0].image=image
self.l_widgets[name][0].grid(**grid_args)
def add_to_notebook(self, name, name_frame, config_args={}, grid_args={}):
self.frames[name_frame]=ttk.Frame(self.frames["main"])
self.l_widgets[name][0].add(self.frames[name_frame], **config_args)
self.l_widgets[name][0].grid(**grid_args)
#END ####
g=Gui()
#here, i get all path to icons from this directory
mypath = "./Wolvesville Notebook/icons/characters"
filenames = next(walk(mypath), (None, None, []))[2] # [] if no file
if filenames==[]:
raise Warning("no icons found")
g.create_notebook("roles")
g.add_to_notebook("roles","all", config_args={"text":"all"})
nbr_images=0
for icon in filenames:
g.create_a_label_with_an_image(icon[:-4], mypath+"/"+icon,
size=(50,50),
grid_args={"row":nbr_images//12,
"column":nbr_images%12},
frame=str(pool))
nbr_images+=1
tk.mainloop()
Here's what the final result look like : Notebook, with one onglet named all, and 5 icons, all transparent but handcuffs
Here is the file that doesn't work : jailer.png.
Thank you in advance for the help !
Check the format of PNG file first, convert to "RGBA" if not.
>>> from PIL import Image
>>> im = Image.open('d:/jailer.png')
>>> im.mode
'LA'
>>> im.getpixel((0, 0))
(0, 0)
>>> new_im = im.convert(mode="RGBA")
>>> new_im.save("D:/jailer_new.png", format="PNG")
Simple code to show the differece, left one is your original copy.
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
im1 = Image.open("D:/jailer.png")
im2 = Image.open("D:/jailer_new.png")
pic1 = ImageTk.PhotoImage(im1)
pic2 = ImageTk.PhotoImage(im2)
label_1 = Label(root, image=pic1, bg='blue')
label_2 = Label(root, image=pic2, bg='blue')
label_1.pack(side='left')
label_2.pack(side='left')
root.mainloop()
AKX commented under my question to convert the grayscale + alpha into rgb + alpha, and it works ! I don't know why it was in grayscale, but it doesn't matter. I will close the subject, thanks !
PS : I can't seem to put a comment as the answer, so I wrote another one.
I try to put a image label on tkinter window, I add the functionality of grow and shrink the image and move the image on the main tkinter window
But as I click grow button, it increases size but got blurred and vice versa for shrink button
Here is my code, tell me where i am wrong?
from tkinter import*
from PIL import Image,ImageTk
from tkinter import Tk, Label, Button
from tkinter.filedialog import askopenfilename
import tkinter as tk
root = Tk()
root.title("image edit")
root.geometry('1000x600+500+100')
root.resizable(False,False)
#take image file from the system
Tk().withdraw()
filepath = askopenfilename()
img = Image.open(filepath)
tk_im = ImageTk.PhotoImage(img)
xi=100
yi=100
wi=100
hi=100
#function to increase size of label
def grow():
global img
global my_label
global xi
global yi
global wi
global hi
i=0
while i<2:
img = img.resize((xi, yi))
tk_im=ImageTk.PhotoImage(img)
my_label.configure(width=wi,
height=hi,image=tk_im)
my_label.image=tk_im
xi+=1
yi+=1
i+=1
wi+=1
hi+=1
#function to decrease size of image
def shrink():
global my_label
global img
global xi
global yi
global wi
global hi
i=0
while i<2:
img = img.resize((xi, yi))
tk_im=ImageTk.PhotoImage(img)
my_label.configure(width=wi,
height=hi,image=tk_im)
my_label.image=tk_im
xi-=1
yi-=1
i+=1
wi-=1
hi-=1
set image in label
my_label=Label(root,image=tk_im)
my_label.image=tk_im
my_label.pack()
buttons to resize image
grow_button=Button(root,text=
"grow",command=grow)
grow_button.pack()
shrink_button=Button(root,text=
"shrink",command=shrink)
shrink_button.pack()
root.mainloop()
your code is not exactly 'wrong' as people have different levels of quality they expect. if you want to be able to set your quality yourself however i would suggest resizing with Nearest Neighbour resampling so you don't introduce any new, blurred colours - just ones already existing in your image:
import numpy as np
from PIL import Image
im = Image.open("ImageTK").convert('RGB')
im = im.resize((200,200),resample=Image.NEAREST)
im.save("result.png")
You can go from a Pillow image to a numpy array with:
numpy_array = np.array(pillowImage)
and from numpy array to a Pillow image with:
pillow_image = Image.fromarray(numpyArray)
You could also use PIL.Image/openCV and PIL.ImageFilter modules:
from PIL import Image, ImageFilter
import cv2
image = cv2.resize(image, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
image = Image.fromarray(image)
Here, fx and fy are the values you have to set yourself.
You should not modify the original image because its quality may be changed especially after shrinking. Save the resized image into another image as below:
resized_img = img.resize((xi, yi)) # don't modify the original image
tk_im = ImageTk.PhotoImage(resized_img)
I am doing a battleship game and a function below that executes to create a new button with an explosion image as it's background. I am using Mac & python 3.7
global redraw_gameboard
global Player
global AI_player
script_dir = os.path.dirname(__file__)
rel_path = "explode.png"
image = ImageTk.PhotoImage(file=os.path.join(script_dir, rel_path))
new_button = Button(redraw_gameboard,
height = 2,
width = 4,
command= already_shot,
image=image)
new_button.grid(row = row, column = column)
This is what is coming out:
I am not sure what you expect, since I don't know what does the "explode.png" image look like. Also, when asking questions on stackoverflow, please always try to post a minimal reproducible example.
However, as I understand, the problem probably comes from the fact that the image is bigger than the button, and it is cropped. Then, only the upper left part of the image is displayed in your buttons.
Suggested solution:
(You will need to install the pillow package if it is not done yet)
import os
from PIL import Image, ImageTk
import tkinter
# Sizes in pixels
BUTTON_HEIGHT = 40
BUTTON_WIDTH = 40
root = tkinter.Tk()
script_dir = os.path.dirname(__file__)
rel_path = "explode.png"
image = Image.open(os.path.join(script_dir, rel_path))
image = image.resize((BUTTON_WIDTH,BUTTON_HEIGHT))
imtk = ImageTk.PhotoImage(image)
# Using a void image for other buttons so that the size is given in pixels too
void_imtk = tkinter.PhotoImage(width=BUTTON_WIDTH, height=BUTTON_HEIGHT)
def create_button(row, column, im):
new_button = tkinter.Button(root,
height = BUTTON_HEIGHT,
width = BUTTON_WIDTH,
image=im)
new_button.grid(row = row, column = column)
create_button(0,0, imtk)
create_button(0,1, void_imtk)
create_button(1,0, void_imtk)
create_button(1,1, imtk)
root.mainloop()
Of course, you will want to make some changes for your program, e.g. using your widget architecture.
I'm currently using PIL to display images in Tkinter. I'd like to temporarily resize these images so that they can be viewed more easily. How can I go about this?
Snippet:
self.pw.pic = ImageTk.PhotoImage(Image.open(self.pic_file))
self.pw.pic_label = TK.Label(self.pw , image=self.pw.pic,borderwidth=0)
self.pw.pic_label.grid(column=0,row=0)
Here's what I do and it works pretty well...
image = Image.open(Image_Location)
image = image.resize((250, 250), Image.ANTIALIAS) ## The (250, 250) is (height, width)
self.pw.pic = ImageTk.PhotoImage(image)
There you go :)
EDIT:
Here is my import statement:
from Tkinter import *
import tkFont
from PIL import Image
And here is the complete working code I adapted this example from:
im_temp = Image.open(Image_Location)
im_temp = im_temp.resize((250, 250), Image.ANTIALIAS)
im_temp.save("ArtWrk.ppm", "ppm") ## The only reason I included this was to convert
## The image into a format that Tkinter woulden't complain about
self.photo = PhotoImage(file="ArtWrk.ppm") ## Open the image as a tkinter.PhotoImage class()
self.Artwork.destroy() ## Erase the last drawn picture (in the program the picture I used was changing)
self.Artwork = Label(self.frame, image=self.photo) ## Sets the image too the label
self.Artwork.photo = self.photo ## Make the image actually display (If I don't include this it won't display an image)
self.Artwork.pack() ## Repack the image
And here are the PhotoImage class docs: http://www.pythonware.com/library/tkinter/introduction/photoimage.htm
Note...
After checking the pythonware documentation on ImageTK's PhotoImage class (Which is very sparse) I appears that if your snippet works than this should as well as long as you import the PIL "Image" Library an the PIL "ImageTK" Library and that both PIL and tkinter are up-to-date. On another side-note I can't even find the "ImageTK" module life for the life of me. Could you post your imports?
if you don't want save it you can try it:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
same = True
#n can't be zero, recommend 0.25-4
n=2
path = "../img/Stalin.jpeg"
image = Image.open(path)
[imageSizeWidth, imageSizeHeight] = image.size
newImageSizeWidth = int(imageSizeWidth*n)
if same:
newImageSizeHeight = int(imageSizeHeight*n)
else:
newImageSizeHeight = int(imageSizeHeight/n)
image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS)
img = ImageTk.PhotoImage(image)
Canvas1 = Canvas(root)
Canvas1.create_image(newImageSizeWidth/2,newImageSizeHeight/2,image = img)
Canvas1.config(bg="blue",width = newImageSizeWidth, height = newImageSizeHeight)
Canvas1.pack(side=LEFT,expand=True,fill=BOTH)
root.mainloop()
the easiest might be to create a new image based on the original, then swap out the original with the larger copy. For that, a tk image has a copy method which lets you zoom or subsample the original image when making the copy. Unfortunately it only lets you zoom/subsample in factors of 2.