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).
Related
I have a tk.Label object with an image (via tk.Label(self.root,image='image.png')) and want to add a color filter to it, i.e. make the image more red. How can I do that?
You can modify the image with PIL, and save it as a new file. In Tkinter, you can use PhotoImage(file="file.png") to use the image as a button:
# importing image object from PIL
from PIL import ImageOps, Image
import PIL.Image
# creating an image object
img = Image.open(r"test.png").convert("L")
# image colorize function
img = ImageOps.colorize(img, black="red", white="white")
# save the image as new.png
img.save("new.png")
from tkinter import *
root = Tk()
# set the img1 variable as the image we just created
img1 = PhotoImage(file="new.png")
b = Button(image=img1)
b.pack()
root.mainloop()
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()
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 missing something simple here... I'm trying to insert an image in a canvas,
photoconn = Image.open(giffname)
photoconn = photoconn.resize((w0*self.resize/100,h*self.resize/100), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(photoconn)
pc.append(photoImg)
self.cv.create_image(10, 10, anchor=NW, image=photoImg)
and I get the dreaded _tkinter.TclError: image "pyimage39" doesn't exist
Yet I memorize the image in the global pc array, and the debugger shows that the variables seem ok, with photoconn being a <PIL.GifImagePlugin.GifImageFile 0x260fab8; len=28>, and photoImg being a <PIL.ImageTk.PhotoImage 0x260fb48; len=12>.
... what am I missing?
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.