# 创建函数 defget_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)
# 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
defzip_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制作计算器GUI
tkinter是python自带的GUI库,适合初学者练手创建小软件
import tkinter as tk
root = tk.Tk() # Main box window root.title("Standard Calculator") # Title shown at the title bar root.resizable(0, 0) # disabling the resizeing of the window
# Creating an entry field: 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)
defbuttonClick(num):# function for clicking temp = e.get( ) # temporary varibale to store the current input in the screen e.delete(0, tk.END) # clearing the screen from index 0 to END e.insert(0, temp + num) # inserting the incoming number input
defbuttonClear():# function for clearing e.delete(0, tk.END)
# 代码过长,部分略
4、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)