溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

有哪些超級(jí)實(shí)用的Python自動(dòng)化腳本

發(fā)布時(shí)間:2023-04-13 09:53:05 來(lái)源:億速云 閱讀:145 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“有哪些超級(jí)實(shí)用的Python自動(dòng)化腳本”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“有哪些超級(jí)實(shí)用的Python自動(dòng)化腳本”吧!

給照片添加水印

給照片添加水印的代碼多種多樣,下面這種的或許是最為簡(jiǎn)單的形式:

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愛(ài)好者集中營(yíng)', pos=(10, 10))
檢測(cè)文本文件的相似性

很多時(shí)候我們需要來(lái)檢查兩文件的相似性,到底存在著多少的雷同,或許以下的這個(gè)腳本文件可以派得上用場(chǎng):

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)
對(duì)文件內(nèi)容進(jìn)行加密

有時(shí)候我們手中文件的內(nèi)容十分的重要、十分地機(jī)密,我們可以選擇對(duì)此進(jìn)行加密,代碼如下:

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)

我們生成密鑰,然后對(duì)文件內(nèi)容進(jìn)行加密,當(dāng)然這個(gè)密鑰后面在對(duì)文件進(jìn)行解密的時(shí)候會(huì)派上用場(chǎng),因此密鑰一定要保存完好,解密的代碼如下:

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)

上面的腳本,其中的密鑰是一個(gè)隨機(jī)生成的隨機(jī)數(shù),當(dāng)然密鑰也可以是我們自己指定的,代碼如下:

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")
將照片轉(zhuǎn)換為PDF

有時(shí)候我們需要將照片轉(zhuǎn)換成PDF格式,或者將照片依次添加到PDF文件當(dāng)中去,代碼如下:

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")]))
修改照片的長(zhǎng)與寬

我們要是想要修改照片的長(zhǎng)度和寬度的話,下面的這個(gè)代碼可以幫得上忙,代碼如下:

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)
對(duì)于照片的其他操作

除了上面修改照片的長(zhǎng)度與寬度之外,針對(duì)照片我們還有其他的操作,例如模糊化照片的內(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')

照片進(jìn)行銳化的處理:

img = Image.open('1.jpg')
sharp = img.filter(ImageFilter.SHARPEN)
sharp.save('output.jpg')

照片左右對(duì)稱翻轉(zhuǎn),代碼如下:

img = Image.open('1.jpg')
transpose = img.transpose(Image.FLIP_LEFT_RIGHT)
transpose.save('output.jpg')

照片進(jìn)行灰度處理:

img = Image.open('1.jpg')
convert = img.convert('L')
convert.save('output.jpg')
測(cè)試網(wǎng)速

當(dāng)然我們?cè)陂_始測(cè)網(wǎng)速之前需要提前下載好依賴的模塊

pip install speedtest-cli

然后我們開始嘗試測(cè)試一下網(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)速的測(cè)試 ...")

net = Speedtest()
Testing_Speed(net)
貨幣匯率的轉(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
生成二維碼

其中包括了二維碼的生成以及二維碼的解析,代碼如下:

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愛(ài)好者集中營(yíng) 欣一")

我們?cè)賮?lái)看一下二維碼的解析,代碼如下:

def Decode_Qrcode(file_name):
result = decode(Image.open(file_name))
print("Data:", result[0][0].decode())

Decode_Qrcode("文件名")
制作一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)應(yīng)用

調(diào)用的是Python當(dāng)中的flask模塊來(lái)制作網(wǎng)頁(yè)應(yīng)用,代碼如下:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
return "Hello World!"

@app.route("/python")
def test():
return "歡迎來(lái)到Python愛(ài)好者集中營(yíng),欣一"

if __name__ == "__main__":
app.run(debug=True)

到此,相信大家對(duì)“有哪些超級(jí)實(shí)用的Python自動(dòng)化腳本”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI