使用PIL将图像转换为特定调色板而不抖动

时间:2015-04-03 13:34:28

标签: 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()    

我在pictu's answer to a similar question中提到了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()

我尝试了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()

如何在不抖动的情况下自动将图像转换为特定的调色板?我想避免一个处理Python中每个像素的解决方案,如John La Rooy's answer中所建议的那样,并对其进行评论,因为我之前的解决方案涉及用Python编写的内部循环,对于大型图像来说已经证明明显很慢。 p>

2 个答案:

答案 0 :(得分:7)

用C实现的PIL部分位于PIL._imaging模块中,Image.core之后也可用from PIL import Image。 当前版本的Pillow为每个PIL.Image.Image实例提供名为im的成员,该成员是ImagingCore的实例,是PIL._imaging中定义的类。 您可以使用help(oldimage.im)列出其方法,但这些方法本身在Python中没有记录。

convert ImagingCore对象的ImagingCore方法在_imaging.c中实现。 它需要一到三个参数并创建一个新的Imaging_Type对象(在_imaging.c中称为mode)。

  • "P"(必填):模式字符串(例如dither
  • paletteimage(可选,默认为0):PIL传递0或1
  • ImagingCore(可选):带调色板的quantize()

我遇到的问题是dist-packages/PIL/Image.py中的dither强制quantize()参数为1。 所以我删除了quantize()方法的副本并更改了它。 这在Pillow的未来版本中可能不起作用,但如果没有,他们可能会在以后的版本中实现扩展的量化器接口" #!/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() 中的评论承诺。

{{1}}

答案 1 :(得分:2)

我把所有这些都变得更快了,为你加上了解和理解。转为枕头而不是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.