尝试使用随机坐标创建合成图像(使用 PIL)并一次保存多个文件 [英] Trying to create a composite image with random coordinates (using PIL) and saving multiple files at once

查看:57
本文介绍了尝试使用随机坐标创建合成图像(使用 PIL)并一次保存多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

更新

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

from PIL import Image随机导入def alpha_composite_paste(img1, img2, box=None):''' Alpha 合成将一个图像粘贴到另一个图像上.`img1` 和 `img2` 都可以可以是图像对象或图像文件的文件路径.盒子"参数要么是给出左上角的 2 元组,要么是 4 元组定义左、上、右、下像素坐标,或 None(与 (0, 0) 相同).两个图像都必须是 RGBA 模式.为方便起见,返回修改后的 img1.'''if isinstance(img1, str): # 图片文件路径?img1 = Image.open(img1)if isinstance(img2, str): # 图片文件路径?img2 = Image.open(img2)# 使完全透明的图像与 img1 大小相同.tmp_img = Image.new('RGBA', img1.size, color=(0,0,0,0))tmp_img.paste(img2, box) # 在上面粘贴 img2.img1.alpha_composite(tmp_img)返回 img1bckgnd = Image.open('background2.png').convert('RGBA') # 转换为需要的模式.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()

I'm trying to adapt the code from this page with a slightly different purpose:

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

I'm using a black background in the back with 2 images (a white X and an orange dot) made with transparent background.The result is a complete black background.I'd like to have the orange dot behind the white X cross but unfortunately nothing is visible.

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()

As a last action I would like to save (for example 500 times) the output files in jpg or similar format. I have tried various solutions without being able to write anything really functional... any help really appreciated! How do you suggest I proceed?

解决方案

Your question is very similar to what's in my answer to one titled How do I make the background of an image, that is pasted on top of another image, transparent in pillow? with a few (relatively minor) differences:

  1. The background image needed to first be converted into "RGBA" mode (because it's a paletted, "P" mode, image)
  2. Multiple images are being composited.

Here the results (see linked answer for explanation):

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()

Sample test.png file generated:

Update

Since the above contains a lot of repeated code, and in principle one should generally strive to keep things DRY, here's an alternative implementation that defines a function to do the image compositing work.

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()

这篇关于尝试使用随机坐标创建合成图像(使用 PIL)并一次保存多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆