bia*_*lix 47 python python-imaging-library
我需要将在黑色背景字母上绘制为白色的一系列图像转换为白色和黑色被反转的图像(作为负片).如何使用PIL实现这一目标?
Gar*_*err 72
请尝试以下文档中的以下内容:http://effbot.org/imagingbook/imageops.htm
from PIL import Image
import PIL.ImageOps
image = Image.open('your_image.png')
inverted_image = PIL.ImageOps.invert(image)
inverted_image.save('new_name.png')
Run Code Online (Sandbox Code Playgroud)
注意:"ImageOps模块包含许多'现成的'图像处理操作.这个模块有点实验性,大多数操作员只能处理L和RGB图像."
小智 30
如果图像是RGBA透明,这将失败...这应该工作:
from PIL import Image
import PIL.ImageOps
image = Image.open('your_image.png')
if image.mode == 'RGBA':
r,g,b,a = image.split()
rgb_image = Image.merge('RGB', (r,g,b))
inverted_image = PIL.ImageOps.invert(rgb_image)
r2,g2,b2 = inverted_image.split()
final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a))
final_transparent_image.save('new_file.png')
else:
inverted_image = PIL.ImageOps.invert(image)
inverted_image.save('new_name.png')
Run Code Online (Sandbox Code Playgroud)
Gre*_*sky 18
对于使用"1"模式的图像的任何人(即1位像素,黑色和白色,每个字节存储一个像素 - 请参阅文档),您需要在调用之前将其转换为"L"模式PIL.ImageOps.invert.
从而:
im = im.convert('L')
im = ImageOps.invert(im)
im = im.convert('1')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57082 次 |
| 最近记录: |