i got this code and i get this error:
AttributeError: 'PhotoImage' object has no attribute 'resize'
I have also tried rgrapg = Image.open("risinggrap.jpg") and i get this error: _tkinter.TclError: image "" doesn't exist
rgraph = ImageTk.PhotoImage(Image.open("risinggrap.jpg"))
rgraph = rgraph.resize((200,250),Image.ANTIALIAS)
photoLabe = Label(x, image=rgraph)```
You need to load the image first, resize it, then cast it into ImageTk.PhotoImage. Here is a working example that goes like so:
import tkinter as tk
from PIL import Image, ImageTk
x = tk.Tk()
# 1. load image
image = Image.open("risinggrap.jpg")
# 2. resize it
image = image.resize((200, 250), Image.ANTIALIAS)
# 3. cast it into ImageTk.PhotoImage
rgraph = ImageTk.PhotoImage(image)
photoLabel = tk.Label(x, image = rgraph)
photoLabel.pack(side = "bottom", fill = "both", expand = "yes")
x.mainloop()
Related
I know that in CSS you can set the maximum size of an image by using max-width and max-height. I want to do the same thing with tkinter. I've already tried using Image.open("/path/to/file").resize(500), but I got the error TypeError: 'int' object is not iterable. Here is my code:
from tkinter import *
from PIL import Image, ImageTk
root=Tk()
current_image=0
images=[ImageTk.PhotoImage(Image.open("/users/27cadem/documents/display.png").resize(500))]
panel=Label(root,image=images[current_image])
panel.pack()
root.mainloop()
The code can be something like this:
from tkinter import Tk, Label
from PIL import Image, ImageTk
root = Tk()
file = 'img-path.png'
image = Image.open(file)
max_width = 500
pixels_x, pixels_y = tuple([int(max_width/image.size[0] * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((pixels_x, pixels_y)))
label = Label(root, image=img)
label.image = img
label.pack()
root.mainloop()
I'd like to know if I did something wrong in my code or if I have to update something cause every single time I try to resize an image I have this: Attribute Error 'PhotoImage' object has no attribute 'resize'.
import tkinter as tk
from PIL import Image, ImageTk
image1 = Image.open("enseigne.JPEG")
image1= ImageTk.PhotoImage(image1)
imageresize = image1.thumbnail((500,500), Image.ANTIALIAS)
imageresize = ImageTk.PhotoImage(imageresize)
limage = tk.Label(app, image=imageresize)
The error means what it says, PhotoImage object does not have resize attribute, then what has? It is the Image object that has resize. You can remove your first PhotoImage(because it uses resize), as its no use:
image1 = Image.open("enseigne.JPEG")
imageresize = image1.thumbnail((500,500), Image.ANTIALIAS)
imageresize = ImageTk.PhotoImage(imageresize)
limage = tk.Label(app, image=imageresize)
If you want shorter code:
image1 = ImageTk.PhotoImage(Image.open("enseigne.JPEG").thumbnail((500,500), Image.ANTIALIAS))
limage = tk.Label(app, image=image1)
Obvious question: You are not calling resize but why are you getting such error? It is because thumbnail uses resize implicitly(based on conditions).
Hello I am having issues with resizing my picture. I am trying to resize the image to fit the blue drawing. However the way I am doing it, returns an error.
File "gui.py", line 42, in fileDialog
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
AttributeError: 'PhotoImage' object has no attribute 'resize
I am just testing it to see if it fits by doing 800,600 I really don't know.
def fileDialog(self):
self.filename = filedialog.askopenfilename(title="Select")
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column=1, row=2)
self.label.configure(text=self.filename)
self.photo= ImageTk.PhotoImage(file = self.filename)
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
self.display.grid(row=0)
Is there something that I am doing incorrectly? Please advise.
You need to resize the image, not the photoimage.
import tkinter as tk
from PIL import Image, ImageTk
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root = tk.Tk()
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage = tk.Label(root, image=root.photoimg)
labelimage.pack()
To address the new question, you do not have to know the filename at the time of label creation. The following code produces the same result:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
labelimage = tk.Label(root)
labelimage.pack()
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage.configure(image=root.photoimg)
I'm trying to place a .png image within a LabelFrame in a Tkinter window. I imported PIL so .png image types should be supported (right?). I can't seem to get the image to show up.
Here is my revised code:
import Tkinter
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
make_frame = LabelFrame(root, text="Sample Image", width=150, height=150)
make_frame.pack()
stim = "image.png"
width = 100
height = 100
stim1 = stim.resize((width, height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(image.open(stim1))
in_frame = Label(make_frame, image = img)
in_frame.pack()
root.mainloop()
With this code, I got an AttributeError that reads: "'str' has no attribute 'resize'"
#Mickey,
You have to call the .resize method on the PIL.Image object and not the filename, which is a string. Also, you may prefer to use PIL.Image.thumbnail instead of PIL.Image.resize, for reasons described clearly here. Your code was close, but this might be what you need:
import Tkinter
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
make_frame = LabelFrame(root, text="Sample Image", width=100, height=100)
make_frame.pack()
stim_filename = "image.png"
# create the PIL image object:
PIL_image = Image.open(stim_filename)
width = 100
height = 100
# You may prefer to use Image.thumbnail instead
# Set use_resize to False to use Image.thumbnail
use_resize = True
if use_resize:
# Image.resize returns a new PIL.Image of the specified size
PIL_image_small = PIL_image.resize((width,height), Image.ANTIALIAS)
else:
# Image.thumbnail converts the image to a thumbnail, in place
PIL_image_small = PIL_image
PIL_image_small.thumbnail((width,height), Image.ANTIALIAS)
# now create the ImageTk PhotoImage:
img = ImageTk.PhotoImage(PIL_image_small)
in_frame = Label(make_frame, image = img)
in_frame.pack()
root.mainloop()
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.