自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

十個Python自動化腳本,日常工作更輕松

開發(fā) 前端
你是否有過需要為網(wǎng)站或社交媒體優(yōu)化圖像,但又不想打開Photoshop的情況?現(xiàn)在只需一個簡單的Python腳本,在強大的Pillow模塊的幫助下,你可以輕松完成圖片的調(diào)整大小、裁剪、銳化等多種操作,非常簡單省力。

Python 在自動化領(lǐng)域的表現(xiàn)堪稱一流,其強大的自動化能力能夠大幅簡化開發(fā)者的日常工作。

本文精選了10個Python腳本,幫助你輕松自動化日常工作流程,提升效率。

1 圖像優(yōu)化器:告別Photoshop

你是否有過需要為網(wǎng)站或社交媒體優(yōu)化圖像,但又不想打開Photoshop的情況?現(xiàn)在只需一個簡單的Python腳本,在強大的Pillow模塊的幫助下,你可以輕松完成圖片的調(diào)整大小、裁剪、銳化等多種操作,非常簡單省力。

下面的腳本展示了如何進行裁剪、調(diào)整大小、翻轉(zhuǎn)、旋轉(zhuǎn)、調(diào)節(jié)對比度、模糊、銳化以及濾鏡處理。

# 圖像優(yōu)化
from PIL import Image, ImageFilter, ImageOps, ImageEnhance

# 加載圖像
im = Image.open("Image1.jpg")

# 裁剪圖像
im = im.crop((34, 23, 100, 100))

# 調(diào)整圖像大小
im = im.resize((50, 50))

# 水平翻轉(zhuǎn)圖像
im = im.transpose(Image.FLIP_LEFT_RIGHT)

# 旋轉(zhuǎn)圖像360度
im = im.rotate(360)

# 壓縮圖像
im.save("Image1.jpg", optimize=True, quality=90)

# 應(yīng)用模糊效果
im = im.filter(ImageFilter.BLUR)

# 應(yīng)用銳化效果
im = im.filter(ImageFilter.SHARPEN)

# 調(diào)整亮度
enhancer = ImageEnhance.Brightness(im)
im = enhancer.enhance(1.5)

# 調(diào)整對比度
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(1.5)

# 添加濾鏡
im = ImageOps.grayscale(im)
im = ImageOps.invert(im)
im = ImageOps.posterize(im, 4)

# 保存優(yōu)化后的圖像
im.save("Image1.jpg")

2 視頻優(yōu)化器:打造專業(yè)級視頻

這個視頻優(yōu)化器具備了所有基礎(chǔ)功能,比如剪輯、變速,以及通過MoviePy庫提供的一系列炫酷特效。

# 視頻優(yōu)化
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)

# 反轉(zhuǎn)視頻
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])

# 給視頻添加VFX
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")

3 郵件定時器:精準掌控郵件發(fā)送

這個郵件定時器能夠讓你設(shè)定特定時間自動發(fā)送郵件,確保你不會錯過任何重要郵件。它利用smtplib來發(fā)送郵件,并通過schedule庫來安排郵件的發(fā)送時間。

# 郵件定時器
import smtplib
import schedule
import time
def send_email():
    sender_email = "your_email@gmail.com"
    receiver_email = "recipient_email@gmail.com"
    password = "your_email_password"
    subject = "Automated Email"
    body = "This is an automated email sent using Python."
    message = f"Subject: {subject}\n\n{body}"
    with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message)
        
# 安排郵件在每天上午8點發(fā)送
schedule.every().day.at("08:00").do(send_email)
while True:
    schedule.run_pending()
    time.sleep(1)

4 社交媒體自動發(fā)布器:輕松管理多平臺發(fā)帖

這個Python自動化腳本不僅能幫你在各個平臺上發(fā)布內(nèi)容,還能輕松調(diào)整發(fā)帖的時間間隔。

# 社交媒體自動發(fā)布器
import tweepy
import schedule
import time
def post_to_twitter():
    api_key = "YOUR_API_KEY"
    api_secret = "YOUR_API_SECRET"
    access_token = "YOUR_ACCESS_TOKEN"
    access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"
    auth = tweepy.OAuthHandler(api_key, api_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    tweet = "This is an automated tweet using Python!"
    api.update_status(tweet)
    
# 安排每6小時發(fā)布一次推文
schedule.every(6).hours.do(post_to_twitter)
while True:
    schedule.run_pending()
    time.sleep(1)

5 將PDF轉(zhuǎn)換為圖像:無需復(fù)雜軟件

PDF文件雖然方便,但有時候我們需要將其轉(zhuǎn)換成圖像。無論是處理掃描文檔還是為演示文稿提取圖片,PyMuPDF都能幫你輕松實現(xiàn)。只需幾行代碼,就能將PDF頁面轉(zhuǎn)換成高質(zhì)量的圖像,簡單又高效。

# PDF轉(zhuǎn)圖像
import fitz
def pdf_to_images(pdf_file):
    doc = fitz.open(pdf_file)
    for page in doc:
        pix = page.get_pixmap()
        output = f"page{page.number}.png"
        pix.writePNG(output)
pdf_to_images("test.pdf")

6 獲取API數(shù)據(jù):簡化數(shù)據(jù)獲取

API無處不在,但手動提取數(shù)據(jù)是一件苦差事。這個腳本使用獲取API數(shù)據(jù)腳本自動化從Web API獲取數(shù)據(jù)。提取天氣、股票價格、GitHub倉庫——你說出來。這個腳本使用urllib3處理GET和POST請求。

# 獲取API數(shù)據(jù)
import urllib3

# 使用GET請求獲取API數(shù)據(jù)
url = "https://api.github.com/users/psf/repos"
http = urllib3.PoolManager()
response = http.request('GET', url)
print("Status Code:", response.status)
print("Response Data:", response.data)

# 使用POST請求發(fā)布API數(shù)據(jù)
url = "https://httpbin.org/post"
http = urllib3.PoolManager()
response = http.request('POST', url, fields={'hello': 'world'})
print("Status Code:", response.status)

7 電池監(jiān)控:時刻警惕電量變化

電池指示燈腳本會密切關(guān)注你的電池,在需要插入電源時提醒你充電。使用plyer和psutil,這個腳本確保你永遠不會錯過低電池警報。

# 電池提醒器
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 a power source",
            timeout=10
        )
    sleep(50)

8 網(wǎng)絡(luò)爬蟲:挖掘網(wǎng)絡(luò)寶藏

網(wǎng)絡(luò)爬蟲無疑是個強大的工具,但手動一頁頁瀏覽網(wǎng)頁就太費時費力了。這個腳本自動為你抓取和提取網(wǎng)頁上的數(shù)據(jù),利用requests和BeautifulSoup庫,幫你省去了數(shù)小時瀏覽工作、新聞或產(chǎn)品信息的麻煩。

# 網(wǎng)絡(luò)爬蟲腳本
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

# 從網(wǎng)站提取特定數(shù)據(jù)
data = soup.find("div", {"class": "content"}).get_text()
print(data)

9 Pytest:自動化測試,確保代碼質(zhì)量

自動化測試徹底改變了軟件開發(fā)的游戲規(guī)則。Pytest讓你能夠迅速編寫并執(zhí)行Python代碼的測試,確保一切功能都符合預(yù)期。這個腳本能夠自動對函數(shù)進行測試,并檢查你的代碼是否存在缺陷。

# 使用Pytest進行自動化測試
import pytest

# 要測試的函數(shù)
def add_numbers(x, y):
    return x + y
    
# 函數(shù)的測試用例
def test_addition():
    assert add_numbers(1, 2) == 3    assert add_numbers(-1, 1) == 0
    assert add_numbers(0, 0) == 0
    assert add_numbers(10, 5) == 15
if __name__ == "__main__":
    pytest.main()

10 文件備份和同步:確保文件安全

這個腳本將啟用兩個文件夾之間的文件備份和同步。任何一個文檔的任何修改都會自動更新到第二個文件夾。非常適合保持備份或在多個設(shè)備上工作。

# 文件備份和同步腳本
import os
import shutil
def backup_and_sync(source_folder, backup_folder):
    for root, _, files in os.walk(source_folder):
        for file in files:
            source_path = os.path.join(root, file)
            backup_path = os.path.join(backup_folder, root.replace(source_folder, ""), file)
            # 如果備份文件夾中不存在目錄,則創(chuàng)建目錄
            os.makedirs(os.path.dirname(backup_path), exist_ok=True)
            # 將文件復(fù)制到備份文件夾
            shutil.copy2(source_path, backup_path)
    # 刪除備份文件夾中不在源文件夾中的文件
    for root, _, files in os.walk(backup_folder):
        for file in files:
            backup_path = os.path.join(root, file)
            source_path = os.path.join(source_folder, root.replace(backup_folder, ""), file)
            if not os.path.exists(source_path):
                os.remove(backup_path)
source_folder = "path/to/source/folder"
backup_folder = "path/to/backup/folder"
backup_and_sync(source_folder, backup_folder)
責任編輯:武曉燕 來源: Python學研大本營
相關(guān)推薦

2024-07-01 18:07:30

Python腳本自動化

2024-08-14 14:42:00

2024-06-21 10:46:44

2022-10-09 14:50:44

Python腳本

2024-10-28 19:36:05

2024-12-10 07:15:00

2025-03-17 09:32:19

PythonExcel腳本

2022-05-07 14:08:42

Python自動化腳本

2025-02-07 12:58:33

python自動化腳本

2024-12-24 00:00:00

技巧日志框架

2022-09-22 07:00:31

PC手機存儲

2022-07-27 08:01:28

自動化DevOps

2021-04-23 22:44:57

Python開發(fā)辦公自動化

2024-08-19 10:21:37

接口Python魔法方法

2022-07-05 14:00:49

編排工具自動化

2022-01-11 06:53:23

腳本編碼Python

2024-05-13 16:29:56

Python自動化

2019-05-06 14:12:41

人工智能AI自動化

2024-11-13 13:14:38

2024-08-16 21:14:36

點贊
收藏

51CTO技術(shù)棧公眾號