8

I have looked everywhere to find a way to set the size of an image. The image is set to a url. I have found other questions on the site but none of them have worked.

import urllib.request, base64

u = urllib.request.urlopen(currentWeatherIconURL)
raw_data = u.read()
u.close()

b64_data = base64.encodestring(raw_data)
image = PhotoImage(data=b64_data)

label = Label(image=image, bg="White")
label.pack()

That is the code that creates the image, how would I set the size of the image

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
SeanOMik
  • 235
  • 2
  • 3
  • 6
  • If you're looking to literally resize in-place, you probably should use PIL or another library. See [this post](https://stackoverflow.com/questions/28464331/resizing-images-with-imagetk-photoimage-with-tkinter-python) – Ron Norris Jul 07 '17 at 18:33

4 Answers4

12

As mentioned by several others, you should use PIL to resize your image before attaching it to a tkinter label:

from tkinter import Tk, Label
from PIL import Image, ImageTk

root = Tk()

img = ImageTk.PhotoImage(Image.open('img-path.png').resize(pixels_x, pixels_y)) # the one-liner I used in my app
label = Label(root, image=img, ...)
label.image = img # this feels redundant but the image didn't show up without it in my app
label.pack()

root.mainloop()
Nelson
  • 922
  • 1
  • 9
  • 23
  • looks like the problem is rather to use PIL with raw_data from request – PRMoureu Jul 07 '17 at 19:29
  • 1
    You can use [this method](https://stackoverflow.com/a/26079673/5673922) to replace the `Image.open(...)` call – Nelson Jul 07 '17 at 19:37
  • cStringIO doesn't exist, I tried the io.StringIO but get the error: TypeError: initial_value must be str or None, not bytes. Here's the line of code: image = ImageTk.PhotoImage(Image.open(io.StringIO(data=b64_data)).resize(currentWeatherIconWidth, currentWeatherIconHeight)) – SeanOMik Jul 08 '17 at 05:40
  • @SeanOMik I know little to nothing about images and base64 and how all of that works, but I would try to put `raw_data` into the StringIO call instead of the encoded data. It said it wanted a str object, so I'm assuming that's what the `raw_data` is before it's encoded – Nelson Jul 08 '17 at 09:54
  • @SeanOMik While working on something entirely different, I found a good example of what I mentioned before: `image = Image.open(StringIO(urllib2.urlopen(image_url).read()))`, showing the use of the raw data from urlopen with StringIO – Nelson Jul 09 '17 at 04:02
  • @Nelson, no it still gives me the same error, thanks anyway – SeanOMik Jul 11 '17 at 04:10
  • Thank you so much for the `label.image = img` comment! – user1543042 May 13 '22 at 01:46
8

New syntax of resize:

resize((pixels_x, pixels_y))

So the code can be comething like this:

from tkinter import Tk, Label
from PIL import Image, ImageTk

root = Tk()

file = '/home/master/Work/Tensorflow/Project/Data/images/p001.png'

image = Image.open(file)

zoom = 1.8

#multiple image size by zoom
pixels_x, pixels_y = tuple([int(zoom * 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()

based on Nelson answer

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
5

You can add this one line if a simple zoom is acceptable :

image = PhotoImage(data=b64_data)
image = image.subsample(4, 4) # divide by 4
# image = image.zoom(2, 2)    # zoom x 2
label = Label(image=image, bg="White")

Otherwise you should use the PIL lib which provide more accurate tools.

PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • This wouldn't work. I need to set it the an exact measurement of pixels. How would I use the PIL lib with my code? – SeanOMik Jul 07 '17 at 18:43
  • so this will be PIL : maybe [this post can help you](https://stackoverflow.com/questions/3397157/how-to-read-a-raw-image-using-pil) – PRMoureu Jul 07 '17 at 18:45
  • This is the answer that WORKS, thank you very much. – Weilory May 30 '20 at 07:29
0
try:
    # Relative Path
    img = Image.open(File)
    width, height = img.size
    print(width)
    print(height)

    img = img.resize((round(680/height*width) , round(680)))

    # Saved in the same relative location
    # img.save("resized_picture.jpg")

    img = ImageTk.PhotoImage(img)
    print(img.height()) 
    print(img.width())
except IOError:
    pass