ホームページ  >  記事  >  バックエンド開発  >  Python 自動化スクリプトのいくつかのアプリケーション シナリオ

Python 自動化スクリプトのいくつかのアプリケーション シナリオ

王林
王林転載
2023-05-07 21:22:191375ブラウズ

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 ページ全体を簡単に取得して画像に変換できます。このスクリプトは、PDF テキスト抽出で知られる人気のある PyMuPDF モジュールを使用します。

# 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. スペル修正

この素晴らしいスクリプトは、テキスト単語のスペル ミスを修正するのに役立ちます。文中の 1 つまたは複数の単語を修正する方法を説明するスクリプトを以下に示します。

# 拼写修正器
# 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 を使用すると、1 日あたり 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 アプリケーションを作成するのに役立ちます。最新のアプリケーションのフロントエンドの開発を開始するために必要なすべての方法を以下に示します。

rree

以上がPython 自動化スクリプトのいくつかのアプリケーション シナリオの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。