(Python)我如何影响PIL的图像量化选择?

2024-09-29 23:31:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行下面的代码来删除均匀色块之间不需要的渐变。这在很多情况下都能很好地工作,但是有时会失败,因为一些渐变色和调色板颜色很相似,比如下面的图像(top=original,bottom=result,right=palete image)。我怎么能避免呢?你知道吗

enter image description here

from PIL import Image
import os

path_images = "D:/RasterCalc/Samples_Original"
path_palette = "D:/RasterCalc/Palettes/palette_nsph.png"
path_output = "D:/RasterCalc/Samples_Converted"

image_palette = (Image.open(path_palette)).convert('RGBA')
image_palette = image_palette.convert('P')

for filename in os.listdir(path_images):
    image = (Image.open(path_images+"/"+filename)).convert('RGB')
    print(filename)
    image_converted = image.quantize(palette=image_palette)
    image_converted.save(path_output+"/"+filename)

print("==================")
print("done!")

/edit:现在用一些圆圈来澄清!你知道吗


Tags: pathimageimportconvertoutputosopenfilename

热门问题