用 chevereto 图床的小伙伴快进来,有福利~

2017-01-02 18:06:29 +08:00
 fffflyfish

嗯,今天本来想好好学习别人的代码的,突然看到 requests 模块,想想貌似这模块很常用的说,我竟然没用过,于是我就找了下文档看了下,嗯,光看不练是不对的,然后我就想做点什么,突然想到我每次写 hexo blog 的时候,每次要把图片截取保存到本地,然后上传到我的图床,然后拷贝链接, OMG 好烦啊,于是发现这篇文档 , 是时候造个轮子了。

下面是代码

#coding=utf-8
import requests
import json
import mimetypes
from PIL import ImageGrab
import datetime

def upload(files):
    APIKey = "YOUR API KEY"
    format = "json"
    url = "http://domain.com/api/1/upload/?key="+ APIKey + "&format=" + format
    #files = 
    r = requests.post(url , files = files)

    return json.loads(r.text)

def formatSource(filename):
    imageList = []
    type = mimetypes.guess_type(filename)[0]
    imageList.append(('source' , (filename , open(filename , 'rb') , type)))
    print imageList
    return imageList

if __name__ == "__main__":
    print "将图片截图或复制到剪切板中即可~~, ctrl+z 结束"
    recentVal = None
    while(True):
        tmpValue = ImageGrab.grabclipboard()
        if recentVal != tmpValue:
            recentVal = tmpValue
            now = datetime.datetime.now()
            now = now.strftime("%Y-%m-%d %H:%M:%S")
            filename = 'IMG'+ now + '.png'
            if recentVal is not None:
                recentVal.save(filename, 'png')
                #filenames.append(filename)
                #recentVal = None
                print filename
                jsonData = upload(formatSource(filename))

                if jsonData['status_code'] != 200:
                    print "error: " , jsonData['error']['message']
                    print "status code : " , jsonData['status_code']
                else:
                    print "orignal url: " , jsonData['image']['display_url']
                    print "thumb url: " , jsonData['image']['thumb']['url']

运行示例

有点慢啊,因为我的服务器在国外~~

使用方法

将你的 chevereto 的 API key 拷贝到对应位置,替换 domain.com 为你自己的域名,嗯,就这样,这只是练习代码,难免有些 bug ,你懂得

其实讲道理用curl就可以弄的,但是我不会把本地的图片传到服务器上,据说要base64编码~~~sad

嗯,开年第一个轮子,大家新年快乐!

11155 次点击
所在节点    Python
18 条回复
Showfom
2017-01-02 18:19:00 +08:00
可以加上 sm.ms 图床嘛 嘻嘻
misaka19000
2017-01-02 18:22:15 +08:00
挺有意思的,赞一个
lyanforever
2017-01-02 18:29:51 +08:00
等我有了图床
fffflyfish
2017-01-02 18:38:20 +08:00
@Showfom

```py
#coding=utf-8
import requests
import json
import mimetypes
from PIL import ImageGrab
import datetime

def upload(files):
APIKey = "YOUR API KEY"
format = "json"
#url = "http://domain.com/api/1/upload/?key="+ APIKey + "&format=" + format
url = "https://sm.ms/api/upload?ssl=False&format=json"
#files =
r = requests.post(url , files = files)

return json.loads(r.text)

def formatSource(filename):
imageList = []
type = mimetypes.guess_type(filename)[0]
imageList.append(('smfile' , (filename , open(filename , 'rb') , type)))
print imageList
return imageList

if __name__ == "__main__":
print "将图片截图或复制到剪切板中即可~~, ctrl+z 结束"
recentVal = None
while(True):
tmpValue = ImageGrab.grabclipboard()
if recentVal != tmpValue:
recentVal = tmpValue
now = datetime.datetime.now()
now = now.strftime("%Y-%m-%d %H:%M:%S")
filename = 'IMG'+ now + '.png'
if recentVal is not None:
recentVal.save(filename, 'png')
#filenames.append(filename)
#recentVal = None
print filename
jsonData = upload(formatSource(filename))

if jsonData['code'] != "success":
print "error: " , jsonData['msg']
print "status code : " , jsonData['code']
else:
print "url: " , jsonData['data']['url']
#print "orignal url: " , jsonData['image']['display_url']
#print "thumb url: " , jsonData['image']['thumb']['url']
```

临时改了改,亲测可以用
fffflyfish
2017-01-02 18:39:32 +08:00
回复不支持 markdown 吗。。。。
fffflyfish
2017-01-02 19:11:19 +08:00
Showfom
2017-01-02 19:33:18 +08:00
zrj766
2017-01-02 21:18:04 +08:00
好东西,研究下
mingyun
2017-01-02 23:29:53 +08:00
buseni
2017-06-22 14:05:11 +08:00
怎么实现的呢,怎么可以上传到别的网站上面呢,我现在发现上传到自己网站图片太大了
tianwm
2017-08-28 23:01:45 +08:00
@buseni 看来要别人的 api 给你
qcloud
2017-10-10 13:49:08 +08:00
emmm 没有 mac
fffflyfish
2017-10-10 14:41:08 +08:00
@qcloud windows 上有 sharex 可以直接配置,linux 的 Mac 的方式一样
qcloud
2017-10-10 15:04:52 +08:00
@fffflyfish #13 咦,你居然在呢,这个...chevereto 你知道怎么自定义图片的尺寸吗?
fffflyfish
2017-10-10 15:19:57 +08:00
@qcloud 自定义图片尺寸?不是上传一张图片就可以得到这张图片的多个尺寸的链接吗?如果是自定义这个尺寸的话“仪表盘”的“图片上传”里可以定义
qcloud
2017-10-10 15:21:22 +08:00
@fffflyfish #15 嗯,这个我看过了,但是对于我的问题似乎无解-.-,这个图片可以自定义吗?我的意思是,每张图片上传的路径是不一样
fffflyfish
2017-10-10 15:32:30 +08:00
@qcloud 路径不都是默认上传到 images 目录下吗?我还是没理解你图片自定义的意思,你的意思是 chevereto 的“图片上传”里自定义图片尺寸这个功能不能用吗?
qcloud
2017-10-10 15:37:21 +08:00
@fffflyfish 能用但是不适合我的需求

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/331736

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX