如何将GIF转换为字符串?

时间:2017-09-18 11:52:16

标签: python ocr

我想将此网址下载转换为gif。     http://t.51chuli.com/contact/d91779cced1ade729yz45q72bn3o5o1z.gif

到phone_num字符串

我该怎么办? 我的代码是:

import requests

from PIL import Image

import tesserocr

img = requests.get(url='http://t.51chuli.com/contact/d91779cced1ade729yz45q72bn3o5o1z.gif')
with open('image.gif','wb') as f:
    f.write(img.content)

pil = Image.open('image.gif')
text = tesserocr.image_to_text(pil)

print text

但没有输出

1 个答案:

答案 0 :(得分:0)

我有想法:

import sys
from pytesseract import *
import requests
import os
import re
from PIL import Image
from PIL import ImageEnhance
import pkg_resources
url = 'http://t.51chuli.com/contact/d91779cced1ade729yz45q72bn3o5o1z.gif'
html = requests.get(url)
with open('vercode.gif', 'wb') as f:
    f.write(html.content)
    f.flush()
    os.fsync(f.fileno())
if os.path.isfile('vercode.gif'):
    image = Image.open('vercode.gif')
    image = image.convert('L')
    threshold = 250 
    initTable = lambda x:0 if x < threshold else 1
    binaryImage = image.point(initTable, '1')
    vcode = image_to_string(binaryImage, lang="eng", config='-psm 7')
    print vcode.encode('utf-8').replace(' ','')