搜尋
首頁後端開發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 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您應該檢查腳本是否使用錯誤的Python版本執行?您應該檢查腳本是否使用錯誤的Python版本執行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。