简体   繁体   English

将 .txt 文件转换为图像

[英]convert .txt file to image

I have txt be like this " 01100000,10101000,01101000,01110000,10101100,01110000,01101000 " how can I read this file to use it in this code ?我的 txt 是这样的“01100000,10101000,01101000,01110000,10101100,01110000,01101000”我如何读取这个文件以在此代码中使用它?

value = " how can read file here"
vdiv = [value[i:i+8] for i in range(0, len(value), 8)]

def diff(inp):
    if inp == '1':
        return (0,0,0)
    if inp == '0':
        return (255,255,255)
    else:
        pass

img = Image.new( 'RGB', (8,len(vdiv)), "white")
pixels = img.load()

##
for x in range(img.size[0]):
    for y in range(img.size[1]):
        for i in vdiv:
            for i2 in i:
                pixels[x,y] = diff(i2) #Creates a black image. What do?

img.show()

You can use open('filename.txt', 'r').read() like so:您可以像这样使用open('filename.txt', 'r').read()

fd = open('filename.txt', 'r')
value = fd.read()
fd.close()

...

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

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