将字符转换为元组并返回 [英] Convert character(s) to tuple and back

查看:110
本文介绍了将字符转换为元组并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改此编码器的输出:
https://github.com/akapila011/Text- to-Image/blob/master/text_to_image/encode.py
从灰度到三色方案(如此处所示):三色
我需要从编码器更改的主要代码行是:

I am trying to change the output of this encoder:
https://github.com/akapila011/Text-to-Image/blob/master/text_to_image/encode.py
from grayscale to a tri-color scheme such as the one shown here: Tricolor
The main lines of code I need to change from the encoder are:

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

我仅以base64文本形式加载,因此我只能严格使用64个字符.
有人告诉我,我需要更改convert_char_to_int以将元组作为RGB值返回.但是我不确定如何做到这一点?我是否将int转换为rgb,如果这样,如何转换?
我需要颠倒该过程,以便我也将解码回文本.

Im loading in base64 text only, so im working with 64 characters strictly.
I was told that I needed to change convert_char_to_int to return the tuple as an RGB value. But im not sure how to do this? Do I convert int to rgb, if so, how so?
I'd need to reverse the process in order for me to Decode it back into text too.

推荐答案

它看起来像 PIL 是他们正在使用的库.要回答您的问题,实际上取决于您如何将rgb值编码为chars.一种方法是让一个字符代表一种颜色的亮度-因此,需要三个字符代表一个像素.另一点可以确定的是,您的字符只能表示0到63之间的值,而不是通常的0到255.因此,您需要将每个值乘以4,这样图像就不会太暗.

It looks like the PIL is the library that they are using. To answer your question, it really depends on how you want to encode your rgb values as chars. One way to do it would be to let one char represent the brightness of one of the colors - so it would take three chars to represent a pixel. The other point be sure of is that your chars will only represent values between 0 and 63 instead of the usual 0 to 255. Therefore you will want to multiply each value by 4 so that the image is not extremely dark.

这是我重写encode函数的方法:

Here's how I would rewrite the encode function:

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

ind = 0

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

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

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

            r = convert_char_to_int(text[ind], limit=64) * 4
            g = convert_char_to_int(text[ind+1], limit=64) * 4
            b = convert_char_to_int(text[ind+2], limit=64) * 4

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

            ind += 3

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

            break

img.save(result_path)

return result_path

这篇关于将字符转换为元组并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆