尝试创建具有随机坐标的合成图像(使用 PIL)并一次保存多个文件

Trying to create a composite image with random coordinates (using PIL) and saving multiple files at once

我正在尝试调整此页面中的代码,目的略有不同:

Mapping an image with random coordinates, using PIL, without them stay one on top of the other

我在背面使用黑色背景和 2 张透明图像(一个白色 X 和一个橙色圆点)background.The 结果是全黑 background.I 想要白色 X 十字后面的橙色点,但不幸的是什么都看不见。

https://i.ibb.co/WfZ4NhQ/background2.png

https://i.ibb.co/Lvm3CbD/orangedot.png

https://i.ibb.co/23mXjqm/whiteX.png

https://i.ibb.co/FYMg8QL/test.png

from PIL import Image
import random


#whiteX = 200x200
#orangedot = 30x30

background2 = Image.open('background2.png')
whiteX = Image.open('whiteX.png')
orangedot = Image.open('orangedot.png')

positionxorangedot = random.randint(30, 170)
positionyorangedot = random.randint(0, 100)


background2.paste(whiteX)
background2.paste(orangedot, (positionxorangedot, positionyorangedot), orangedot)


background2.save("test.png")

background2.show()

作为最后的操作,我想以 jpg 或类似格式保存(例如 500 次)输出文件。我尝试了各种解决方案,但无法编写任何真正实用的东西...... 任何帮助真的很感激!你建议我如何进行?

您的问题与 to one titled 中的问题非常相似,但有一些(相对较小的)差异:

  1. 需要先将背景图像转换为“RGBA”模式(因为它是调色板、“P”模式图像)
  2. 正在合成多张图片。

这里是结果(解释见链接答案):

from PIL import Image
import random


#bckgnd = 200x200
#whiteX = 200x200
#orangedot = 30x30

bckgnd = Image.open('background2.png').convert('RGBA')  # Convert to mode supporting alpha.

whiteX = Image.open('whiteX.png')
tmp_img = Image.new('RGBA', bckgnd.size, color=(0,0,0,0))
tmp_img.paste(whiteX)  # Assumes it's the same size as background image.
bckgnd.alpha_composite(tmp_img)

orangedot = Image.open('orangedot.png')
orangedot_x, orangedot_y = random.randint(30, 170), random.randint(0, 100)
tmp_img = Image.new('RGBA', bckgnd.size, color=(0,0,0,0))
tmp_img.paste(orangedot, (orangedot_x, orangedot_y))
bckgnd.alpha_composite(tmp_img)

bckgnd.save("test.png")
bckgnd.show()

生成的示例 test.png 文件:

更新

由于上面包含很多重复的代码,原则上应该尽量保留一些东西DRY,这里有一个替代实现,它定义了一个函数来完成图像合成工作。

from PIL import Image
import random


def alpha_composite_paste(img1, img2, box=None):
    ''' Alpha composite paste one image over another. Both `img1` and `img2` can
        be either an image object or the file path of an image file. The `box`
        argument is either a 2-tuple giving the upper left corner, a 4-tuple
        defining the left, upper, right, and lower pixel coordinate, or None
        (same as (0, 0)). Both images must be RGBA mode.

        For convenience, returns modified img1.
    '''
    if isinstance(img1, str):  # Image file path?
        img1 = Image.open(img1)
    if isinstance(img2, str):  # Image file path?
        img2 = Image.open(img2)
    # Make fully transparent image the same size as img1.
    tmp_img = Image.new('RGBA', img1.size, color=(0,0,0,0))
    tmp_img.paste(img2, box)  # Paste img2 on top of that.
    img1.alpha_composite(tmp_img)
    return img1


bckgnd = Image.open('background2.png').convert('RGBA')  # Convert to needed mode.

alpha_composite_paste(bckgnd, 'whiteX.png')

orangedot_x, orangedot_y = random.randint(30, 170), random.randint(0, 100)
alpha_composite_paste(bckgnd, 'orangedot.png', (orangedot_x, orangedot_y))

bckgnd.save("test.png")
bckgnd.show()