书接上文,再奉上几个小工具,方便实用!!!
1. 图片添加水印
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def watermark(img_path,output_path, text, pos):
img = Image.open(img_path)
drawing = ImageDraw.Draw(img)
black = (10, 5, 12)
drawing.text(pos, text, fill=black)
img.show()
img.save(output_path)
img = '原图片.png'
watermark(img, '目标图片.jpg','Python都知道', pos=(10, 10))
2. 图片转素描
# 图像转换
import cv2
# 读取图片
img = cv2.imread("输入.jpg")
# 灰度
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
invert = cv2.bitwise_not(grey)
# 高斯滤波
blur_img = cv2.GaussianBlur(invert, (7, 7), 0)
inverse_blur = cv2.bitwise_not(blur_img)
sketch_img = cv2.divide(grey, inverse_blur, scale=256.0)
# 保存
cv2.imwrite('输出.jpg', sketch_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 图片转PDF
import os
import img2pdf
with open("Output.pdf", "wb") as file:
file.write(img2pdf.convert([i for i in os.listdir('文件路径') if i.endswith(".jpg")]))
4. 提取PDF表格
# 方法一
import camelot
tables = camelot.read_pdf("tables.pdf")
print(tables)
tables.export("extracted.csv", f="csv", compress=True)
# 方法二, 需要安装Java8
import tabula
tabula.read_pdf("tables.pdf", pages="all")
tabula.convert_into("table.pdf", "output.csv", output_format="csv", pages="all")
5. 拼写检查
# 拼写检查
# 方法一
import textblob
text = "mussage"
print("original text: " + str(text))
checked = textblob.TextBlob(text)
print("corrected text: " + str(checked.correct()))
# 方法二
import autocorrect
spell = autocorrect.Speller(lang='en')
# 以英语为例
print(spell('cmputr'))
print(spell('watr'))
print(spell('survice'))
6. 查看cpu温度
# 获取CPU温度
from time import sleep
from pyspectator.processor import Cpu
cpu = Cpu(monitoring_latency=1)
with cpu:
while True:
print(f'Temp: {cpu.temperature} °C')
sleep(2)
7. 裁剪图片
from PIL import Image
import os
def img_resize(file, h, w):
img = Image.open(file)
Resize = img.resize((h,w), Image.ANTIALIAS)
Resize.save('resized.jpg', 'JPEG', quality=90)
img_resize("文件路径", 400, 200)
8. 合并表格
import pandas as pd
# 文件名
filename = "test.xlsx"
# 表格数量
T_sheets = 5
df = []
for i in range(1, T_sheets+1):
sheet_data = pd.read_excel(filename, sheet_name=i, header=None)
df.append(sheet_data)
# 合并表格
output = "merged.xlsx"
df = pd.concat(df)
df.to_excel(output)