1、利用PIL模块处理图片
下载PIL,网址如下:http://www.pythonware.com/products/pil/
2、在windows下练习:
编写Python源码
from PIL import Image
def get_Text(img):
codelist = “WQOo”’,. "#显示的字符
count = len(codelist)
print(count)
img = img.convert(“L”)
charlist = ‘’
print(img.size[0], img.size[1])
for h in range(0, img.size[1]):
for w in range(0, img.size[0]):
gray = img.getpixel((w, h))
pos = gray / 30 #数字与显示的字符个数有关,255/个数=30
charlist = charlist + codelist[pos]
charlist = charlist + ‘\n’
return charlist
if name == ‘main’:
file = open(‘d:\img\1.jpg’, ‘rb’)
img = Image.open(file)
outfile = open(‘d:\img\tmp.txt’, ‘w’)
outfile.write(get_Text(img))
outfile.close()