打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
10 个杀手级 Python 自动化脚本
userphoto

2023.11.25 甘肃

关注

因此,请将本文保留在您的收藏夹中以备将来参考。在IT行业,程序员永远不会停止学习...

现在,让我们开始吧。

01、 图像优化器

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

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

# 图像优化

# 安装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 等。

# 视频优化器

# 安装moviepy库

# 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转换为图像
# 安装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 请求,此自动化脚本对您来说是一个方便的工具。使用 Urllib3 模块,您可以获取和发布 API 请求。

# 获取API数据

# 安装urllib3库

# pip install urllib3

import 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 获取当前电池百分比。

# 电池指示灯
# 安装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
# 安装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、拼写更正

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

# Spell Fixer
# 安装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')

08、互联网下载器

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

# Python Downloader
# 安装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 篇新闻文章。

#World News Fetcher
# 安装requests库
import requests
ApiKey = 'YOUR_API_KEY'
url = '
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())

10、PySide2 图形用户界面

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

# PySide 2

# 安装PySide2库

# pip install PySide2

from PySide6.QtWidgets import *

from PySide6.QtGui import *

import sys

app = QApplication(sys.argv)

window = QWidget()

# 调整窗口大小

window.resize(500, 500)

# 设置窗口标题

window.setWindowTitle('PySide2 窗口')

# 添加按钮

button = QPushButton('点击我', window)

button.move(200, 200)

# 添加标签文本

label = QLabel('你好,Medium', window)

label.move(200, 150)

# 添加输入框

input_box = QLineEdit(window)

input_box.move(200, 250)

print(input_box.text())

# 添加单选按钮

radio_button = QRadioButton('单选按钮', window)

radio_button.move(200, 300)

# 添加复选框

checkbox = QCheckBox('复选框', 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('消息框')

msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

window.show()

sys.exit(app.exec())

好了,今天的分享就到此为止。喜欢就给点个赞吧~

有什么问题可以评论区回复

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
10 个杀手级的 Python 自动化脚本
Python3制作图片缩略图
超全Python图像处理讲解(多图预警)
少为人知的Python图片处理
Python·Numpy库 | 常用函数大全(含注释)
30 行代码带你用 Python 在命令行查看图片
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服