分享10个超级实用事半功倍的Python自动化脚本
Python
高级脚本,帮助我们减少无谓的时间浪费,提高工作学习中的效率。给照片添加水印
给照片添加水印的代码多种多样,下面这种的或许是最为简单的形式,
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def watermark_Image(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 = '2.png'
watermark_Image(img, 'watermarked_2.jpg','Python爱好者集中营', pos=(10, 10))
检测文本文件的相似性
from difflib import SequenceMatcher
def file_similarity_checker(f1, f2):
with open(f1, errors="ignore") as file1, open(f2, errors="ignore") as file2:
f1_data = file1.read()
f2_data = file2.read()
checking = SequenceMatcher(None, f1_data, f2_data).ratio()
print(f"These files are {checking*100} % similar")
file_1 = "路径1"
file_2 = "路径2"
file_similarity_checker(file_1, file_2)
对文件内容进行加密
from cryptography.fernet import Fernet
def encrypt(filename, key):
fernet = Fernet(key)
with open(filename, 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open(filename, 'wb') as enc_file:
enc_file.write(encrypted)
key = Fernet.generate_key()
filename = "file.txt"
encrypt(filename, key)
def decrypt(filename, key):
fernet = Fernet(key)
with open(filename, 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted)
with open(filename, 'wb') as dec_file:
dec_file.write(decrypted)
decrypt(filename, key)
import pyAesCrypt
def Encryption(input_file_path, output_file_path, key):
pyAesCrypt.encryptFile(input_file_path, output_file_path, key)
print("File has been decrypted")
def Decryption(input_file_path, output_file_path, key):
pyAesCrypt.decryptFile(input_file_path, output_file_path, key)
print("File has been decrypted")
将照片转换为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")]))
修改照片的长与宽
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)
对于照片的其他操作
img = Image.open('1.jpg')
blur = img.filter(ImageFilter.BLUR)
blur.save('output.jpg')
照片翻转90度
img = Image.open('1.jpg')
rotate = img.rotate(90)
rotate.save('output.jpg')
照片进行锐化的处理
img = Image.open('1.jpg')
sharp = img.filter(ImageFilter.SHARPEN)
sharp.save('output.jpg')
照片左右对称翻转,代码如下
img = Image.open('1.jpg')
transpose = img.transpose(Image.FLIP_LEFT_RIGHT)
transpose.save('output.jpg')
照片进行灰度处理
img = Image.open('1.jpg')
convert = img.convert('L')
convert.save('output.jpg')
测试网速
当然我们在开始测网速之前需要提前下载好依赖的模块
pip install speedtest-cli
然后我们开始尝试测试一下网速
from speedtest import Speedtest
def Testing_Speed(net):
download = net.download()
upload = net.upload()
print(f'下载速度: {download/(1024*1024)} Mbps')
print(f'上传速度: {upload/(1024*1024)} Mbps')
print("开始网速的测试 ...")
net = Speedtest()
Testing_Speed(net)
货币汇率的转换
例如我们想要看一下美元与英镑之间的汇率转换,100美元可以换成多少的英镑,代码如下
# 导入模块
from currency_converter import CurrencyConverter
from datetime import date
# 案例一
conv = CurrencyConverter()
c = conv.convert(100, 'USD', 'GBP')
print(round(c, 2)) # 保留两位小数
或者我们想要看一下美元与欧元之间的汇率转换,100美元可以换成多少的欧元,
# 案例二
c = conv.convert(100, 'USD', 'EUR', date=date(2022, 3, 30))
print(round(c, 2)) # 44.1
生成二维码
其中包括了二维码的生成以及二维码的解析,代码如下
import qrcode
from PIL import Image
from pyzbar.pyzbar import decode
def Generate_qrcode(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,)
qr.add_data(data)
qr.make(fit=True)
image = qr.make_image(fill_color="black", back_color="white")
image.save("qrcode.png")
Generate_qrcode("Python爱好者集中营 欣一")
我们再来看一下二维码的解析,代码如下
def Decode_Qrcode(file_name):
result = decode(Image.open(file_name))
print("Data:", result[0][0].decode())
Decode_Qrcode("文件名")
制作一个简单的网页应用
调用的是Python
当中的flask
模块来制作网页应用,代码如下
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello World!"
@app.route("/python")
def test():
return "欢迎来到Python爱好者集中营,欣一"
if __name__ == "__main__":
app.run(debug=True)
任何疑问,欢迎留言或添微信上给我消息
我们的文章到此就结束啦~记得点赞
如何找到我: