0
imagelist=[]
with open("imagelink.txt") as url:
for url2 in url:
    if url2.strip():
        raw_data= urllib.request.urlopen(url2.strip()).read()
        im = ImageTk.PhotoImage(data=raw_data)
        result = maintext.image_create(0.0, image=im)
        imagelist.append(im)

So this is my code i am opening a image URL and displaying the image in a text box,so how do I resize the image from the URL I tried using resize function but it says photo image doesn't have resize function.So how do I resize the URL image?

  • (1) Download the data from the URL (2) Load the data into a Pillow Image object (not an ImageTk.PhotoImage) (3) Resize the Pillow image (4) Make a ImageTk.PhotoImage out of the Pillow image. – AKX Jun 08 '21 at 09:04
  • [This answer](https://stackoverflow.com/a/38241889/13145954) may help, and explains how to manipulate a PhotoImage directly. – Seon Jun 08 '21 at 09:04
  • 1
    @Seon when i follwed that i get an error AttributeError: 'PhotoImage' object has no attribute 'zoom' – Anime Nerdy Jun 08 '21 at 09:14
  • Write the data to a file and load the data with `PIL.Image()` then manipulate, then load for tkinter – Delrius Euphoria Jun 08 '21 at 09:32
  • @CoolCloud There is no other way where i can directly resize?if you have please write the code so that i can easily get it – Anime Nerdy Jun 08 '21 at 09:35

1 Answers1

0

You can use PIL.Image.open() and io.BytesIO() to open and resize the retrieved image:

import io
from PIL import Image, ImageTk
...
imagelist=[]
with open("imagelink.txt") as url:
    for url2 in url:
        url2 = url2.strip()
        if url2:
            raw_data = urllib.request.urlopen(url2).read()
            # open the image and resize
            im = Image.open(io.BytesIO(raw_data)).resize((300,100))
            im = ImageTk.PhotoImage(im)
            #im = ImageTk.PhotoImage(data=raw_data)
            result = maintext.image_create(0.0, image=im)
            imagelist.append(im)
acw1668
  • 40,144
  • 5
  • 22
  • 34