python 当做图床使用,读取粘贴板的图像保存上传,完成后将URL地址写入到剪贴板
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 当做图床使用,读取粘贴板的图像保存上传,完成后将URL地址写入到剪贴板相关的知识,希望对你有一定的参考价值。
#!/usr/local/bin/python2
# -*- coding: utf-8 -*-
# Author: Ficapy
# Create: '16/1/1'
import datetime
import os
import sys
import atexit
import requests
import imghdr
from cStringIO import StringIO
from hashlib import md5
from subprocess import call
from base64 import b64encode
from os.path import expanduser, exists, basename, getsize
NAME = '--------'
PWD = '---------'
URL = 'http://v0.api.upyun.com/'
BUCKET = 'ficapy'
DIR = 'blogimg' if sys.argv.__len__() > 1 else 'capture'
TINYPNG = 'Basic ' + b64encode('api:------------')
s = requests.Session()
if sys.argv.__len__() > 2:
sys.exit('argv error')
def capture():
try:
call(['/usr/local/bin/pngpaste', '-v'], stderr=open('/dev/null', 'w'))
except OSError:
print('please preinstall pngpaste use `brew install pngpaste` before use this script')
sys.exit()
file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S.png')
file_path = os.path.join('/tmp', file_name)
atexit.register(lambda x: os.remove(x) if os.path.exists(x) else None, file_path)
save = call(['/usr/local/bin/pngpaste', file_path])
if save == 1:
sys.exit()
return file_path, file_name
if sys.argv.__len__() == 1:
file_path, file_name = capture()
else:
# 任意字符代表捕获粘贴板
file_path = expanduser(sys.argv[-1])
if not exists(file_path):
file_path, file_name = capture()
else:
if imghdr.what(file_path) not in ['jpeg', 'jpg', 'png']:
sys.exit('file type error')
file_name = basename(file_path)
# 速度太慢,可以当CDN存在大量文件后使用桌面工具批量处理再上传
# def tinypng(file_path):
# # TODO 当超过限制的时候不使用压缩
# if getsize(file_path) > 4.5 * 1024 * 1024:
# sys.exit('file too large')
# with open(file_path) as f:
# r = s.post('https://api.tinify.com/shrink', headers={'Authorization': TINYPNG}, data=f).json()
# if 'error' in r:
# sys.exit(r['message'])
# url = r['output']['url']
# f = StringIO()
# for chunk in s.get(url, headers={'Authorization': TINYPNG}, stream=True).iter_content(chunk_size=1024):
# f.write(chunk)
# return f
# f = tinypng(file_path)
# f.seek(0, os.SEEK_END)
# size = f.tell()
# f.seek(0)
size = getsize(file_path)
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
# method,path,date,CONTENT_LENGTH,password
params = ['PUT', '/' + BUCKET + '/' + DIR + '/' + file_name, date, str(size), md5(PWD).hexdigest()]
sign = md5('&'.join(params)).hexdigest()
with open(file_path) as f:
url = URL + BUCKET + '/' + DIR + '/' + file_name
ret = requests.put(url, headers={'Authorization': 'UpYun ' + NAME + ':' + sign, 'Date': date}, data=f)
ret.raise_for_status()
ret = 'https://{0}.b0.upaiyun.com/{1}/{2}'.format(BUCKET, DIR, file_name)
call('echo {0} | tr -d "\n" | pbcopy'.format(ret), shell=True)
print(ret)
以上是关于python 当做图床使用,读取粘贴板的图像保存上传,完成后将URL地址写入到剪贴板的主要内容,如果未能解决你的问题,请参考以下文章
Java17读取粘贴板的内容
在图像中查找拼字游戏板的角点
Python基于 Pillow 的图像处理(零基础入门教程)
python读取图像的几种方式
opencv之python版图像的读取显示保存像素遍历以及结合matplotlib使用
Opencv-Python:图像尺寸图像的读取显示保存与复制