I need to convert series of images drawn as white on black background letters to images where white and black are inverted (as negative). How can I achieve this using PIL?
我需要将在黑色背景字母上绘制为白色的一系列图像转换为白色和黑色被反转的图像(作为负片)。如何使用PIL实现这一目标?
5 个解决方案
#1
51
Try the following from the docs: http://effbot.org/imagingbook/imageops.htm
请从文档中尝试以下内容: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')
Note: "The ImageOps module contains a number of 'ready-made' image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images."
注意:“ImageOps模块包含许多'现成的'图像处理操作。这个模块有点实验性,大多数操作员只能处理L和RGB图像。”
#2
16
If the image is RGBA transparent this will fail... This should work though:
如果图像是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')
#3
11
For anyone working with an image in "1" mode (i.e., 1-bit pixels, black and white, stored with one pixel per byte -- see docs), you need to convert it into "L" mode before calling PIL.ImageOps.invert
.
对于使用“1”模式的图像的任何人(即1位像素,黑色和白色,每个字节存储一个像素 - 请参阅文档),您需要在调用PIL之前将其转换为“L”模式.ImageOps 。倒置。
Thus:
从而:
im = im.convert('L')
im = ImageOps.invert(im)
im = im.convert('1')
#4
0
In case someone is inverting a CMYK
image, the current implementations of PIL and Pillow don't seem to support this and throw an error. You can, however, easily circumvent this problem by inverting your image's individual bands using this handy function (essentially an extension of Greg Sadetsky's post above):
如果有人正在反转CMYK图像,PIL和Pillow的当前实现似乎不支持这个并抛出错误。但是,您可以通过使用这个方便的功能(基本上是Greg Sadetsky上面的帖子的扩展)反转图像的各个波段来轻松解决这个问题:
def CMYKInvert(img) :
return Image.merge(img.mode, [ImageOps.invert(b.convert('L')) for b in img.split()])
#5
0
from PIL import Image
img = Image.open("archive.extension")
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
x,y,z = pixels[i,j][0],pixels[i,j][1],pixels[i,j][2]
x,y,z = abs(x-255), abs(y-255), abs(z-255)
pixels[i,j] = (x,y,z)
img.show()
`
`
#1
51
Try the following from the docs: http://effbot.org/imagingbook/imageops.htm
请从文档中尝试以下内容: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')
Note: "The ImageOps module contains a number of 'ready-made' image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images."
注意:“ImageOps模块包含许多'现成的'图像处理操作。这个模块有点实验性,大多数操作员只能处理L和RGB图像。”
#2
16
If the image is RGBA transparent this will fail... This should work though:
如果图像是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')
#3
11
For anyone working with an image in "1" mode (i.e., 1-bit pixels, black and white, stored with one pixel per byte -- see docs), you need to convert it into "L" mode before calling PIL.ImageOps.invert
.
对于使用“1”模式的图像的任何人(即1位像素,黑色和白色,每个字节存储一个像素 - 请参阅文档),您需要在调用PIL之前将其转换为“L”模式.ImageOps 。倒置。
Thus:
从而:
im = im.convert('L')
im = ImageOps.invert(im)
im = im.convert('1')
#4
0
In case someone is inverting a CMYK
image, the current implementations of PIL and Pillow don't seem to support this and throw an error. You can, however, easily circumvent this problem by inverting your image's individual bands using this handy function (essentially an extension of Greg Sadetsky's post above):
如果有人正在反转CMYK图像,PIL和Pillow的当前实现似乎不支持这个并抛出错误。但是,您可以通过使用这个方便的功能(基本上是Greg Sadetsky上面的帖子的扩展)反转图像的各个波段来轻松解决这个问题:
def CMYKInvert(img) :
return Image.merge(img.mode, [ImageOps.invert(b.convert('L')) for b in img.split()])
#5
0
from PIL import Image
img = Image.open("archive.extension")
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
x,y,z = pixels[i,j][0],pixels[i,j][1],pixels[i,j][2]
x,y,z = abs(x-255), abs(y-255), abs(z-255)
pixels[i,j] = (x,y,z)
img.show()
`
`