简体   繁体   English

灰度到三色转换

[英]Grayscale to tri-color conversion

I would like to change the output PNG of this script to tri-color (red, blue, yellow) 我想将此脚本的输出PNG更改为三色(红色,蓝色,黄色)

Instead of its current grayscale format. 而不是其当前的灰度格式。

I figure I need to change lines 34-45, but im not sure how to go about it. 我认为我需要更改34-45行,但是我不确定如何去做。

img = Image.new("L", size)  # grayscale, blank black image

ind = 0

for row in range(0, size[0]):

    for col in range(0, size[1]):

        if ind < text_length:  # only change pixel value for length of text

            pixel_value = convert_char_to_int(text[ind], limit=limit)

            img.putpixel((row, col), pixel_value)

            ind += 1

        else:  # end of text, leave remaining pixel(s) black to indicate null

            break

img.save(result_path)

return result_path

Any advice would be useful! 任何建议将是有用的!

According to the PIL documentation for Image.putpixel() : 根据Image.putpixel()PIL文档

im.putpixel(xy, colour) 输入像素(xy,颜色)

Modifies the pixel at the given position. 在给定位置修改像素。 The colour is given as a single numerical value for single-band images, and a tuple for multi-band images. 对于单波段图像,颜色作为单个数值给出,对于多波段图像,颜色作为元组给出。

So, to insert a coloured pixel using RGB format: 因此,要使用RGB格式插入彩色像素:

img = Image.new("RBG", size)  # RGB image

img.putpixel((row, col), (r,g,b))

You may also be interested in Image.convert() 您可能也对Image.convert()感兴趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM