推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jerryliang
V2EX  ›  Python

求助腾讯云 API 编写

  •  
  •   jerryliang · May 13, 2020 · 1626 views
    This topic created in 2218 days ago, the information mentioned may be changed or developed.

    楼主是一个半吊子,用 tx 的 API 生成了一段 ocr 编码,想用 base64,然后添加了 python 图片转 base64 的编码。

    with open("1.jpg","rb") as f:
        base64_data = base64.b64encode(f.read())  
    print(base64_data)
    print(type(base64_data))
    req = models.GeneralBasicOCRRequest()
    
    params = '{"ImageBase64":"base64_data"}'
    

    但是呢,这样子写直接提示我图片解析失败问 params 里的 base64_data 怎么变成 print(base64_data)里的 base64.

    ClericPy
        1
    ClericPy  
       May 13, 2020
    文档地址你也不发, 随便答一波吧:

    1. params 是给 GET 请求拼凑 query 字符串用的, 这里命名如果没错那参数提交时候可能就错了, 就我所知我上传图片都是 POST 的

    2. POST 请求时候的实体主体 data 都是要编码成 bytes 的, 所以记得 encode 一下

    3. params = '{"ImageBase64":"base64_data"}' 这里要塞变量进去的,

    所以要么 json.dumps({"ImageBase64":base64_data})

    要么就 params = '{"ImageBase64":"%s"}' % base64_data

    总之别忘了 encode
    jerryliang
        2
    jerryliang  
    OP
       May 14, 2020
    @ClericPy 嗯,自己 print 了 params 。是一个 str 的类型,故使用字符串拼接的方式把它拼接了,然后就搞定了
    with open("1.jpg","rb") as f:
    base64_data = base64.b64encode(f.read())
    #print(base64_data)
    #print(type(base64_data))
    req = models.GeneralBasicOCRRequest()
    a = "{\"ImageBase64\":\""
    b = "\"}"
    params = a + base64_data + b
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   929 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
    ♥ Do have faith in what you're doing.