Python中高级的自动化脚本有哪些
Admin 2022-09-03 群英技术资讯 888 次浏览
今天小编跟大家讲解下有关“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格式,或者将照片依次添加到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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是Python中的__new__和__init__的区别,对于__new__和__init__两者的区别及关联,有一些朋友不是很清楚,对此这篇文章就给大家来介绍一下,有需要的朋友接下来一起跟随小编看看吧。
这篇文章主要介绍了Python实战之实现简单的名片管理系统,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下
这篇文章主要介绍了如何从Python的cmd中获得.py文件参数操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
本篇文章给大家带来了关于Python的相关知识,其中主要整理了随机森林模型的相关问题,包括了集成模型简介、随机森林模型基本原理、使用sklearn实现随机森林模型等等内容,
在日常的工作和生活中,我们经常会遇到需要抠图的场景,即便是只有一张图片需要抠,也会抠得我们不耐烦。本文将为大家分享一个20行代码就能实现是批量抠图,需要的可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008