十個簡單使用的Python自動化腳本分享
在日常的工作學(xué)習(xí)當中,我們總會遇到各式各樣的問題,其中不少的問題都是一遍又一遍簡單重復(fù)的操作,不妨直接用Python腳本來自動化處理,今天小編就給大家分享十個Python高級腳本,幫助我們減少無謂的時間浪費,提高工作學(xué)習(xí)中的效率。
1.給照片添加水印
給照片添加水印的代碼多種多樣,下面這種的或許是最為簡單的形式:
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))2.檢測文本文件的相似性
很多時候我們需要來檢查兩文件的相似性,到底存在著多少的雷同,或許以下的這個腳本文件可以派得上用場:
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)3.對文件內(nèi)容進行加 密
有時候我們手中文件的內(nèi)容十分的重要、十分地機密,我們可以選擇對此進行加密,代碼如下:
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)我們生成密鑰,然后對文件內(nèi)容進行加密,當然這個密鑰后面在對文件進行解密的時候會派上用場,因此密鑰一定要保存完好,解密的代碼如下:
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)上面的腳本,其中的密鑰是一個隨機生成的隨機數(shù),當然密鑰也可以是我們自己指定的,代碼如下:
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")4.將照片轉(zhuǎn)換為PDF
有時候我們需要將照片轉(zhuǎn)換成PDF格式,或者將照片依次添加到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")]))5.修改照片的長與寬
我們要是想要修改照片的長度和寬度的話,下面的這個代碼可以幫得上忙,代碼如下:
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)6.對于照片的其他操作
除了上面修改照片的長度與寬度之外,針對照片我們還有其他的操作,例如模糊化照片的內(nèi)容:
img = Image.open('1.jpg')
blur = img.filter(ImageFilter.BLUR)
blur.save('output.jpg')照片翻轉(zhuǎn)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')照片左右對稱翻轉(zhuǎn),代碼如下:
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')7.測試網(wǎng)速
當然我們在開始測網(wǎng)速之前需要提前下載好依賴的模塊
pip install speedtest-cli
然后我們開始嘗試測試一下網(wǎng)速:
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("開始網(wǎng)速的測試 ...")
net = Speedtest()
Testing_Speed(net)8.貨幣匯率的轉(zhuǎn)換
例如我們想要看一下美元與英鎊之間的匯率轉(zhuǎn)換,100美元可以換成多少的英鎊,代碼如下:
# 導(dǎo)入模塊 from currency_converter import CurrencyConverter from datetime import date # 案例一 conv = CurrencyConverter() c = conv.convert(100, 'USD', 'GBP') print(round(c, 2)) # 保留兩位小數(shù)
或者我們想要看一下美元與歐元之間的匯率轉(zhuǎn)換,100美元可以換成多少的歐元:
# 案例二 c = conv.convert(100, 'USD', 'EUR', date=date(2022, 3, 30)) print(round(c, 2)) # 44.1
9.生成二維碼
其中包括了二維碼的生成以及二維碼的解析,代碼如下:
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("文件名")10.制作一個簡單的網(wǎng)頁應(yīng)用
調(diào)用的是Python當中的flask模塊來制作網(wǎng)頁應(yīng)用,代碼如下:
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)到此這篇關(guān)于十個簡單使用的Python自動化腳本分享的文章就介紹到這了,更多相關(guān)Python自動化腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch中torch.argmax()函數(shù)使用及說明
這篇文章主要介紹了Pytorch中torch.argmax()函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Pycharm在創(chuàng)建py文件時,自動添加文件頭注釋的實例
今天小編就為大家分享一篇Pycharm在創(chuàng)建py文件時,自動添加文件頭注釋的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python使用JSON模塊進行數(shù)據(jù)處理(編碼解碼)
這篇文章主要為大家介紹了python使用JSON模塊進行數(shù)據(jù)處理編碼解碼的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
分享15?個python中的?Scikit-Learn?技能
這篇文章主要介紹了分享15?個python中的?Scikit-Learn?技能,Scikit-Learn?是一個非常棒的?python?庫,用于實現(xiàn)機器學(xué)習(xí)模型和統(tǒng)計建模,有降維、特征選擇、特征提取、集成技術(shù)等特征,下文相關(guān)內(nèi)容需要的朋友可以參考一下2022-03-03

