pic_to_qiniu.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:useful_script 作者: JyHu 项目源码 文件源码
def upload(img, need_zip):
    if os.path.exists(img) and os.path.isfile(img):
        if imghdr.what(img):
            if need_zip:
                try:
                    o_img = img + '.ori'
                    if not os.path.isfile(o_img) or not imghdr.what(o_img):     # ?????????????????
                        print('???? ?', img) 
                        s_img = tinify.from_file(img)
                        s_img.to_file(img + '.z')
                        os.rename(img, img + '.ori')
                        os.rename(img + '.z', img)
                except Exception as e:
                    print('??????')
            rstr = str(time.time())+''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 12))
            key = md5(rstr.encode('utf-8')).hexdigest()   # ??????????
            mime_type = 'image/%s' % img[img.rfind('.') + 1:]
            token = q.upload_token(bucket, key)
            ret, info = put_file(token, key, img, mime_type=mime_type, check_crc=True)
            if ret['key'] == key and ret['hash'] == etag(img): print('?????' + 'http://' + domain + '/' +key)
            else :t('????')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号