Python3 tkinter set image size

Viewed 33359

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

4 Answers

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

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
Related