如何通过Python中的函数创建具有特定名称的变量?

时间:2020-02-20 02:10:54

标签: python

我想创建一个函数,以使我更轻松地加载和缩放图像。我在一个文件夹中有很多图像,它们使用相同的路径进行访问,并且它们都具有与PNG文件相同的名称。我该如何创建一个函数,在其中可以输入文件名列表,并让Python使用所述名称创建变量,然后相应地缩放它们?

这是我现在拥有的代码:

Ocean = p.image.load("textures/tiles/Ocean.png")
Grassland = p.image.load("textures/tiles/Grassland.png")
Plains = p.image.load("textures/tiles/Plains.png")
FGrassland = p.image.load("textures/tiles/FGrassland.png")
FPlains = p.image.load("textures/tiles/FPlains.png")
HGrassland = p.image.load("textures/tiles/HGrassland.png")
HPlains = p.image.load("textures/tiles/HPlains.png")
MGrassland = p.image.load("textures/tiles/MGrassland.png")
MPlains =p.image.load("textures/tiles/MPlains.png")
Desert = p.image.load("textures/tiles/Desert.png")
HDesert = p.image.load("textures/tiles/HDesert.png")
Tundra = p.image.load("textures/tiles/Tundra.png")
FTundra = p.image.load("textures/tiles/FTundra.png")
HTundra = p.image.load("textures/tiles/HTundra.png")
MTundra = p.image.load("textures/tiles/MTundra.png")
Undiscovered = p.image.load("Undiscovered.png")
#########################################
Ocean = p.transform.scale(Ocean,(EFFECTIVE_SCALE,EFFECTIVE_SCALE))
Grassland = p.transform.scale(Grassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
FGrassland = p.transform.scale(FGrassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
HGrassland = p.transform.scale(HGrassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
MGrassland = p.transform.scale(MGrassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
Plains = p.transform.scale(Plains, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
Desert = p.transform.scale(Desert, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
Tundra = p.transform.scale(Tundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE ))
FTundra = p.transform.scale(FTundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE ))
Undiscovered = p.transform.scale(Undiscovered, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
HTundra = p.transform.scale(HTundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
MTundra = p.transform.scale(MTundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))

我想创建这样的东西:

def LoadTextures(self,textureList):
    for t in range textureList:
        texturepath = str("textures/tiles/" + textureList[t] + ".png")
        (create a new variable with the name of the png) = p.image.load("texturepath")
        (use same variable name as the above line) = p.transform.scale(textureList[t],(EFFECTIVE_SCALE,EFFECTIVE_SCALE))

有人知道我该怎么做吗?

0 个答案:

没有答案