Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在for循环中使用PIL的image.paste时会出现图像重叠问题?_Python_Python Imaging Library - Fatal编程技术网

Python 为什么在for循环中使用PIL的image.paste时会出现图像重叠问题?

Python 为什么在for循环中使用PIL的image.paste时会出现图像重叠问题?,python,python-imaging-library,Python,Python Imaging Library,我试图从一组形状或属性中生成50个随机轮廓图像。有不同颜色背景的分组,身体的矩形占位符,圆形头部,椭圆形手臂,然后是一组数字。除了纯色背景图像外,每个图像都有一个透明的背景。所有图像均为PNG 目前,我正在运行一个for循环,生成一个随机数,并使用该数来运行检索属性图像随机组合的函数。然后我使用Image.paste链将随机属性图像集放在一起并保存到外部文件夹 问题是,一些生成的图像共享来自先前生成的图像的重叠属性。几乎就像没有重新分配变量一样。这里有一个例子- 以下是我正在使用的代码: bod

我试图从一组形状或属性中生成50个随机轮廓图像。有不同颜色背景的分组,身体的矩形占位符,圆形头部,椭圆形手臂,然后是一组数字。除了纯色背景图像外,每个图像都有一个透明的背景。所有图像均为PNG

目前,我正在运行一个for循环,生成一个随机数,并使用该数来运行检索属性图像随机组合的函数。然后我使用Image.paste链将随机属性图像集放在一起并保存到外部文件夹

问题是,一些生成的图像共享来自先前生成的图像的重叠属性。几乎就像没有重新分配变量一样。这里有一个例子-

以下是我正在使用的代码:

body1 = Image.open("test-images/body1.png")

# example of array of images
bodyArr = (body1, body2, body3, body4, body5, body6, body7, body8)

# example of function to select a position in above array
def getBody(num):
    result = int(num[2:4])

    if result < 12:
        return 0
    elif result >= 12 and result < 24:
        return 1
    elif result >= 24 and result < 36:
        return 2
    elif result >= 36 and result < 48:
        return 3
    elif result >= 48 and result < 60:
        return 4
    elif result >= 60 and result < 72:
        return 5
    elif result >= 72 and result < 84:
        return 6
    else:
        return 7

for x in range(0, 50):

    # generate random, 10 digit number
    randomStr = str(randint(100000000000, 999999999999))[1:11]

    # assigning images 
    backgroundImage = bgArr[getBg(randomStr)]
    bodyImage = bodyArr[getBody(randomStr)]
    headImage = headArr[getHead(randomStr)]
    armsImage = armsArr[getArms(randomStr)]
    numImage = numArr[getNum(randomStr)]

    backgroundImage.paste(bodyImage, (0,0), bodyImage)
    backgroundImage.paste(headImage, (0,0), headImage)
    backgroundImage.paste(armsImage, (0,0), armsImage)
    backgroundImage.paste(numImage, (0,0), numImage)

    imgname = f'{dirname}/profile_images/profile{x}.png'
    backgroundImage.save(imgname)
你知道这是什么原因吗?我尝试过使用大量Image.show进行调试,以查看哪里出了问题,但是在方法中设置title参数在预览中不起作用,而且很难获得完整的时间线


谢谢你的帮助

在for循环中,您正在从bgArr[getBgrandomStr]获取背景图像的浅拷贝。这意味着,您使用的对象与以前可能修改过的对象相同

你能试试看吗

从python中,我们可以看到

Python中的赋值语句不复制对象,而是在目标和对象之间创建绑定。对于可变的或包含可变项的集合,有时需要一个副本,以便可以在不更改另一个副本的情况下更改一个副本

深度副本构造一个新的复合对象,然后递归地将在原始副本中找到的对象的副本插入其中

import copy

# ...

for x in range(0, 50):
    #...
    # assigning images 
    backgroundImage = copy.deepcopy(bgArr[getBg(randomStr)])