1

I would like to resize an image using TKinter. Please note that I will not be using PIL for this.

How can I currently have this image, which works fine.

logo = PhotoImage(file="logo_dribbble-01_1x.PNG")
label = Label(f1,image=logo, borderwidth=0, highlightthickness=0)
label.pack()

logo_dribbble-01_1x.PNG

I would like to resize this image so that the logo looks smaller.

I tried doing this, which was suggested here

smallLogo = PhotoImage(file="logo_dribbble-01_1x.PNG")
smallLogo = smallLogo.subsample(2, 2)
smallLabel = Label(f1,image=smallLogo, borderwidth=0, highlightthickness=0)
smallLabel.pack()

But this creates an empty label without displaying the image.

I tried to resize the image using Photoshop and use that image and then use that .png image to display the smaller image like so:

logo = PhotoImage(file="logo_dribbble-01_1xsmall.PNG")
smallLabel = Label(f1,image=smallLogo, borderwidth=0, highlightthickness=0)
smallLabel.pack()

logo_dribbble-01_1xsmall.PNG

But, I get this error when I try to run the code _tkinter.TclError: encountered an unsupported criticial chunk type "mkBF"

How can I resolve this issue?

Nae
  • 14,209
  • 7
  • 52
  • 79
jim bob
  • 53
  • 6
  • always put full error message (Traceback) in question (as text, not screenshot). There are other useful informations. – furas Jan 19 '18 at 11:05
  • BTW: tkinter doesn't support PNG. To display PNG you have to use `Image`, `ImageTk` from `PIL/pillow` – furas Jan 19 '18 at 11:07
  • 2
    In some platforms, Python does not support tk v8.6, so it won't be a portable solution. For instance, in macOS, we can only use tk v8.5, wich does not support PNG. We would need to either convert the image to GIF, or to use an external library, like Pillow. – Victor Domingos Jan 19 '18 at 11:25
  • @furas the first part of code worked, and I used the PNG format, I will change the error to the full Traceback soon. – jim bob Jan 19 '18 at 11:28
  • @Victor Domingos I will try using GIF format, and I will update if that worked or not soon. Thanks – jim bob Jan 19 '18 at 11:29
  • @furas Are you certain that tkinter not supporting .png? I have a working example with png and only tkinter is imported. – Nae Jan 19 '18 at 15:37
  • 1
    @Nae maybe something changed in the newest `TCL/TK` or `tkinter` but older didn't work with PNG and you have information on page [PhotoImage](http://effbot.org/tkinterbook/photoimage.htm). Or maybe currently tkinter use PIL/pillow in background. – furas Jan 19 '18 at 15:42
  • 1
    @Nae - I found info for Tk 8.6: [Built-in PNG Image Support](http://www.tcl.tk/software/tcltk/8.6.html) – furas Jan 19 '18 at 15:46

3 Answers3

0

The following code worked for me:

from tkinter import *

f1 = Tk()
smallLogo = PhotoImage(file="image.PNG")
smallLogo = smallLogo.subsample(2, 2)
smallLabel = Label(f1,image=smallLogo, borderwidth=0, highlightthickness=0)
smallLabel.pack()
f1.mainloop()

Note I'm on tk-8.6

pyThink
  • 159
  • 5
  • In some platforms, Python does not support tk v8.6, so it won't be a portable solution. For instance in macOS, we can only use tk v8.5, wich does not support PNG. We would need to either convert the image to GIF, or to use an external library, like Pillow. – Victor Domingos Jan 19 '18 at 11:24
0

I had to keep a reference of the image that was being used by the label like so:

logo = PhotoImage(file="image.png")
logo = logo.subsample(2, 2)
label = Label(root,image=logo, borderwidth=0, highlightthickness=0)
label.image = logo
label.pack()
jim bob
  • 53
  • 6
0
smallLogo = PhotoImage(file="logo_dribbble-01_1x.PNG")
smallLogo_one = smallLogo.subsample(2, 2)
smallLabel = Label(f1,image=smallLogo_one, borderwidth=0, highlightthickness=0)
smallLabel.pack()

I think this will solve the problem for you.Your variable for PhotoImage is the same as variable for the Subsample to trim the image for you. I change the variable for subsample to smallLogo_one the parsed it to the image attribute in the Lable.

AD WAN
  • 1,414
  • 2
  • 15
  • 28