报错提示:
>>> if 0: img = Image.Image
w,h = img.size[:2] #size of the image
SyntaxError: invalid syntax
源代码如下:
from PIL import Image
import os
def PngToTxt(f):
Palette={'000':'#'#榛�
,'010':'@'#鏆楃豢
,'020':'/'#缁�
,'001':'$'#娣辫摑
,'011':'<'#闈�
,'021':'"'#浜�豢鑹�
,'002':'='#钃�
,'012':'\\'#闂�摑鑹�
,'022':'_'#娴呯豢
,'100':'>'#鏆楃孩
,'110':'*'#鏆楅粍
,'120':'~'#榛勭豢鑹�
,'101':'%'#鏆楃传
,'111':'+'#鐏�
,'121':'^'#娴呯豢鑹�
,'102':'|'#绱�綏鍏�
,'112':'!'
,'122':'-'
,'200':'&'#绾�
,'210':';'#姗欒壊
,'220':"'"#榛�
,'201':')'#娣辩矇鑹�
,'211':'.'#绮夌孩鑹�
,'221':'`'
,'202':''#绱�
,'212':','
,'222':' '#鐧�
}
img = Image.open(f)
if 0: img = Image.Image
#缩小, 结果比较模糊, 还是用原比例较好
#img = img.resize((80,60))
w,h = img.size[:2]
print w,h
result = []
for i in range(h):
result1 = []
for j in range(w):
pixel = img.getpixel((j,i))[:3]
result1.append(Palette[''.join([str(int(x//85.3)) for x in pixel])])
result.append(result1)
r="\n".join(["".join(x) for x in result])
print r
object_file=os.path.splitext(f)[0]+".txt"
open(object_file,'w').write(r)