尝试使用colorsys改变色调/饱和度。返回带有奇怪色块的灰度图像

2024-04-25 09:55:12 发布

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

我想更改图像的色调/饱和度/亮度。不管我是否真的改变了h/s/v参数,输出的图像总是输入图像的灰度版本,带有一些块状的彩色部分。在这个代码示例中,我故意忽略了实际更改值,以便您可以看到问题的受控示例。尽管有一条评论解释了我打算在何时何地改变色调。你知道吗

from PIL import Image
import colorsys
import numpy as np

image = Image.open("/home/some_image.jpg", mode="r")
width, height = image.size
np_image = np.asarray(image)
image.close()
np_image = np_image.reshape((height*width, 3))

out_image = np.zeros((height*width, 3), dtype=np.uint8)
#out_image = np.zeros((height*width, 3))

for i in range(0, len(np_image)-1):
    hsv_tuple = colorsys.rgb_to_hsv(np_image[i][0], np_image[i][1], np_image[i][2])
    h = hsv_tuple[0]
    # apply some transformations to the hue component...
    # h = h + yada yada yada
    rgb_tuple = colorsys.hsv_to_rgb(h, hsv_tuple[1], hsv_tuple[2])
    rgb_tuple = np.asarray(rgb_tuple)
    out_image[i] = rgb_tuple

out_image = out_image.reshape((height, width, 3))

outim = Image.fromarray(out_image, "RGB")
outim.save("/home/some_recoloured_image.jpg")

使用此项:

input image

我明白了:

output image


Tags: to图像imageimportnpsomergbout