Convert character(s) to tuple and back

孤街浪徒 提交于 2019-12-11 16:35:07

问题


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

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.


回答1:


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.

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



回答2:


I suspect you want to encode different characters into different colour channels:

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

        if ind >= text_length : break # only change pixel value for length of text

        r = convert_char_to_int(text[ind], limit=limit)
        ind = max( ind+1, text_length - 1)  # don't go beyond text length
        g = convert_char_to_int(text[ind], limit=limit)
        ind = max( ind+1, text_length - 1)
        b = convert_char_to_int(text[ind], limit=limit)
        ind += 1

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


来源:https://stackoverflow.com/questions/51334345/convert-characters-to-tuple-and-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!