Python Forum
Using PIL to resize an image produces no image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using PIL to resize an image produces no image
#1
Question 
I am selecting a file using a tkinter file dialog. I open the file, resize it, create a PhotoImage and then update my canvas. I end up getting no image.

Can someone explain how I can open an image file, resize it, and update my canvas with the resized image?

My code:
from PIL import ImageTk, Image   # To handle JPG files
import tkinter as tk
from tkinter import filedialog as fd

new_image = None


def change_image():
    global new_image
    filetypes = (("PNG image", "*.png"),
                 ("JPG image", "*.jpg"),
                 ("All files", "*.*"),)
    file_picked = fd.askopenfilename(filetypes=filetypes)
    picked_image = Image.open(file_picked)
    resized_image = picked_image.resize((5, 5))
    new_image = ImageTk.PhotoImage(resized_image)
    my_canvas.itemconfigure(canvas_image, image=new_image)


# Start
interface = tk.Tk()

my_canvas = tk.Canvas(interface, width=700, height=700)
my_image = tk.PhotoImage()
open_button = tk.Button(interface, text="Open image", command=change_image)

canvas_image = my_canvas.create_image((0, 0), anchor="nw", image=my_image)
my_canvas.pack()
open_button.pack()

interface.mainloop()
Reply
#2
This works for me

import tkinter as tk
from PIL import ImageTk, Image
from tkinter import filedialog as fd

def change_image():
    filetypes = (
        ('PNG Image', '*.PNG *.png'),
        ('JPG Image', '*.JPG *.jpg'),
        ('All Files', '*.*')
    )

    file_picked = fd.askopenfilename(filetypes=filetypes)
    picked_image = Image.open(file_picked)
    
    resized_image = picked_image.resize((50,50))
    new_image = ImageTk.PhotoImage(resized_image)
    new_image.bak = new_image
    my_canvas.itemconfigure(img, image=new_image)


interface = tk.Tk()

my_canvas = tk.Canvas(interface, width=700, height=700, bg='white')
my_canvas.pack(expand=True, fill='both')
img = my_canvas.create_image(0,10, anchor='nw')

open_button = tk.Button(interface, text='Open Image', command=change_image)
open_button.pack(pady=8)

interface.mainloop()
Calab likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
Maybe the problem is you resize the image to be 5 x 5. When you change the size to something larger, that you can actually see. It works fine.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter weird image displaying behaviour pyDream 2 1,016 Oct-18-2025, 11:15 AM
Last Post: deanhystad
  Tkinter: An image and label are not appearing. emont 7 5,967 Jul-28-2025, 09:40 AM
Last Post: enablespindle
  [Tkinter] button image changing but not visually updating RuaridhW 2 2,078 Dec-16-2024, 03:18 PM
Last Post: RuaridhW
  Update Image on Button Click the_muffin_man 2 3,270 Nov-05-2024, 01:29 AM
Last Post: menator01
  [PyQt] Rotake rect, image or text at its center LavaCreeperKing 8 4,755 Oct-24-2024, 09:42 PM
Last Post: deanhystad
  Wont create Image from function the_muffin_man 10 4,933 Jul-10-2024, 06:20 PM
Last Post: AdamHensley
  I'm trying to visualize neuron weights with PIL but getting a white image. pointdexter16 1 1,923 May-31-2024, 07:51 AM
Last Post: wrestlingrisotto
  image conversion Skaperen 7 4,124 Sep-20-2023, 07:29 PM
Last Post: Skaperen
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 13,907 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] Load image and show in label ko_fal 8 10,242 Oct-25-2022, 09:20 AM
Last Post: ko_fal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020