微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用随机坐标创建合成图像使用 PIL并一次保存多个文件 更新

如何解决尝试使用随机坐标创建合成图像使用 PIL并一次保存多个文件 更新

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

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

我在背面使用黑色背景和透明背景制作的 2 张图像(一个白色 X 和一个橙色点)。结果是一个完整的黑色背景。我希望在白色后面有橙色点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 次)输出文件。我尝试了各种解决方案,却无法编写任何真正实用的东西...... 任何帮助真的很感激!你如何建议我继续?

解决方法

您的问题与 my answerHow do I make the background of an image,that is pasted on top of another image,transparent in pillow? 中的问题非常相似,但有一些(相对较小的)差异:

  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))
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',0))
tmp_img.paste(orangedot,(orangedot_x,orangedot_y))
bckgnd.alpha_composite(tmp_img)

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

生成的示例 test.png 文件:

test.png file generated

更新

由于上面包含很多重复的代码,原则上应该尽量保留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,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,100)
alpha_composite_paste(bckgnd,'orangedot.png',orangedot_y))

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?