一起用python批量制作表情包吧,使用imgflip API

已收录   阅读次数: 1,136
2021-02-1219:30:40 发表评论
摘要

使用python这门编程语言,能够快速生成各种有意思的项目,极大的提高了生产力,比如爬虫,YouTube下载器等等,具体可以参看下方的前情提要,而这次为大家介绍的是用python生成表情包,也就是meme,而且非常简单,迅速,分享给大家……

分享至:
一起用python批量制作表情包吧,使用imgflip API

开篇寄语

使用python这门编程语言,能够快速生成各种有意思的项目,极大的提高了生产力,比如爬虫,YouTube下载器等等,具体可以参看下方的前情提要,而这次为大家介绍的是用python生成表情包,也就是meme,而且非常简单,迅速,分享给大家。

前情提要

内容详情

本篇用到了python的两个库,分别是:

  • requests
  • fake_useragent(选用或者不选用随意)

需要用到一个网站的api,那就是https://imgflip.com/,各种有意思的meme图可以从上面找到,今天需要用到他家的api,所以先进行注册。

一起用python批量制作表情包吧,使用imgflip API

注册成功后,记号账号和密码,用到该网站的api就需要用到的。

该网站的api是这个:

打开这个api,可以看到热门meme的数据,截图如下:

一起用python批量制作表情包吧,使用imgflip API

根据这个api,咱们筛选出这列出的100个模板名称,以方便后面制作meme。

打开pycharm等IDE输入以下代码:

import requests
import urllib
from fake_useragent import UserAgent

ua = UserAgent()#伪装成浏览器

#Fetch the available memes
data = requests.get('https://api.imgflip.com/get_memes').json()['data']['memes']
images = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in data]

#List all the memes
print('Here is the list of available memes : \n')
i = 1
for img in images:
    print(i,img['name'])
    i+=1

可以得到目前的top100热门meme表情,如下图所示:

一起用python批量制作表情包吧,使用imgflip API

这样输出出来的就是表情meme的名称了,那就第一个好了。

一起用python批量制作表情包吧,使用imgflip API

根据文中所示,需要填写相关信息,接着在下面写好,随便写点什么:

id = int(input('Enter the serial number of the meme : '))
text0 = input('Enter first text : ')
text1 = input('Enter second text : ')

填写成果如下图所示:

一起用python批量制作表情包吧,使用imgflip API
URL = 'https://api.imgflip.com/caption_image'
params = {
    'username':"保密",
    'password':"保密",#请用自己的
    'template_id':images[id-1]['id'],
    'text0':text0,
    'text1':text1
}
response = requests.request('POST',URL,params=params).json()
print(response)

在IDE打印出来的效果如下图所示:

一起用python批量制作表情包吧,使用imgflip API

其中url的地址就是生成的图片,如下图所示。

一起用python批量制作表情包吧,使用imgflip API

此时,可以直接将图片保存到本地,当然,也可以在IDE中是实现保存。

abc = response["data"]["url"]
req = requests.get(abc)
with open("demo.jpg", "wb") as f:
    f.write(req.content)

运行后,就可以在本地看到保存的图片了,如下图所示。

一起用python批量制作表情包吧,使用imgflip API

完整代码如下所示:

import requests
import urllib
from fake_useragent import UserAgent

ua = UserAgent()

#Fetch the available memes
data = requests.get('https://api.imgflip.com/get_memes').json()['data']['memes']
images = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in data]

#List all the memes
print('Here is the list of available memes : \n')
i = 1
for img in images:
    print(i,img['name'])
    i+=1

id = int(input('Enter the serial number of the meme : '))
text0 = input('Enter first text : ')
text1 = input('Enter second text : ')

URL = 'https://api.imgflip.com/caption_image'
params = {
    'username':"保密",
    'password':"保密",#请用自己的
    'template_id':images[id-1]['id'],
    'text0':text0,
    'text1':text1
}
response = requests.request('POST',URL,params=params).json()
abc = response["data"]["url"]
req = requests.get(abc)
with open("demo.jpg", "wb") as f:
    f.write(req.content)

此网站上有大量的meme供你使用,尽情快速创作吧。

温馨提示

伯衡君又发现一个更快速的创建方法,可以参看下面的这篇文章:

  • 我的微信
  • 微信扫一扫加好友
  • weinxin
  • 我的微信公众号
  • 扫描关注公众号
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: