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
fffflyfish
V2EX  ›  Python

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

  •  
  •   fffflyfish ·
    FormatFish · 2017-01-02 18:06:29 +08:00 · 11121 次点击
    这是一个创建于 2642 天前的主题,其中的信息可能已经有所发展或是发生改变。

    嗯,今天本来想好好学习别人的代码的,突然看到 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

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

    18 条回复    2017-10-10 15:37:21 +08:00
    Showfom
        1
    Showfom  
       2017-01-02 18:19:00 +08:00 via iPhone
    可以加上 sm.ms 图床嘛 嘻嘻
    misaka19000
        2
    misaka19000  
       2017-01-02 18:22:15 +08:00
    挺有意思的,赞一个
    lyanforever
        3
    lyanforever  
       2017-01-02 18:29:51 +08:00
    等我有了图床
    fffflyfish
        4
    fffflyfish  
    OP
       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
        5
    fffflyfish  
    OP
       2017-01-02 18:39:32 +08:00
    回复不支持 markdown 吗。。。。
    fffflyfish
        6
    fffflyfish  
    OP
       2017-01-02 19:11:19 +08:00
    Showfom
        7
    Showfom  
       2017-01-02 19:33:18 +08:00
    zrj766
        8
    zrj766  
       2017-01-02 21:18:04 +08:00
    好东西,研究下
    mingyun
        9
    mingyun  
       2017-01-02 23:29:53 +08:00
    buseni
        10
    buseni  
       2017-06-22 14:05:11 +08:00
    怎么实现的呢,怎么可以上传到别的网站上面呢,我现在发现上传到自己网站图片太大了
    tianwm
        11
    tianwm  
       2017-08-28 23:01:45 +08:00
    @buseni 看来要别人的 api 给你
    qcloud
        12
    qcloud  
       2017-10-10 13:49:08 +08:00
    emmm 没有 mac
    fffflyfish
        13
    fffflyfish  
    OP
       2017-10-10 14:41:08 +08:00
    @qcloud windows 上有 sharex 可以直接配置,linux 的 Mac 的方式一样
    qcloud
        14
    qcloud  
       2017-10-10 15:04:52 +08:00
    @fffflyfish #13 咦,你居然在呢,这个...chevereto 你知道怎么自定义图片的尺寸吗?
    fffflyfish
        15
    fffflyfish  
    OP
       2017-10-10 15:19:57 +08:00
    @qcloud 自定义图片尺寸?不是上传一张图片就可以得到这张图片的多个尺寸的链接吗?如果是自定义这个尺寸的话“仪表盘”的“图片上传”里可以定义
    qcloud
        16
    qcloud  
       2017-10-10 15:21:22 +08:00
    @fffflyfish #15 嗯,这个我看过了,但是对于我的问题似乎无解-.-,这个图片可以自定义吗?我的意思是,每张图片上传的路径是不一样
    fffflyfish
        17
    fffflyfish  
    OP
       2017-10-10 15:32:30 +08:00
    @qcloud 路径不都是默认上传到 images 目录下吗?我还是没理解你图片自定义的意思,你的意思是 chevereto 的“图片上传”里自定义图片尺寸这个功能不能用吗?
    qcloud
        18
    qcloud  
       2017-10-10 15:37:21 +08:00 via iPhone
    @fffflyfish 能用但是不适合我的需求
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1005 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 19:48 · PVG 03:48 · LAX 12:48 · JFK 15:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.