-1
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) # save a reference of the image    

EDIT

Ok so i copied/followed the code exactly but when i run the images are not seen the in the text widget just white screen is visible

2 Answers2

0

First, you are skipping the first line. You might want to include it, like so:

with open("imagelink.txt") as url:
    for url2 in url.readlines():
        # do stuff

else :

with open("imagelink.txt") as url:
    line = url.readline()
    while line:
        # do stuff
        line = url.readline()

Then, you might need to check if your line is or has one or several url strings. You could use a regular expression for that.

import re
# Example of regex pattern matching urls. You can find more defined ones, if you need.
url_pattern = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"

with open("imagelink.txt") as url:
    line = url.readline()
    while line:
        for link in re.findall(url_pattern, line):
             u = urllib.request.urlopen(link)
             # do other stuff
        line = url.readline()

The second inner for loop insures you don't try to do anything with something that is not an url. If no url was found, the inner loop will break and pass to the next line.

Thanks to @acw1668 for the correction on url.read_line vs url.readlines.

Whole Brain
  • 2,097
  • 2
  • 8
  • 18
  • 1
    I think `for url2 in url.readline()` should be either `for url2 in url` or `for url2 in url.readlines()` instead. – acw1668 Jun 07 '21 at 08:52
  • Yes, my mistake. Thank you. – Whole Brain Jun 07 '21 at 09:04
  • @Whole Brain nope after following this nothing happened and also i think you did not get my problem? – Anime Nerdy Jun 07 '21 at 09:16
  • I answered the question highlighted in the title. If you still have an issue, then it is with the tkinter image part, which is a different question. – Whole Brain Jun 07 '21 at 09:20
  • @WholeBrain my problem here is that its not reading all the links present in the text file its just reading 1st line and giving the results – Anime Nerdy Jun 07 '21 at 09:33
  • That's the problem I just addressed. You should get all urls present in the text file, unless urls are splitted between several lines. – Whole Brain Jun 07 '21 at 09:37
0

Only the first image in the text widget is shown because you used same variable for all instances of PhotoImage(), so the previous loaded images will be garbage collected.

You need to save a reference for all the instances of image:

imagelist = []  # list to store references of image
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) # save a reference of the image

Note that the result of maintext.image_create(...) is not a tkinter widget, so you cannot call .pack() on it.

acw1668
  • 40,144
  • 5
  • 22
  • 34