我有错误:
Traceback (most recent call last):
File "C:/Users/kch/PycharmProjects/Captca_is_good/captcha.py", line 79, in <module>
m.update("{0}{1}".format(time.time(),count))
TypeError: Unicode-objects must be encoded before hashing
当我尝试在Python 3.5.0中执行此代码时:
from PIL import Image
import hashlib
import time
im = Image.open("captcha.gif")#.encoding = 'ISO-8859-1'
im = im.convert("P")
im2 = Image.new("P",im.size,176)
im = im.convert("P")
temp = {}
for x in range(im.size[1]):
for y in range(im.size[0]):
pix = im.getpixel((y,x))
temp[pix] = pix
if pix == 0: # these are the numbers to get
im2.putpixel((y,x),0)
inletter = False
foundletter=False
start = 0
end = 0
letters = []
for y in range(im2.size[0]): # slice across
for x in range(im2.size[1]): # slice down
pix = im2.getpixel((y,x))
if pix != 176:
inletter = True
if foundletter == False and inletter == True:
foundletter = True
start = y
if foundletter == True and inletter == False:
foundletter = False
end = y
letters.append((start,end))
inletter=False
print(letters)
count = 0
for letter in letters:
m = hashlib.md5()
im3 = im2.crop(( letter[0] , 0, letter[1],im2.size[1] ))
m.update("{0}{1}".format(time.time(),count))
im3.save("./%s.gif"%(m.hexdigest()))
count += 1
出了什么问题?