一些简单的实用程序,主要为图片处理
包括:
可以直接拖拽图片至py文件进行处理,也可以读取剪切板或手动输入文件路径进行处理
注意拖拽处理可能需要配置注册表的相关属性
import requests
import json
import os
import sys
import pyperclip
from PIL import Image
'''该程序需要联网利用api来压缩图片'''
URL = 'https://api.tinify.com/shrink'
'''此处填写你在tinify网站上申请的apikey'''
APIKEY = '{your_apikey}'
def getNewPic(picpath):
session = requests.Session()
session.auth=('api',APIKEY)
picdata = open(picpath,'rb').read()
re = session.request('POST', URL, data=picdata)
picurl = json.loads(re.text)['output']['URL']
newpicdata = requests.get(picurl).content
dstpath = '压缩_' + picpath
with open(dstpath, 'wb') as pic:
pic.write(newpicdata)
return picurl
def compress(picpath):
#获取文件名
picpath = os.path.basename(picpath)
if picpath.endswith('.bmp'):
print('无法压缩BMP格式文件',picpath)
if input('按回车则转化为png格式进行压缩') == '':
Image.open(picpath).save('格式转换_'+picpath.replace('.bmp', '.png'))
picpath = '格式转换_' + picpath.replace('.bmp', '.png')
else:
return
pyperclip.copy(getNewPic(picpath))
print(pyperclip.paste())
def main(*path):
for picpath in path:
compress(picpath)
if __name__ == '__main__':
args = sys.argv[1:]
if args: #尝试以拖拽在py文件上运行的文件路径执行函数,可批量拖拽处理
print('导入文件',*(f for f in args))
main(*args)
else: #否则直接输入或读取剪切板
path = input('原图片路径,回车读取剪切板:\n')
while path == '':
try:
path = pyperclip.paste()
except:
path = input('输入路径:\n')
main(path)
print('完成')
import os
import sys
import pyperclip
from PIL import Image
'''
此程序不直接通过Image库中的resize方法压缩大小,
而是计算相邻像素平均值来压缩图片
压缩的实际效果稍微好一点
'''
def getAvgColor(pixs,x,y):
#判断是否为灰度图片
if isinstance(pixs[0,0],int):
pix = 0
for i in range(x,x+2):
for j in range(y, y+2):
pix += pixs[i,j]
return pix//4
#彩色图片(rgb或rgba格式)处理
else:
pixlen = len(pixs[0,0])
pix = [0]*pixlen
for i in range(x,x+2):
for j in range(y, y+2):
for m in range(pixlen):
pix[m] += pixs[i,j][m]
for m in range(pixlen):
pix[m] = pix[m]//4
return tuple(pix)
def getNewPic(picpath, dstpath):
img = Image.open(picpath)
pixs = img.load()
w,h = img.size
newimg = img.resize((w//2, h//2)) #通过resize保留原始颜色属性
for x in range(w//2):
for y in range(h//2):
pix = getAvgColor(pixs,2*x,2*y)
newimg.putpixel((x,y),pix)
newimg.save(dstpath)
def main(*path):
for picpath in path:
dstpath = '尺寸压缩_'+os.path.basename(picpath)
getNewPic(picpath, dstpath)
if __name__ == '__main__':
args = tuple(sys.argv[1:])
if args: #尝试以拖拽在py文件上运行的文件路径执行函数,可批量拖拽处理
print('导入文件',*(f for f in args))
main(*args)
else: #否则直接输入或读取剪切板
path = input('原图片路径,回车读取剪切板:\n')
while path == '':
try:
path = pyperclip.paste()
except:
path = input('输入路径:\n')
main(path)
print('完成')
import json
import qrcode
import requests
import pyperclip
'''
以下提供两种方法生成二维码,
一是getqrcode,通过api接口获取,需要联网
二是makeqrcode,本地生成,无需联网
'''
#联图网http://www.liantu.com/pingtai/免费二维码生成api
def getqrcode(text, dstpath='qrcode.png'):
'''联网api获取'''
url = 'http://qr.liantu.com/api.php?text='
#依据api请求的要求,需要替换一些字符
text = text.replace('&', '%26').replace('\n', '%OA')
re =requests.get(url + text)
with open(dstpath, 'wb') as qr:
qr.write(re.content)
def makeqrcode(text, dstpath='qrcode.png'):
'''本地生成'''
pic= qrcode.make(text)
pic.save(dstpath)
if __name__ == '__main__':
text = input('输入内容,回车读取剪切板:\n')
if text == '':
text = pyperclip.paste()
print('读取剪切板内容: ',text)
makeqrcode(text)
#getqrcode(text)
print('完成')
json, requests, pyperclip, qrcode, Image(在PIL包中)
https://github.com/jinke18/py_utils/