图片转txt
2022-03-20 python 1501
# ! /usr/bin/python # -*- coding: utf-8 -*- # author:凌 # datetime:2022/03/20 16:31 # software:PyCharm from PIL import Image # pip install Pillow import argparse def get_char(r, g, b, alpha=256): if alpha == 0: return ' ' #assic_char = list("'\"\\!@#$%^&*()_+abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:{}<>?0123456789./|") assic_char = list("#= ") #assic_char = list("0$+-") length = len(assic_char) gray = (2126 * r + 7152 * g + 722 * b) / 10000 # gray/256=x/len(assic_char) x = int((gray / (alpha+1.0)) * len(assic_char)) xi = int(x % length) return assic_char[xi] def write_file(out_file_name, content): with open(out_file_name, "w") as f: f.write(content) def main(file_name="test.jpg", width=500, height=300,out_file_name="pic.txt"): im = Image.open(file_name) im = im.resize((width, height), Image.NEAREST) text = "" for i in range(height): for j in range(width): content = im.getpixel((j, i)) text += get_char(*content) text += "\n" print(text) write_file(out_file_name, text) def parse_param(): """ param analyzer :return: """ parser = argparse.ArgumentParser() # input_file parser.add_argument("input_file") parser.add_argument("out_file") parser.add_argument("--width",type=int,default=50) parser.add_argument("--height",type=int,default=50) args = parser.parse_args() width,height,in_file,out_file = args.width,args.height,args.input_file,args.out_file return width,height,in_file,out_file if __name__ == "__main__": #width,height,in_f,out_f = parse_param() # http://dun.163.com/trial/jigsaw # py index.py 1.jpg 111111.txt --width 100 --height 100 # py index.py screen2.png 111111111.txt --width 321 --height 153 # py index.py shape.png 222222.txt --width 60 --height 158 # py index.py quekou.png 111111111.txt --width 70 --height 174 #main(file_name=in_f,width=width,height=height,out_file_name=out_f) main("3.png", width=85, height=15)
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论