V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
vZexc0m
V2EX  ›  Python

小熊猫写日记 Python 版

  •  
  •   vZexc0m · 2018-05-21 16:26:40 +08:00 · 6189 次点击
    这是一个创建于 2138 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到 v2 上有一个 js 版小熊猫日记生成,我也用 Python 写了一个。 部署在 pythonanywhere 地址:[http://qwer110.pythonanywhere.com/][1] 可能有点慢,谅解一下,毕竟免费的。 *图片示例: image

    第 1 条附言  ·  2018-05-21 21:43:29 +08:00

    贴下核心代码

     def create():
    139     if request.method == "GET":
    140         text = request.args.get("text")
    141     else:
    142         text = request.form.get("text")
    143     if text == "" or not text:
    144         return jsonify(error="内容不能为空")
    145     str_list = list()
    146     for i in range(0, len(text), 24):
    147         if i + 24 > len(text):
    148             str_list.append(text[i:])
    149         else:
    150             str_list.append(text[i:i + 24])
    151     txt = "\n".join(str_list)
    152     img = Image.open('/home/QWER110/mysite/src/base.jpg')
    153     font = ImageFont.truetype('/home/QWER110/mysite/src/ios.ttf', 35)
    154     draw = ImageDraw.Draw(img)
    155     draw.text((31, 750), txt, (0, 0, 0), font=font)
    156     byte_io = BytesIO()
    157     img.save(byte_io, 'JPEG')
    158     byte_io.seek(0)
    159     base64_img = "data:image/png;base64," + base64.b64encode(byte_io.getvalue()).decode('ascii')
    160     return base64_img
    
    13 条回复    2018-05-22 10:14:30 +08:00
    Marmot
        1
    Marmot  
       2018-05-21 16:30:44 +08:00
    可以的 666
    teaser
        2
    teaser  
       2018-05-21 16:44:00 +08:00
    真的骚
    CoCoMcRee
        3
    CoCoMcRee  
       2018-05-21 17:11:30 +08:00
    赞一个
    whx20202
        4
    whx20202  
       2018-05-21 17:16:56 +08:00
    能不能问一下怎么实现的呢?好奇怎么在图片中加入文字
    还有一开始没文字的图片是怎么弄的
    John60676
        5
    John60676  
       2018-05-21 17:29:27 +08:00
    @whx20202 PIL 自己去了解
    mashirozx
        6
    mashirozx  
       2018-05-21 17:47:56 +08:00 via Android
    敢问是只是单纯 pillow 拼了一下吗😛
    mandy0119
        7
    mandy0119  
       2018-05-21 18:11:35 +08:00
    字体有点小啊
    cheese
        8
    cheese  
       2018-05-21 18:19:04 +08:00   ❤️ 2
    高清的表情图没有灵魂
    littleshy
        9
    littleshy  
       2018-05-21 19:09:58 +08:00
    不居中一下吗?
    E1n
        10
    E1n  
       2018-05-21 22:13:51 +08:00
    这图片有点魔性啊 z
    Artists
        11
    Artists  
       2018-05-21 22:38:25 +08:00
    挺有趣的喔
    fushall
        12
    fushall  
       2018-05-22 08:02:57 +08:00
    2333333333 你太有才了
    37miao
        13
    37miao  
       2018-05-22 10:14:30 +08:00
    问下 js 版本的在哪呀,没搜到
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2704 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:34 · PVG 23:34 · LAX 08:34 · JFK 11:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.