banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

10 個順手級的 Python 自動化腳本

以下是 10 個實用的 Python 自動化腳本,適合各種需求。

  1. 圖片優化器
    使用 Pillow 模塊來編輯圖像。
# Image Optimizing
# pip install Pillow
import PIL

# Croping
im = PIL.Image.open("Image1.jpg")
im = im.crop((34, 23, 100, 100))

# Resizing
im = PIL.Image.open("Image1.jpg")
im = im.resize((50, 50))

# Flipping
im = PIL.Image.open("Image1.jpg")
im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)

# Rotating
im = PIL.Image.open("Image1.jpg")
im = im.rotate(360)

# Compressing
im = PIL.Image.open("Image1.jpg")
im.save("Image1.jpg", optimize=True, quality=90)

# Bluring
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.BLUR)

# Sharpening
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.SHARPEN)

# Set Brightness
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Brightness(im)
im = im.enhance(1.5)

# Set Contrast
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Contrast(im)
im = im.enhance(1.5)

# Adding Filters
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageOps.grayscale(im)
im = PIL.ImageOps.invert(im)
im = PIL.ImageOps.posterize(im, 4)

# Saving
im.save("Image1.jpg")
  1. 視頻優化器
    使用 MoviePy 模塊來優化視頻。
python
# Video Optimizer
# pip install moviepy
import moviepy.editor as pyedit

# Load the Video
video = pyedit.VideoFileClip("vid.mp4")

# Trimming
vid1 = video.subclip(0, 10)
vid2 = video.subclip(20, 40)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])

# Speed up the video
final_vid = final_vid.speedx(2)

# Adding Audio to the video
aud = pyedit.AudioFileClip("bg.mp3")
final_vid = final_vid.set_audio(aud)

# Reverse the Video
final_vid = final_vid.fx(pyedit.vfx.time_mirror)

# Merge two videos
vid1 = pyedit.VideoFileClip("vid1.mp4")
vid2 = pyedit.VideoFileClip("vid2.mp4")
final_vid = pyedit.concatenate_videoclips([vid1, vid2])

# Add VFX to Video
vid1 = final_vid.fx(pyedit.vfx.mirror_x)
vid2 = final_vid.fx(pyedit.vfx.invert_colors)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])

# Add Images to Video
img1 = pyedit.ImageClip("img1.jpg")
img2 = pyedit.ImageClip("img2.jpg")
final_vid = pyedit.concatenate_videoclips([img1, img2])

# Save the video
final_vid.write_videofile("final.mp4")
  1. PDF 轉圖片
    將 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")
  1. 獲取 API 數據
    從 API 獲取或發布數據。
# pip install urllib3
import urllib3

# Fetch API data
url = "https://api.github.com/users/psf/repos"
http = urllib3.PoolManager()
response = http.request('GET', url)
print(response.status)
print(response.data)

# Post API data
url = "https://httpbin.org/post"
http = urllib3.PoolManager()
response = http.request('POST', url, fields={'hello': 'world'})
print(response.status)
  1. 電池指示燈
    監控電池電量並發出通知。
# Battery Notifier
# pip install 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)
  1. 語法固定器
    自動糾正文本中的語法錯誤。
# Grammar 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)
  1. 拼寫修正
    糾正文本中的拼寫錯誤。
# Spell Fixer
# pip install textblob
from textblob import *

# Fixing Paragraph Spells
def fix_paragraph_words(paragraph):
    sentence = TextBlob(paragraph)
    correction = sentence.correct()
    print(correction)

# Fixing Words Spells
def fix_word_spell(word):
    word = Word(word)
    correction = word.correct()
    print(correction)

fix_paragraph_words("This is sammple tet!!")
fix_word_spell("maangoo")
  1. 互聯網下載器
    創建自己的下載器。
# 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")
  1. 獲取世界新聞
    獲取每日世界新聞。
# World News Fetcher
# pip install requests
import requests

ApiKey = "YOUR_API_KEY"
url = f"https://api.worldnewsapi.com/search-news?text=hurricane&api-key={ApiKey}"
headers = {
    'Accept': 'application/json'
}
response = requests.get(url, headers=headers)
print("News: ", response.json())
  1. PySide2 GUI
    創建 GUI 應用程序。
# PySide 2 
# pip install PySide2
from PySide6.QtWidgets import *
from PySide6.QtGui import *
import sys

app = QApplication(sys.argv)
window = QWidget()

# Resize the Window
window.resize(500, 500)

# Set the Window Title
window.setWindowTitle("PySide2 Window")

# Add Buttons
button = QPushButton("Click Me", window)
button.move(200, 200)

# Add Label Text
label = QLabel("Hello Medium", window)
label.move(200, 150)

# Add Input Box
input_box = QLineEdit(window)
input_box.move(200, 250)

# Add Radio Buttons
radio_button = QRadioButton("Radio Button", window)
radio_button.move(200, 300)

# Add Checkbox
checkbox = QCheckBox("Checkbox", window)
checkbox.move(200, 350)

# Add Slider
slider = QSlider(window)
slider.move(200, 400)

# Add Progress Bar
progress_bar = QProgressBar(window)
progress_bar.move(200, 450)

# Add Image 
image = QLabel(window)
image.setPixmap(QPixmap("image.png"))

# Add Message Box
msg = QMessageBox(window)
msg.setText("Message Box")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

window.show()
sys.exit(app.exec())
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。