- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 PIL paste()
功能。我也想戴上面具,但我不断收到此错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
canvases[0].paste(mnist_images[i],
box=tuple(map(lambda p: int(round(p)), positions[i])), mask=mask)
If a mask is given, this method updates only the regions indicated by the mask. You can use either
"1"
,"L"
, or"RGBA"
images (in the latter case, the alpha band is used as mask). Where the mask is 255, the given image is copied as is. Where the mask is 0, the current value is preserved. Intermediate values will mix the two images together, including their alpha channels if they have them.
"1"
或
"L"
?
最佳答案
面具也必须是 PIL Image
.这没有明确提及 in the docs ,但它确实指出:
You can use either “1”, “L” or “RGBA” images (in the latter case, the alpha band is used as mask). Where the mask is 255, the given image is copied as is. Where the mask is 0, the current value is preserved. Intermediate values will mix the two images together, including their alpha channels if they have them.
Image
s。来自
Pillow concepts page :
The mode of an image defines the type and depth of a pixel in the image. The current release supports the following standard modes:
1
(1-bit pixels, black and white, stored with one pixel per byte)
L
(8-bit pixels, black and white)
...
Image
和
mask = Image.fromarray(mask)
bool
键入,然后您会想要执行以下操作:
mask = Image.fromarray(np.uint8(255*mask))
>>> import numpy as np
>>> import cv2
>>> from PIL import Image
>>> img = Image.fromarray(np.uint8(255*np.random.rand(400, 400, 3)))
>>> sub_img = Image.fromarray(np.uint8(255*np.ones((200, 200, 3))))
>>> mask = Image.fromarray(np.uint8(255*(np.random.rand(200, 200) > 0.7)))
>>> img.paste(sub_img, (0, 0), mask)
sub_img
在
img
在左上角,并从粘贴操作中屏蔽了大约 70% 的像素,因此该区域中只有大约 30% 的像素实际上显示为白色。
关于python - 如何使用PIL贴面膜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47723154/
我是一名优秀的程序员,十分优秀!