我这里有更好的,可以手动调清晰度。像这个就很清晰。
程序是Python程序,代码如下:
from PIL import Image
from pygame import *
from tkinter.filedialog import *
#char_set = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
char_set = """▓躝韊矚飅矙蠨雦蠾鑘轥轥驑钄钃隬韈驌騙顨闘飀靂赙駰駎韂騲顤飕騻畹階隞M骫駘禧蜈鎡飔%蛲靔頄搿N蛏焖楂聍柘柩矧宕長怩闫釸饨袷叩佚5忌迲玄C彐引无Y仔玎氿q爿川必不/众玍门}刁王寸疒江兰立y亡广ど辷人ベ廴ソと乛ン辶∵︷゛﹨灬﹣丶﹒ """
char_two=["M","%","N","5","C","Y","q","/","}","y"," "]
imgfile=askopenfilename()
try:
xx=int(input("最大长:"))
yy=int(input("最大宽:"))
except:
xx=0
yy=0
im = Image.open(imgfile).convert("RGBA")
x,y = im.size # 宽高像素
img2 = Image.new('RGB', size=(x, y), color=(255, 255, 255))
img2.paste(im, (0, 0), mask=im)
im=img2
#im = Image.frombuffer('RGB', (x, y),image.tostring(image.load(imgfile),"RGB"),"raw", "RGB", 0, 1)
if y>=x:
if round(xx / x * y ) <= yy:
sizeb = (xx, round(xx / x * y ))
else:
sizeb = (round(47 / y * x), yy)
else:
if round(yy / y * x)<=xx:
sizeb = (round(yy/y * x), yy)
else:
sizeb = (xx, round(xx/ x * y))
if xx==0 and yy==0:
sizeb=(x,y)
im = im.resize(sizeb, Image.ANTIALIAS)
im = im.convert('L')
im.save('t.jpeg')
text = ''
for i in range(im.height):
for j in range(im.width):
gray = im.getpixel((j, i))
gray = round(gray/255*114
text += char_set[gray]
if char_set[gray] in char_two:
text += char_set[gray]
text += '\n'
print(text)
with open('pic.txt', 'w',encoding="utf-8")as f:
f.write(text)
text2=text.split("\n")
print(text2)
xxx,yyy=sizeb
init()
font1=font.SysFont("simsun", 14)
aaaa = display.set_mode((xxx*14,yyy*14))
aaaa.fill("white")
numm=0
while True:
try:
text3=text2[numm]
except:
break
font.init()
text = font1.render(text3, True, "black")
textRect = text.get_rect()
textRect.center = (xxx * 7, 7+numm*14)
aaaa.blit(text, textRect)
numm+=1
display.flip()
image.save(aaaa, "1.png")
quit()