用树莓皮上的PIL将一个图像添加到另一个图像中

2024-09-24 22:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是带摄像头的树莓皮。我写了一个脚本让相机模块拍照。现在,在我拍下照片后,我需要一个Python脚本,它将最近拍摄的照片和其他图片(比如水印或徽标)放在一起。我试着用这个:http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ,但我不知道我必须使用哪些命令或语法。 有人能帮帮我吗。我是Python的新成员。在

编辑: 我的Python脚本。。。它与时间合影。在

from datetime import datetime
zeit = datetime.now()
zeit = "%s-%s-%s-%s:%s:%s" % (zeit.year,zeit.day,zeit.month,zeit.hour,zeit.minute,zeit.second)
Bildformat = ".png"

Bildname = "Bild-" + zeit + Bildformat

from subprocess import call

#call (["raspistill -o " + Bildname + " -t 1 -n"], shell=True)
call (["raspistill -o /home/pi/cam_project/Bilder/" + Bildname + " -t 1 -n"], shell=True)

编辑二:谢谢你的回答。这是我的全部代码。我想在教程中添加类似的内容(查看上面的链接)。其主要思想是相机拍摄一张图片,PIL库(见链接)抓取图片并将这张最近拍摄的照片添加到另一张图片中(我以前制作过,这张照片在同一个目录中(比如水印或徽标))

比如:

^{pr2}$

如果你点击我在上面发布的链接,并在代码后向下滚动到“滚动图像”:

For more advanced tricks, the paste method can also take a transparency mask as an optional argument. In this mask, the value 255 indicates that the pasted image is opaque in that position (that is, the pasted image should be used as is). The value 0 means that the pasted image is completely transparent. Values in-between indicate different levels of transparency.


作为输出,我想要经过处理的图片(带有徽标/水印)


Tags: theimage脚本datetimethatis链接图片
1条回答
网友
1楼 · 发布于 2024-09-24 22:29:46

我想这对你有用。它可以处理您使用的水印图像的透明度,也可以使水印图像更亮,因此它不可见。在

import PIL.Image
import PIL.ImageEnhance

# images
base_path = 'base.jpg'
watermark_path = 'watermark.png'
base = PIL.Image.open(base_path)
watermark = PIL.Image.open(watermark_path)

# optional lightness of watermark from 0.0 to 1.0
brightness = 0.5
watermark = PIL.ImageEnhance.Brightness(watermark).enhance(brightness)

# apply the watermark
some_xy_offset = (10, 20)
# the mask uses the transparency of the watermark (if it exists)
base.paste(watermark, some_xy_offset, mask=watermark)
base.save('final.png')
base.show()

public domain cat image

(公共域cat映像)

相关问题 更多 >