您好,欢迎访问一九零五行业门户网

五个有趣的Python脚本

python可以玩的方向有很多,比如爬虫、预测分析、gui、自动化、图像处理、可视化等等,可能只需要十几行代码就能实现酷炫的功能。
因为python是动态脚本语言,所以代码逻辑比java要简要很多,实现同样的功能少写很多代码。而且python生态有众多的第三方工具库,把功能都封装在包里,只需要你调用接口,就能使用复杂的功能。
下面举几个简单好玩的脚本例子,初学者可以照着代码写写,能快速掌握python语法。
1、使用pil、matplotlib、numpy对模糊老照片进行修复
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) img = np.asarray(img) flat = img.flatten() def get_histogram(image, bins): histogram = np.zeros(bins) for pixel in image: histogram[pixel] += 1 return histogram hist = get_histogram(flat, 256) cs = np.cumsum(hist) nj = (cs - cs.min()) * 255 n = cs.max() - cs.min() cs = nj / n cs = cs.astype('uint8') img_new = cs[flat] img_new = np.reshape(img_new, img.shape) fig = plt.figure() fig.set_figheight(15) fig.set_figwidth(15) fig.add_subplot(1, 2, 1) plt.imshow(img, cmap='gray') plt.title(image 'before' contrast adjustment) 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.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、使用tkinter制作计算器guitkinter是python自带的gui库,适合初学者练手创建小软件
import tkinter as tk root = tk.tk() root.title(standard calculator) root.resizable(0, 0) e = tk.entry(root, width=35, bg='#f0ffff', fg='black', borderwidth=5, justify='right', font='calibri 15') e.grid(row=0, column=0, columnspan=3, padx=12, pady=12) def buttonclick(num): temp = e.get( ) e.delete(0, tk.end) e.insert(0, temp + num) def buttonclear(): e.delete(0, tk.end)
4、pdf转换为word文件使用pdf2docx库,可以将pdf文件转为word格式
from pdf2docx import converter import os import sys pdf = input(enter the path to your file: ) assert os.path.exists(pdf), file not found at, +str(pdf) f = open(pdf,'r+') 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'): doc_name = input(enter the custom name : )+.docx else: pdf_name = os.path.basename(pdf) doc_name =os.path.splitext(pdf_name)[0] + .docx cv = converter(pdf) path = os.path.dirname(pdf) cv.convert(os.path.join(path, , doc_name) , start=0, end=none) print(word doc created!) cv.close()
5、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 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 mm[to] = receiver_1_name,receiver_2_name mm[subject] = header(subject_content,'utf-8') body_content = 你好,这是一个测试邮件! message_text = mimetext(body_content,plain,utf-8) 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) stp = smtplib.smtp() stp.connect(mail_host, 25) stp.set_debuglevel(1) stp.login(mail_sender,mail_license) stp.sendmail(mail_sender, mail_receivers, mm.as_string()) print(邮件发送成功) stp.quit()
小结python还有很多好玩的小脚本,你可以根据自己的场景来编写,也可以使用现成的第三方库。
以上就是五个有趣的python脚本的详细内容。
其它类似信息

推荐信息