日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
分享十個(gè)超級(jí)實(shí)用事半功倍的Python自動(dòng)化腳本

給照片添加水印

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

成都創(chuàng)新互聯(lián)公司專注于西藏企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。西藏網(wǎng)站建設(shè)公司,為西藏等地區(qū)提供建站服務(wù)。全流程定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

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))

檢測(cè)文本文件的相似性

很多時(shí)候我們需要來檢查兩文件的相似性,到底存在著多少的雷同,或許以下的這個(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")]))

修改照片的長與寬

我們要是想要修改照片的長度和寬度的話,下面的這個(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ì)于照片的其他操作

除了上面修改照片的長度與寬度之外,針對(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愛好者集中營 欣一")

我們?cè)賮砜匆幌露S碼的解析,代碼如下:

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

Decode_Qrcode("文件名")

制作一個(gè)簡單的網(wǎng)頁應(yīng)用

調(diào)用的是Python當(dāng)中的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)

分享標(biāo)題:分享十個(gè)超級(jí)實(shí)用事半功倍的Python自動(dòng)化腳本
分享地址:http://www.dlmjj.cn/article/ccdodgo.html