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('????')
评论列表
文章目录