Dam*_*ick 5 python image python-imaging-library
我正在尝试使用Pillow库(Python Image Library,PIL)将PNG格式的RGB图像转换为使用特定的索引调色板.但是我希望使用"圆形到最接近的颜色"方法进行转换,而不是抖动,因为图像是像素艺术,抖动会扭曲区域的轮廓并将噪声添加到要平坦的区域.
我试过Image.Image.paste(),它使用了四种指定的颜色,但它产生了一个抖动的图像:
from PIL import Image
oldimage = Image.open("oldimage.png")
palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
newimage = Image.new('P', oldimage.size)
newimage.putpalette(palettedata * 64)
newimage.paste(oldimage, (0, 0) + oldimage.size)
newimage.show()
Run Code Online (Sandbox Code Playgroud)
我试图Image.Image.quantize()在图片对类似问题的回答中提到,但它也产生了抖动:
from PIL import Image
palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
palimage = Image.new('P', (16, 16))
palimage.putpalette(palettedata * 64)
oldimage = Image.open("School_scrollable1.png")
newimage = oldimage.quantize(palette=palimage)
newimage.show()
Run Code Online (Sandbox Code Playgroud)
我试过Image.Image.convert(),它转换图像没有抖动,但它包括指定的颜色以外的其他颜色,大概是因为它使用了网页调色板或自适应调色板
from PIL import Image
oldimage = Image.open("oldimage.png")
palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
expanded_palettedata = palettedata * 64
newimage = oldimage.convert('P', dither=Image.NONE, palette=palettedata)
newimage.show()
Run Code Online (Sandbox Code Playgroud)
如何在不抖动的情况下自动将图像转换为特定的调色板?我想避免一个处理Python中每个像素的解决方案,正如John La Rooy的回答和评论中所建议的那样,因为我之前的解决方案涉及用Python编写的内部循环,对于大型图像来说已经证明明显很慢.
用C实现的PIL部分在PIL._imaging模块中,也可以Image.core在您之后使用from PIL import Image.当前版本的Pillow为每个PIL.Image.Image实例提供一个名为member im的实例,该实例是在其中ImagingCore定义的类PIL._imaging.您可以列出其方法help(oldimage.im),但这些方法本身在Python中没有记录.
对象的convert方法ImagingCore在_imaging.c.中实现.这需要一到三个参数,并创建一个新的ImagingCore对象(被称为Imaging_Type内_imaging.c).
mode(必填):模式字符串(例如"P")dither (可选,默认为0):PIL传递0或1paletteimage(可选):ImagingCore带调色板的我面临的问题是,quantize()在dist-packages/PIL/Image.py力的dither参数1.所以我把副本quantize()出方法和改变了这一切.这在Pillow的未来版本中可能不起作用,但如果没有,它们可能会实现"更高版本中的扩展量化器接口",即quantize()承诺中的注释.
#!/usr/bin/env python3
from PIL import Image
def quantizetopalette(silf, palette, dither=False):
"""Convert an RGB or L mode image to use a given P image's palette."""
silf.load()
# use palette from reference image
palette.load()
if palette.mode != "P":
raise ValueError("bad mode for palette image")
if silf.mode != "RGB" and silf.mode != "L":
raise ValueError(
"only RGB or L mode images can be quantized to a palette"
)
im = silf.im.convert("P", 1 if dither else 0, palette.im)
# the 0 above means turn OFF dithering
# Later versions of Pillow (4.x) rename _makeself to _new
try:
return silf._new(im)
except AttributeError:
return silf._makeself(im)
palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
palimage = Image.new('P', (16, 16))
palimage.putpalette(palettedata * 64)
oldimage = Image.open("School_scrollable1.png")
newimage = quantizetopalette(oldimage, palimage, dither=False)
newimage.show()
Run Code Online (Sandbox Code Playgroud)
小智 6
我把所有这些都做得更快了,添加了注释让你理解并转换为枕头而不是 pil。基本上。
import sys
import PIL
from PIL import Image
def quantizetopalette(silf, palette, dither=False):
"""Convert an RGB or L mode image to use a given P image's palette."""
silf.load()
# use palette from reference image made below
palette.load()
im = silf.im.convert("P", 0, palette.im)
# the 0 above means turn OFF dithering making solid colors
return silf._new(im)
if __name__ == "__main__":
import sys, os
for imgfn in sys.argv[1:]:
palettedata = [ 0, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 0, 255, 255, 255,85,255,85, 255,85,85, 255,255,85]
# palettedata = [ 0, 0, 0, 0,170,0, 170,0,0, 170,85,0,] # pallet 0 dark
# palettedata = [ 0, 0, 0, 85,255,85, 255,85,85, 255,255,85] # pallet 0 light
# palettedata = [ 0, 0, 0, 85,255,255, 255,85,255, 255,255,255,] #pallete 1 light
# palettedata = [ 0, 0, 0, 0,170,170, 170,0,170, 170,170,170,] #pallete 1 dark
# palettedata = [ 0,0,170, 0,170,170, 170,0,170, 170,170,170,] #pallete 1 dark sp
# palettedata = [ 0, 0, 0, 0,170,170, 170,0,0, 170,170,170,] # pallet 3 dark
# palettedata = [ 0, 0, 0, 85,255,255, 255,85,85, 255,255,255,] # pallet 3 light
# grey 85,85,85) blue (85,85,255) green (85,255,85) cyan (85,255,255) lightred 255,85,85 magenta (255,85,255) yellow (255,255,85)
# black 0, 0, 0, blue (0,0,170) darkred 170,0,0 green (0,170,0) cyan (0,170,170)magenta (170,0,170) brown(170,85,0) light grey (170,170,170)
#
# below is the meat we make an image and assign it a palette
# after which it's used to quantize the input image, then that is saved
palimage = Image.new('P', (16, 16))
palimage.putpalette(palettedata *32)
oldimage = Image.open(sys.argv[1])
oldimage = oldimage.convert("RGB")
newimage = quantizetopalette(oldimage, palimage, dither=False)
dirname, filename= os.path.split(imgfn)
name, ext= os.path.splitext(filename)
newpathname= os.path.join(dirname, "cga-%s.png" % name)
newimage.save(newpathname)
# palimage.putpalette(palettedata *64) 64 times 4 colors on the 256 index 4 times, == 256 colors, we made a 256 color pallet.
Run Code Online (Sandbox Code Playgroud)