关于python:Python脚本梳理

44次阅读

共计 6414 个字符,预计需要花费 17 分钟才能阅读完成。

1、应用 PIL、Matplotlib、Numpy 对含糊老照片进行修复

# encoding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os.path
# 读取图片
img_path = "E:\\test.jpg"
img = Image.open(img_path)

# 图像转化为 numpy 数组
img = np.asarray(img)
flat = img.flatten()

# 创立函数
def get_histogram(image, bins):
    # array with size of bins, set to zeros
    histogram = np.zeros(bins)
    # loop through pixels and sum up counts of pixels
    for pixel in image:
        histogram[pixel] += 1
    # return our final result
    return histogram

# execute our histogram function
hist = get_histogram(flat, 256)

# execute the fn
cs = np.cumsum(hist)

# numerator & denomenator
nj = (cs - cs.min()) * 255
N = cs.max() - cs.min()

# re-normalize the cumsum
cs = nj / N

# cast it back to uint8 since we can't use floating point values in images
cs = cs.astype('uint8')

# get the value from cumulative sum for every index in flat, and set that as img_new
img_new = cs[flat]

# put array back into original shape since we flattened it
img_new = np.reshape(img_new, img.shape)

# set up side-by-side image display
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(15)

# display the real image
fig.add_subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.title("Image'Before'Contrast Adjustment")

# display the new image
fig.add_subplot(1, 2, 2)
plt.imshow(img_new, cmap='gray')
plt.title("Image'After'Contrast Adjustment")
filename = os.path.basename(img_path)

# plt.savefig("E:\\" + filename)
plt.show()

2、将文件批量压缩,应用 zipfile 库

import os
import zipfile
from random import randrange


def zip_dir(path, zip_handler):
    for root, dirs, files in os.walk(path):
        for file in files:
            zip_handler.write(os.path.join(root, file))


if __name__ == '__main__':
    to_zip = input("""
Enter the name of the folder you want to zip
(N.B.: The folder name should not contain blank spaces)
>
""")
    to_zip = to_zip.strip() + "/"
    zip_file_name = f'zip{randrange(0,10000)}.zip'
    zip_file = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
    zip_dir(to_zip, zip_file)
    zip_file.close()
    print(f'File Saved as {zip_file_name}')

3、PDF 转换为 Word 文件

应用 pdf2docx 库,能够将 PDF 文件转为 Word 格局

from pdf2docx import Converter
import os 
import sys

# Take PDF's path as input 
pdf = input("Enter the path to your file:")
assert os.path.exists(pdf), "File not found at,"+str(pdf)
f = open(pdf,'r+')

#Ask for custom name for the word doc
doc_name_choice = input("Do you want to give a custom name to your file ?(Y/N)")

if(doc_name_choice == 'Y' or doc_name_choice == 'y'):
    # User input
    doc_name = input("Enter the custom name :")+".docx"
    
else:
    # Use the same name as pdf
    # Get the file name from the path provided by the user
    pdf_name = os.path.basename(pdf)
    # Get the name without the extension .pdf
    doc_name =  os.path.splitext(pdf_name)[0] + ".docx"

    
# Convert PDF to Word
cv = Converter(pdf)

#Path to the directory
path = os.path.dirname(pdf)

cv.convert(os.path.join(path, "", doc_name) , start=0, end=None)
print("Word doc created!")
cv.close()

4、Python 主动发送邮件

应用 smtplib 和 email 库能够实现脚本发送邮件

import smtplib
import email
# 负责结构文本
from email.mime.text import MIMEText
# 负责结构图片
from email.mime.image import MIMEImage
# 负责将多个对象集合起来
from email.mime.multipart import MIMEMultipart
from email.header import Header

# SMTP 服务器, 这里应用 163 邮箱
mail_host = "smtp.163.com"
# 发件人邮箱
mail_sender = "******@163.com"
# 邮箱受权码, 留神这里不是邮箱明码, 如何获取邮箱受权码, 请看本文最初教程
mail_license = "********"
# 收件人邮箱,能够为多个收件人
mail_receivers = ["******@qq.com","******@outlook.com"]

mm = MIMEMultipart('related')

# 邮件主题
subject_content = """Python 邮件测试"""
# 设置发送者, 留神严格遵守格局, 外面邮箱为发件人邮箱
mm["From"] = "sender_name<******@163.com>"
# 设置接受者, 留神严格遵守格局, 外面邮箱为接受者邮箱
mm["To"] = "receiver_1_name<******@qq.com>,receiver_2_name<******@outlook.com>"
# 设置邮件主题
mm["Subject"] = Header(subject_content,'utf-8')

# 邮件注释内容
body_content = """你好,这是一个测试邮件!"""
# 结构文本, 参数 1:注释内容,参数 2:文本格式,参数 3:编码方式
message_text = MIMEText(body_content,"plain","utf-8")
# 向 MIMEMultipart 对象中增加文本对象
mm.attach(message_text)

# 二进制读取图片
image_data = open('a.jpg','rb')
# 设置读取获取的二进制数据
message_image = MIMEImage(image_data.read())
# 敞开方才关上的文件
image_data.close()
# 增加图片文件到邮件信息当中去
mm.attach(message_image)

# 结构附件
atta = MIMEText(open('sample.xlsx', 'rb').read(), 'base64', 'utf-8')
# 设置附件信息
atta["Content-Disposition"] = 'attachment; filename="sample.xlsx"'
# 增加附件到邮件信息当中去
mm.attach(atta)

# 创立 SMTP 对象
stp = smtplib.SMTP()
# 设置发件人邮箱的域名和端口,端口地址为 25
stp.connect(mail_host, 25)  
# set_debuglevel(1) 能够打印出和 SMTP 服务器交互的所有信息
stp.set_debuglevel(1)
# 登录邮箱,传递参数 1:邮箱地址,参数 2:邮箱受权码
stp.login(mail_sender,mail_license)
# 发送邮件,传递参数 1:发件人邮箱地址,参数 2:收件人邮箱地址,参数 3:把邮件内容格局改为 str
stp.sendmail(mail_sender, mail_receivers, mm.as_string())
print("邮件发送胜利")
# 敞开 SMTP 对象
stp.quit()

5、解析和提取 HTML

此自动化脚本将帮忙你从网页 URL 中提取 HTML,而后还为你提供可用于解析 HTML 以获取数据的性能。


# Parse and Extract HTML
# pip install gazpacho
import gazpacho
# Extract HTML from URL
url = 'https://www.example.com/'
html = gazpacho.get(url)
print(html)
# Extract HTML with Headers
headers = {'User-Agent': 'Mozilla/5.0'}
html = gazpacho.get(url, headers=headers)
print(html)
# Parse HTML
parse = gazpacho.Soup(html)
# Find single tags
tag1 = parse.find('h1')
tag2 = parse.find('span')
# Find multiple tags
tags1 = parse.find_all('p')
tags2 = parse.find_all('a')
# Find tags by class
tag = parse.find('.class')
# Find tags by Attribute
tag = parse.find("div", attrs={"class": "test"})
# Extract text from tags
text = parse.find('h1').text
text = parse.find_all('p')[0].text

6、二维码扫描仪

领有大量二维码图像或只想扫描二维码图像,那么此自动化脚本将帮忙你。该脚本应用 Qrtools 模块,使你可能以编程形式扫描 QR 图像。

# Qrcode Scanner
# pip install qrtools
from qrtools import Qr
def Scan_Qr(qr_img):
    qr = Qr()
    qr.decode(qr_img)
    print(qr.data)
    return qr.data
print("Your Qr Code is:", Scan_Qr("qr.png"))

7、截图


# Grab Screenshot
# pip install pyautogui
# pip install Pillow
from pyautogui import screenshot
import time
from PIL import ImageGrab
# Grab Screenshot of Screen
def grab_screenshot():
    shot = screenshot()
    shot.save('my_screenshot.png')
# Grab Screenshot of Specific Area
def grab_screenshot_area():
    area = (0, 0, 500, 500)
    shot = ImageGrab.grab(area)
    shot.save('my_screenshot_area.png')
# Grab Screenshot with Delay
def grab_screenshot_delay():
    time.sleep(5)
    shot = screenshot()
    shot.save('my_screenshot_delay.png')

8、创立有声读物

将您的 PDF 书籍转换为有声读物,那么这是你的自动化脚本,它应用 GTTS 模块将你的 PDF 文本转换为音频。

# Create Audiobooks
# pip install gTTS
# pip install PyPDF2
from PyPDF2 import PdfFileReader as reader
from gtts import gTTS
def create_audio(pdf_file):
    read_Pdf = reader(open(pdf_file, 'rb'))
    for page in range(read_Pdf.numPages):
        text = read_Pdf.getPage(page).extractText()
        tts = gTTS(text, lang='en')
        tts.save('page' + str(page) + '.mp3')
create_audio('book.pdf')

9、视频水印

应用此自动化脚本为你的视频增加水印,该脚本应用 Moviepy,这是一个不便的视频编辑模块。在上面的脚本中,你能够看到如何增加水印并且能够自在应用它。

# Video Watermark with Python
# pip install moviepy
from moviepy.editor import *
clip = VideoFileClip("myvideo.mp4", audio=True) 
width,height = clip.size  
text = TextClip("WaterMark", font='Arial', color='white', fontsize=28)
set_color = text.on_color(size=(clip.w + text.w, text.h-10), color=(0,0,0), pos=(6,'center'), col_opacity=0.6)
set_textPos = set_color.set_pos(lambda pos: (max(width/30,int(width-0.5* width* pos)),max(5*height/6,int(100* pos))) )
Output = CompositeVideoClip([clip, set_textPos])
Output.duration = clip.duration
Output.write_videofile("output.mp4", fps=30, codec='libx264')

后续有更好在补充》》

正文完
 0