Iterating over a list of images and assigning them as variables in python

I am trying to write a function that iterates over a list of images in python so that they are available for Pillow image processing.

ImTestA = Image.open("A.png")
ImTestB = Image.open("B.png")
ImTestC = Image.open("C.png")
ImTest1 = Image.open("1.png")
ImTest2 = Image.open("2.png")
ImTest3 = Image.open("3.png")
ImTest4 = Image.open("4.png")
ImTest5 = Image.open("5.png")
ImTest6 = Image.open("6.png")

I currently have the above, but I'm trying to reorganize it into something that I can assign to different lengths (use a list with AK or JZ).

from PIL import Image

AtoC = ["A.png", "B.png", "C.png"]

def OpenAns(quest):
    AtoCImages = []
    for image in quest:
        AtoCImages.append(Image.open(image))
        return AtoCImages

OpenAns(AtoC)

ImTestA = AtoCImages[0]
ImTestB = AtoCImages[1]
ImTestC = AtoCImages[2]

The bug with the previous error was fixed and returned to the same problem as before. I'm just trying to clear my code and make it nice and dry. Any help is appreciated.

Traceback (most recent call last):
  File "C:\ImageTest.py", line 13, in <module>
    ImTestA = AtoCImages[0]
NameError: name 'AtoCImages' is not defined

Perhaps I will only have a separate file with a list in each. If I can not cut it.

+4
source share
1 answer

Edit:

for image in AtoC:
    AtoCIm = []
    AtoCIm.append(Image.open(image))

to

AtoCIm = []
for image in AtoC:
    AtoCIm.append(Image.open(image))

will do it.

, . 1 . AtoCIm[1] .

+6

All Articles