栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

用C实现的PIL部分在

PIL._imaging
模块中,您也可以在
Image.core
之后使用
from PIL importImage
。当前版本的Pillow为每个
PIL.Image.Image
实例赋予一个名为成员的成员
im
,该成员是实例的成员,该成员
ImagingCore
定义在
PIL._imaging
。您可以使用列出其方法
help(oldimage.im)
,但是这些方法本身在Python中均未记录。

对象的

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

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

我面临的问题是,

quantize()
dist-packages/PIL/Image.py
力的
dither
参数1。所以我把副本
quantize()
方法出来,改变了这一切。这可能在将来的Pillow版本中不起作用,但如果不能,则他们很可能会实现“
quantize()
承诺的更高版本中的扩展量化器接口”

#!/usr/bin/env python3from PIL import Imagedef 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()


转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/640272.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号