Ok so I'm trying to get images to resize which I could do with Image.PhotoImage but now I'm using RGBA .png files so I have to use ImageTk.PhotoImage and I can't figure out how to resize them anymore any help on this kind of thing? I'm using python,tkinter

Recommended Answers

All 2 Replies

PIL will be your answer. Here is an example ...

# load, resize and save image with Python Imaging Library (PIL)
# optionally display images using Tkinter
# PIL from: http://www.pythonware.com/products/pil/
# or
# http://www.lfd.uci.edu/~gohlke/pythonlibs/

from PIL import Image

# pick an image file you have .bmp  .jpg  .gif.  .png
# (if not in the working directory, give full path)
file_in = 'Roses.jpg'
pil_image = Image.open(file_in)
# retrieve some information
print("image.size   = (%d, %d)" % pil_image.size)
print("image.format = %s" % pil_image.format)  # 'JPEG'
# common modes are 
# "L" (luminance) for greyscale images,
# "RGB" for true color images, 
# "CMYK" for pre-press images
print("image.mode   = %s" % pil_image.mode)    # 'RGB'

# change to a 200x100 size image using best downsize filter
image200x100 = pil_image.resize((200, 100), Image.ANTIALIAS)

# save the resized image as .jpg file to working directory
# (you could save it as .bmp, .gif, .png  too)
file_out = 'Roses200x100.jpg'
image200x100.save(file_out)

print("File saved as %s" % file_out)

# optional show image using Tkinter ...
# for Python3 use tkinter
import Tkinter as tk
from PIL import ImageTk

root = tk.Tk()
root.title(file_in)

# convert PIL image objects to Tkinter PhotoImage objects
tk_image1 = ImageTk.PhotoImage(pil_image)
tk_image2 = ImageTk.PhotoImage(image200x100)

# display the images on labels
label1 = tk.Label(root,image=tk_image1)
label1.pack(padx=5, pady=5)
label2 = tk.Label(root,image=tk_image2)
label2.pack(padx=5, pady=5)

root.mainloop()

Thanks Vegaseat I've got it working now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.