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

# encoding=utf-8import numpy as npimport matplotlib.pyplot as pltfrom PIL import Imageimport 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 functionhist = get_histogram(flat, 256)# execute the fncs = np.cumsum(hist)# numerator & denomenatornj = (cs - cs.min()) * 255N = cs.max() - cs.min()# re-normalize the cumsumcs = nj / N# cast it back to uint8 since we can't use floating point values in imagescs = cs.astype('uint8')# get the value from cumulative sum for every index in flat, and set that as img_newimg_new = cs[flat]# put array back into original shape since we flattened itimg_new = np.reshape(img_new, img.shape)# set up side-by-side image displayfig = plt.figure()fig.set_figheight(15)fig.set_figwidth(15)# display the real imagefig.add_subplot(1, 2, 1)plt.imshow(img, cmap='gray')plt.title("Image 'Before' Contrast Adjustment")# display the new imagefig.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 osimport zipfilefrom random import randrangedef 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 Converterimport 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 docdoc_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 Wordcv = Converter(pdf)#Path to the directorypath = 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 smtplibimport email# 负责结构文本from email.mime.text import MIMEText# 负责结构图片from email.mime.image import MIMEImage# 负责将多个对象集合起来from email.mime.multipart import MIMEMultipartfrom 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()# 设置发件人邮箱的域名和端口,端口地址为25stp.connect(mail_host, 25)  # set_debuglevel(1)能够打印出和SMTP服务器交互的所有信息stp.set_debuglevel(1)# 登录邮箱,传递参数1:邮箱地址,参数2:邮箱受权码stp.login(mail_sender,mail_license)# 发送邮件,传递参数1:发件人邮箱地址,参数2:收件人邮箱地址,参数3:把邮件内容格局改为strstp.sendmail(mail_sender, mail_receivers, mm.as_string())print("邮件发送胜利")# 敞开SMTP对象stp.quit()

5、解析和提取 HTML

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

# Parse and Extract HTML# pip install gazpachoimport gazpacho# Extract HTML from URLurl = 'https://www.example.com/'html = gazpacho.get(url)print(html)# Extract HTML with Headersheaders = {'User-Agent': 'Mozilla/5.0'}html = gazpacho.get(url, headers=headers)print(html)# Parse HTMLparse = gazpacho.Soup(html)# Find single tagstag1 = parse.find('h1')tag2 = parse.find('span')# Find multiple tagstags1 = parse.find_all('p')tags2 = parse.find_all('a')# Find tags by classtag = parse.find('.class')# Find tags by Attributetag = parse.find("div", attrs={"class": "test"})# Extract text from tagstext = parse.find('h1').texttext = parse.find_all('p')[0].text

6、二维码扫描仪

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

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

7、截图

# Grab Screenshot# pip install pyautogui# pip install Pillowfrom pyautogui import screenshotimport timefrom PIL import ImageGrab# Grab Screenshot of Screendef grab_screenshot():    shot = screenshot()    shot.save('my_screenshot.png')# Grab Screenshot of Specific Areadef grab_screenshot_area():    area = (0, 0, 500, 500)    shot = ImageGrab.grab(area)    shot.save('my_screenshot_area.png')# Grab Screenshot with Delaydef 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 PyPDF2from PyPDF2 import PdfFileReader as readerfrom gtts import gTTSdef 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 moviepyfrom 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.durationOutput.write_videofile("output.mp4", fps=30, codec='libx264')

后续有更好在补充》》