搜索
首页后端开发Python教程Python自动化脚本的几种应用场景

10个杀手级应用的Python自动化脚本

01、图像优化器

这个伟大的自动化脚本可以帮助你更好地处理图片,你可以像在Photoshop中一样编辑它们。

该脚本使用流行的Pillow模块。

# 图像优化
# pip install Pillow
import PIL
# 裁剪 
im = PIL.Image.open("Image1.jpg")
im = im.crop((34, 23, 100, 100))
# 调整大小
im = PIL.Image.open("Image1.jpg")
im = im.resize((50, 50))
# 翻转
im = PIL.Image.open("Image1.jpg")
im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
# 旋转
im = PIL.Image.open("Image1.jpg")
im = im.rotate(360)
# 压缩
im = PIL.Image.open("Image1.jpg")
im.save("Image1.jpg", optimize=True, quality=90)
# 模糊化
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.BLUR)
# 锐化
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.SHARPEN)
# 设置亮度
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Brightness(im)
im = im.enhance(1.5)
# 设置对比度
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Contrast(im)
im = im.enhance(1.5)
# 添加过滤器
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageOps.grayscale(im)
im = PIL.ImageOps.invert(im)
im = PIL.ImageOps. posterize(im, 4)
# 保存
im.save("Image1.jpg")

02、视频优化器

通过下面的自动化脚本,你不仅可以用Python来优化视频,还可以用它来优化图像。该脚本使用Moviepy模块,它允许你修剪、添加音频、设置视频速度、添加VFX等。

# 视频优化器
# pip install moviepy
import moviepy.editor as pyedit
# 加载视频
video = pyedit.VideoFileClip("vid.mp4")
# 修剪
vid1 = video.subclip(0, 10)
vid2 = video.subclip(20, 40)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# 加快视频的速度
final_vid = final_vid.speedx(2)
# 在视频中添加音频
aud = pyedit.AudioFileClip("bg.mp3")
final_vid = final_vid.set_audio(aud)
# 反转视频
final_vid = final_vid.fx(pyedit.vfx.time_mirror)
# 合并两个视频
vid1 = pyedit.VideoFileClip("vid1.mp4")
vid2 = pyedit.VideoFileClip("vid2.mp4")
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# 在视频中添加视觉特效
vid1 = final_vid.fx(pyedit.vfx.mirror_x)
vid2 = final_vid.fx(pyedit.vfx.invert_colors)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# 在视频中添加图像
img1 = pyedit.ImageClip("img1.jpg")
img2 = pyedit.ImageClip("img2.jpg")
final_vid = pyedit.concatenate_videoclips([img1, img2])
# 保存视频
final_vid.write_videofile("final.mp4")

03、将PDF转换为图像

这个小的自动化脚本可以很容易地检索整个PDF页面并将其转换为图像。该脚本使用了流行的PyMuPDF模块,该模块以其PDF文本提取而闻名。

# PDF to Images
# pip install PyMuPDF
import fitz
def pdf_to_images(pdf_file):
    doc = fitz.open(pdf_file)
    for p in doc:
        pix = p.get_pixmap()
        output = f "page{p.number}.png"
        pix.writePNG(output)
pdf_to_images("test.pdf")

04、获取API数据

如果你需要从数据库中获取API数据,或者需要向服务器发送API请求,这个自动化脚本是你的一个便利工具。使用Urlib3模块,你可以获取和发布API请求。

# pip install urllib3
输入urllib3
# 获取API数据
url = "https://api.github.com/users/psf/repos"
http = urllib3.PoolManager()
response = http.request('GET', url)
print(response.status)
print(response.data)
# 发布API数据
url = "https://httpbin.org/post"
http = urllib3.PoolManager()
response = http.request('POST', url, fields={'hello': 'world'})
print(response.status)

05、电池指示灯

这个方便的脚本允许你设置你想接收通知的电池百分比。该脚本使用Pyler进行通知,并使用Psutil来获取当前的电池百分比。

# 电池通知器
# pip instal plyer
from plyer import notification
import psutil
from time import sleep
while True:
   battery = psutil.sensors_battery()
    life = battery.percent
    #寿命 = 电池百分比
    if life < 50:
        notification.notify(
            title = "Battery Low" #电池电量不足
            message = "Please connect to power source",
            timeout = 10
        )
    sleep(60)

06、语法修正器

厌倦了校对你的长篇文章或文本?那么,你可以试试这个自动脚本,它将扫描你的文本并纠正语法错误。这个伟大的脚本使用了Happtransformer模块,它是一个机器学习模块,经过训练可以修正文本中的语法错误。

# Grammer Fixer
# pip install happytransformer
from happytransformer import HappyTextToText as HappyTTT
from happytransformer import TTSettings
def Grammer_Fixer(Text):
    Grammer = HappyTTT("T5","prithivida/grammar_error_correcter_v1")
    config = TTSettings(do_sample=True, top_k=10, max_length=100)
    corrected = Grammer.generate_text(Text, args=config)
    print("Corrected Text: ", corrected.text)
Text = "This is smple tet we how know this"
Grammer_Fixer(Text)

07、拼写纠正

这个伟大的脚本将帮助你纠正文本单词中的拼写错误。你可以找到下面的脚本,它将告诉你如何修正一个句子中的单个或多个单词。

# 拼写修正器
# pip 安装 textblob
# pip install textblob
from textblob import *
def fix_paragraph_words(paragraph):
    sentence = TextBlob(paragraph)
    correction = sentence.correct()
    print(correction)
# 修复字词拼写
def fix_word_spell(word):
    word = Word(word)
    更正 = word.correct()
    print(correction)
fix_paragraph_words("this is sammple tet!!")
fix_word_spell("maangoo")

08、互联网下载器

你可能使用下载软件从互联网上下载照片或视频,但现在你可以使用Python IDM模块创建自己的下载器。

# Python Downloader
# pip install internetdownloadmanager
import internetdownloadmanager as idm
def Downloader(url, output):
    pydownloader = idm.Downloader(worker=20,
                                part_size=1024*1024*10,
                                resumable=True,)
    pydownloader .download(url, output)
Downloader("Link url", "image.jpg")
Downloader("Link url", "video.mp4")

09、获取世界新闻

使用这个自动脚本,可以随时以任何国家/地区的任何语言更新每日的世界新闻。这个API允许你每天免费获得50条新闻。

# pip install requests
import requests
ApiKey = "YOUR_API_KEY"
url = "https://api.worldnewsapi.com/search-news?text=hurricane&api-key={ApiKey}"
headers = {
  &#39;Accept&#39;: &#39;application/json&#39;
}
response = requests.get(url, headers=headers)
print("News: ", response.json())

10、PySide2 GUI

这个自动化脚本将帮助你使用PySide2 Gui模块创建你的GUI应用程序。你可以在下面找到开始开发现代应用程序的前端所需的每一种方法。

# PySide2 
# pip install PySide2
from PySide6.QtWidgets import *
from PySide6.QtGui import *
app = QApplication(sys.argv)
window = QWidget()
# 调整窗口的大小
window.resize(500, 500)
# 设置窗口标题
window.setWindowTitle("PySide2 Window")
# 添加按钮
button = QPushButton("Click Me", window)
button.move(200, 200)
# 添加标签文本
label = QLabel("Hello Medium", window)
label.move(200, 150)
# 添加输入框
input_box = QLineEdit(window)
input_box.move(200, 250)
print(input_box.text())
# 添加单选按钮
radio_button = QRadioButton("Radio Button", window)
radio_button.move(200, 300)
# 添加复选框
checkbox = QCheckBox("Checkbox", window)
checkbox.move(200, 350)
# 添加滑块
slider = QSlider(window)
slider.move(200, 400)
# 添加进度条
progress_bar = QProgressBar(window)
progress_bar.move(200, 450)
# 添加图片 
image = QLabel(window)
image.setPixmap(QPixmap("image.png"))
# 添加消息框
msg = QMessageBox(window)
msg.setText("Message Box")
msg.setStandardButtons(QMessageBox.OK | QMessageBox.Cancel)
window.show()
sys.exit(app.exec())

以上是Python自动化脚本的几种应用场景的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
Python的科学计算中如何使用阵列?Python的科学计算中如何使用阵列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何处理同一系统上的不同Python版本?您如何处理同一系统上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

与标准Python阵列相比,使用Numpy数组的一些优点是什么?与标准Python阵列相比,使用Numpy数组的一些优点是什么?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

阵列的同质性质如何影响性能?阵列的同质性质如何影响性能?Apr 25, 2025 am 12:13 AM

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

编写可执行python脚本的最佳实践是什么?编写可执行python脚本的最佳实践是什么?Apr 25, 2025 am 12:11 AM

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

Numpy数组与使用数组模块创建的数组有何不同?Numpy数组与使用数组模块创建的数组有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

Numpy数组的使用与使用Python中的数组模块阵列相比如何?Numpy数组的使用与使用Python中的数组模块阵列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模块与Python中的数组有何关系?CTYPES模块与Python中的数组有何关系?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能