用python做一个简单软件

前言

这是一个课设,用python做一个扫描王软件

我主要做的GUI部分,记录分享一下。也是第一次用python做小软件,python的方便果然是名不虚传

遇到问题

1.python版本

下载了python3.7的编译器

由于最终软件要在win7上运行,即32位的,因此下载了python3.7的32位

打包后遇到问题:w10打包的不能在w7上运行----->下载python32位的解释器

在w10执行python代码,参考博客: https://blog.csdn.net/qq_27280237/article/details/84644900

2.opencv降级

参考博客: https://www.cnblogs.com/guobin-/p/10842486.html

3.安装打包软件pyinstaller

参考博客: https://blog.csdn.net/Nire_Yeyu/article/details/104683888/

https://blog.csdn.net/Nire_Yeyu/article/details/104683888/

https://www.cnblogs.com/xbblogs/p/9682708.html

##最终打包代码
pyinstaller -F -w -i 图片名.ico  文件名.py

4.输出的图片没法保存

有中文路径

软件效果

python代码

1.GUI部分
import PySimpleGUI as sg
import PIL.Image
import scanner_doee2
import cv2
import osimport numpy as npimport other
from other import convert_to_bytesfrom tkinter import *# 全局变量
mp_path = ['读取图像','原图处理','滤镜']
mp_key = ['原图处理', '图像翻转', '寻找轮廓','读取图像','img0']
choices = ('素描滤镜','复古滤镜','反色滤镜','边界滤镜', '模糊滤镜','不加滤镜','浮雕滤镜')
#缓存图片标号
# 原图/翻转后的图片  0
# 圈出轮廓的图 10
# 透视变换后 11
# 调整亮度和对比度后的图 2
# 添加滤镜后的图 3sg.theme('Light Blue 2')
layout1 = [[sg.Frame(layout=[[sg.Text('图像地址'), sg.Input(key='path_in'), sg.FileBrowse()],[sg.Button('读取图像')]], title='读图',title_color='blue')],[sg.Button('翻转调整'),sg.Button('矫正处理')],[sg.Frame(layout=[[sg.Button('手动调节'), sg.Button('自适应均衡化'), sg.Button('清空效果')],], title='亮度和对比度调节',title_color='blue')],[sg.Frame(layout = [[sg.Listbox(choices, size=(15, len(choices)), key='filter')],[sg.Button('滤镜处理')]],  title='滤镜', title_color='blue') ],[sg.Frame(layout=[[sg.Button('阈值调节')]],title='阈值', title_color='blue', relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],[sg.Frame(layout=[[sg.Radio('普通', "RADIO1",key='普通', default=True, size=(10, 1)), sg.Radio('拍书', "RADIO1",key='拍书'),sg.Radio('证书', "RADIO1", key='证件',default=False, size=(10, 1))]],title='应用场景', title_color='blue', relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],[sg.Frame(layout=[[sg.Text('保存地址'), sg.Input(key='path_out'),sg.FolderBrowse(target='path_out')],[sg.Button('输出图像')]], title='输出',title_color='blue')],]
layout2 = [[sg.Text('原图:')],[sg.Image(key='img0',size=(300,300))],[sg.Text('寻找到轮廓后的图:')],[sg.Image(key='img10',  size=(300, 300))],[sg.Text('位置矫正+裁剪后的图:')],[sg.Image(key='img11',size=(300, 300))]]
layout3=[ [sg.Text('调整后的图')],[sg.Image(key='img2',size=(500,500))]]layout = [[sg.Column(layout1, element_justification='c'), sg.VSeperator(),sg.Column(layout2, element_justification='c'),sg.Column(layout3, element_justification='c')]]
window = sg.Window('扫描王', layout)while (True):event, values = window.read()if event !=None:print(event,values)if event =='读取图像':path_in = values['path_in']path_save=os.path.dirname(path_in)img0=cv2.imread(path_in)print(path_save)scanner_doee2.varible(path_save)# orig =  img0  #备份原图# 重新设置图片的大小,以便对其进行处理:选择最佳维度,以便重要内容不会丢失# img0 = cv2.resize(img0, (1500, 880))cv2.imwrite(path_save+'/img0.jpg',img0)window['img0'].update(data=convert_to_bytes(path_in, (300,300)))if event =='翻转调整':img0=np.rot90(img0)cv2.imwrite(path_save + '/img0.jpg', img0)window['img0'].update(data=convert_to_bytes(path_save+'/img0.jpg', (300, 300)))if event=='矫正处理':img1=scanner_doee2.solve(img0)img2=img1img3=img1window['img10'].update(data=convert_to_bytes(path_save+'/img10.jpg', resize=(300,300)))window['img11'].update(data=convert_to_bytes(path_save+'/img11.jpg', resize=(300,300)))if event=='清空效果':img2=img1cv2.imwrite(path_save + '/img2.jpg', img2)window['img2'].update(data=convert_to_bytes(path_save+'/img2.jpg', resize=(500,500)))if event=='手动调节':img2=scanner_doee2.light(img2)cv2.imwrite(path_save + '/img2.jpg', img2)window['img2'].update(data=convert_to_bytes(path_save+'/img2.jpg', resize=(500,500)))if event=='自适应均衡化':img2=scanner_doee2.autoEqualHistColor(img2)cv2.imwrite(path_save + '/img2.jpg', img2)window['img2'].update(data=convert_to_bytes(path_save+'/img2.jpg', resize=(500,500)))if event=='滤镜处理':img3=img2ss=values['filter']print(ss,ss[0])if ss[0]=='复古滤镜':img3 = scanner_doee2.mirror2(img2)elif ss[0]=='素描滤镜':print(ss)img3 = scanner_doee2.mirror1(img2)elif ss[0] == '反色滤镜':print(ss)img3 = scanner_doee2.mirror3(img2)elif ss[0] == '边界滤镜':img3 = scanner_doee2.mirror4(img2)elif ss[0] == '浮雕滤镜':img3 = scanner_doee2.mirror5(img2,1)elif ss[0] == '模糊滤镜':img3 = scanner_doee2.mirror5(img2,2)elif ss[0]=='不加滤镜':img3=img2cv2.imwrite(path_save + '/img2.jpg', img3)window['img2'].update(data=convert_to_bytes(path_save + '/img2.jpg', resize=(500, 500)))if event=='阈值调节':img4=img2img4=scanner_doee2.yuzhi(img2)cv2.imwrite(path_save + '/img2.jpg', img4)window['img2'].update(data=convert_to_bytes(path_save + '/img2.jpg', resize=(500, 500)))if event=='输出图像':img5=cv2.imread(path_save + '/img2.jpg')h,w,c=img5.shape# A4 297*210mm# B5 250*176# 身份证 54*85.6if values['拍书']==True:scale = min(h/250,  w/176)img5=cv2.resize(img5,(int(176* scale), int(250* scale)))elif values['证件']==True:scale = min(h/54,  w/85.6)img5=cv2.resize(img5,(int(85.6* scale), int(54* scale)))elif values['普通']==True:img5 = cv2.imread(path_save + '/img2.jpg')cv2.imshow('output',img5)path_out=values['path_out']cv2.imwrite( path_out+ '/out.jpg', img5)# cv2.imwrite(path_save + '/img2.jpg', img5)if event == sg.WIN_CLOSED or event == 'Exit':break
2.算法部分
import cv2
import numpy as np
from math import sqrt
import cmath
from PIL import Image, ImageFilter
path_save='yes'
def varible(ss):global path_savepath_save=ssprint(path_save)def rectify(h):h = h.reshape((4,2))   #改变数组的形状,变成4*2形状的数组hnew = np.zeros((4,2), dtype = np.float32)  #创建一个4*2的零矩阵#确定检测文档的四个顶点add = h.sum(1)hnew[0] = h[np.argmin(add)]   #argmin()函数是返回最大数的索引hnew[2] = h[np.argmax(add)]diff = np.diff(h, axis = 1)  #沿着制定轴计算第N维的离散差值hnew[1] = h[np.argmin(diff)]hnew[3] = h[np.argmax(diff)]return hnew# 拟合曲线顶点的去中心化
def approxCenter(approx):sum_x,sum_y = 0,0approx_center = approx;for a in approx:sum_x = sum_x + a[0][0];sum_y = sum_y + a[0][1];avr_x = sum_x/len(approx);avr_y = sum_y/len(approx);for a in approx_center:a[0][0] = a[0][0] - avr_xa[0][1] = a[0][1] - avr_yreturn approx_center,avr_x,avr_y#将顶点极坐标化,返回极角
def approxTheta(approx):cn = complex(approx[0][0],approx[0][1])   #得到每个点相对中心的直角坐标r,theta = cmath.polar(cn)       #将直角坐标转为极坐标,得到极角return theta# 合并拟合多边形顶点中的相近点
# approx:拟合多边形(n维数组)
# M:距离阈值
def approxCombine(approx,M):del_indexs = []for i in range(len(approx)):if i not in del_indexs: #判断是否是已删点,如果是则跳过计算for j in range(i+1,len(approx)):if j not in del_indexs:     #判断是否是已删点,如果是则跳过计算#计算两点距离dis = sqrt((approx[i][0][0] - approx[j][0][0])**2 + (approx[i][0][1] - approx[j][0][1])**2)if dis < M :#将两个相近点,近似为中值点approx[i][0][0] = (approx[i][0][0] + approx[j][0][0])/2 approx[i][0][1] = (approx[i][0][1] + approx[j][0][1])/2 del_indexs.append(j)approx = np.delete(approx, del_indexs,0)     #删除多余的近似点approx,avr_x,avr_y = approxCenter(approx);   #将顶点去中心化,用于计算极坐标approx = sorted(approx, key = approxTheta, reverse = True)    #按照极角进行降序排序approx = np.array(approx)   #sorted返回list型,转换为ndarray# 恢复去中心的顶点for a in approx:a[0][0] = a[0][0] + avr_xa[0][1] = a[0][1] + avr_yreturn approx#伽马变换
#gamma > 1时,图像对比度增强
def gamma_trans(input_image, gamma):img_norm = input_image/255.0img_gamma = np.power(img_norm,gamma)*255.0img_gamma = img_gamma.astype(np.uint8)return img_gamma# 彩色直方图均衡(对比度增强)(效果一般)
def equalHistColor(img_in):b, g, r = cv2.split(img_in)b1 = cv2.equalizeHist(b)g1 = cv2.equalizeHist(g)r1 = cv2.equalizeHist(r)img_out = cv2.merge([b1,g1,r1])return img_out# 彩色伽马变换(对比度增强)(效果较好)
def gammaColor(img_in,gamma):b, g, r = cv2.split(img_in)b1 = gamma_trans(b,gamma)g1 = gamma_trans(g,gamma)r1 = gamma_trans(r,gamma)img_out = cv2.merge([b1,g1,r1])return img_out# 亮度调节,原理:将原图与一张全黑图像融合,调节融合的比例,即为亮度调节
# c为原图所占比例,c > 1时,亮度增强
def light_img(img1, c):rows, cols, channels = img1.shape# 新建全零(黑色)图片数组:np.zeros(img1.shape, dtype=uint8)blank = np.zeros([rows, cols, channels], img1.dtype)dst = cv2.addWeighted(img1, c, blank, 1-c, 0)   #两幅图像融合,当1-c小于0时,亮度增强return dstdef solve(image):# print(path_save)# path_save='C:/Users/53055/Desktop/pythonProject3'#创建原始图像的副本orig = image.copy()orig_w, orig_h, ch = orig.shape  # 读取大小#重新设置图片的大小,以便对其进行处理:选择最佳维度,以便重要内容不会丢失image = cv2.resize(image, (1500,880))orig_h_ratio = orig_h / 1500.0  # 保存缩放比例orig_w_ratio = orig_w / 880.0  # 保存缩放比例#对图像进行灰度处理,并进而进行行高斯模糊处理gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)blurred = cv2.GaussianBlur(gray, (5,5), 0)#使用canny算法进行边缘检测edged = cv2.Canny(blurred,0,50)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))edged = cv2.dilate(edged, kernel)  # 膨胀#创建canny算法处理后的副本orig_edged = edged.copy()#找到边缘图像中的轮廓,只保留最大的,并初始化屏幕轮廓#findContours()函数用于从二值图像中查找轮廓# RETR_LIST:寻找所有轮廓# CHAIN_APPROX_NONE:输出轮廓上所有的连续点contours, hierarchy = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)approxs = []for c in contours:p = cv2.arcLength(c, True)   #计算封闭轮廓的周长或者曲线的长度approx = cv2.approxPolyDP(c, 0.02*p, True)  #指定0.02*p精度逼近多边形曲线,这种近似曲线为闭合曲线,因此参数closed为Trueapprox_cmb = approxCombine(approx,60)   # 合并轮廓中相近的坐标点if len(approx_cmb) == 4:                 #如果是四边approxs.append(approx_cmb)      #该轮廓为可能的目标轮廓# 将轮廓的拟合多边型按面积大小降序排序approxs = sorted(approxs, key = cv2.contourArea, reverse = True)# 选取面积最大的四边形轮廓target = approxs[0]# 将轮廓映射到原图上for t in target:t[0][0] = t[0][0] * orig_h_ratiot[0][1] = t[0][1] * orig_w_ratio# 在原灰度图上绘制寻找到的目标四边形轮廓orig_marked = orig# all_approxs = cv2.cvtColor(temp, cv2.COLOR_GRAY2RGB)cv2.drawContours(orig_marked,[target],-1,(0,255,0),8)# cv2.imshow('orig_marked',orig_marked)# 保存圈出轮廓的图cv2.imwrite(path_save + '/img10.jpg', orig_marked)# for i in range(len(approxs)):#     cv2.drawContours(all_approxs,[approxs[i]],-1,(0,255,0),2)#将目标轮廓映射到800*800四边形(用于透视变换)approx = rectify(target)pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])# 透视变换# 使用gtePerspectiveTransform函数获得透视变换矩阵:approx是源图像中四边形的4个定点集合位置;pts2是目标图像的4个定点集合位置M = cv2.getPerspectiveTransform(approx, pts2)# 使用warpPerspective函数对源图像进行透视变换,输出图像dst大小为800*800dst = cv2.warpPerspective(orig, M, (800,800))# 进行位置校正、裁剪(透视变换)后的图像# cv2.imshow("trans",dst)cv2.imwrite(path_save + '/img11.jpg', dst)return dst# 彩色限制对比度自适应直方图均衡化(图像亮度均衡)
def autoEqualHistColor(img_in):b, g, r = cv2.split(img_in)clahe = cv2.createCLAHE(1,tileGridSize = (8,8))b1 = clahe.apply(b)g1 = clahe.apply(g)r1 = clahe.apply(r)img_out = cv2.merge([b1,g1,r1])return img_out# 手动调节亮度和对比度
def light(dst):data=[110,220]def l_c_regulate(x):l = cv2.getTrackbarPos('light', 'light & contrast regulate')gamma = cv2.getTrackbarPos('contrast', 'light & contrast regulate')lighted = light_img(img_lc_regulate, l / 100.0)  # 亮度调节gammaed = gammaColor(lighted, gamma / 100.0)  # gamma变换cv2.imshow("light & contrast regulate", gammaed)data=[l,gamma]return gammaedimg_lc_regulate = dst   # 复制原图cv2.namedWindow('light & contrast regulate')    #创建windowcv2.createTrackbar('light', 'light & contrast regulate', 110, 500, l_c_regulate)       #亮度滑动条cv2.createTrackbar('contrast', 'light & contrast regulate', 210, 500, l_c_regulate)    #对比度滑动条l_c_regulate(0)      #先运行一次回调函数while(1):k=cv2.waitKey(1)&0xFFif k==27:   #ECS键cv2.destroyWindow('light & contrast regulate')lighted = light_img(img_lc_regulate, data[0] / 100.0)  # 亮度调节gammaed = gammaColor(lighted, data[1] / 100.0)  # gamma变换breakreturn  gammaed# 素描滤镜
def mirror1(img_in):img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY)  # 转为灰度图img_in = cv2.equalizeHist(img_in)   # 直方图均衡化inv = 255- img_in   # 图像取反blur = cv2.GaussianBlur(inv, ksize=(5, 5), sigmaX=50, sigmaY=50)  # 高斯滤波res = cv2.divide(img_in, 255- blur, scale= 255)     #颜色减淡混合res = gamma_trans(res,2)    #伽马变换,增强对比度return res#复古滤镜(运行超级慢)
def mirror2(img_in):img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY)  # 转为灰度图im_color = cv2.applyColorMap(img_in, cv2.COLORMAP_PINK)return im_color# 反色滤镜
def mirror3(img_in):inv = 255- img_in   # 图像取反return inv# 边界滤镜(利用canny算子实现)
def mirror4(img_in):img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY)img_f = cv2.Canny(img_in,100,200)return  img_f# cv2.imshow('img_f',img_f)def mirror5(dst,type):img_f = Image.fromarray(cv2.cvtColor(dst,cv2.COLOR_BGR2RGB))if type ==1:img_f = img_f.filter(ImageFilter.EMBOSS)      #浮雕滤镜elif type==2:img_f = img_f.filter(ImageFilter.BLUR)      #模糊滤镜img_f = cv2.cvtColor(np.asarray(img_f),cv2.COLOR_RGB2BGR)return  img_f# # 以下为PIL库的部分滤镜效果## # OpenCV的图片格式转换成PIL.Image格式# img_f = Image.fromarray(cv2.cvtColor(dst,cv2.COLOR_BGR2RGB))## # 滤镜处理# # ImageFilter.BLUR 模糊滤镜# # ImageFilter.SHARPEN 锐化滤镜# # ImageFilter.SMOOTH  平滑滤镜# # ImageFilter.SMOOTH_MORE 平滑滤镜(阀值更大)# # ImageFilter.EMBOSS    浮雕滤镜# # ImageFilter.FIND_EDGES  边界滤镜# # ImageFilter.EDGE_ENHANCE    边界加强# # ImageFilter.EDGE_ENHANCE_MORE   边界加强(阀值更大)# # ImageFilter.CONTOUR   轮廓滤镜# img_f = img_f.filter(ImageFilter.EMBOSS)      #浮雕滤镜# # img_f = img_f.filter(ImageFilter.CONTOUR)     #素描滤镜# # img_f = img_f.filter(ImageFilter.FIND_EDGES)    #边界滤镜## # PIL.Image转换成OpenCV格式# img_f = cv2.cvtColor(np.asarray(img_f),cv2.COLOR_RGB2BGR)def yuzhi(img_in):# 二值化阈值调节示例# 关于二值化,用身份证照片测试时,全局阈值进行二值化效果还可以,但如果存在灰度不均匀,会出现部分信息缺失# OTSU自动阈值法的效果也不错(效果不错的前提是图像灰度均匀,本质是一种全局最佳阈值的方法,依旧存在全局阈值的缺点)# 使用区域自适应阈值时,对不同灰度的区域有很好的效果,但如果窗口过小,会导致噪点被放大,可以通过调节偏移阈值去除噪点# 窗口调大到一定值时,效果等同于使用全局阈值,因此最终使用区域自适应阈值方法进行二值化# demo中使用滑块调节自适应阈值窗口的size,# 关于消除噪点,尝试过高斯滤波、膨胀,效果不好data=[57,30]def bin_regulate(x):data[0] = cv2.getTrackbarPos('auto size', 'bin regulate')  # 自适应阈值窗口大小if data[0] == 0:data[0] = 1  # 窗口最小大小为3data[1] = cv2.getTrackbarPos('threshold', 'bin regulate')  # 自适应阈值偏移量# img_bin = cv2.GaussianBlur(img_bin, ksize=(3, 3), sigmaX=100, sigmaY=100)  #高斯滤波# 固定全局阈值二值化# ret,img_bin =  cv2.threshold(img_bin, t, 255, cv2.THRESH_BINARY)# OTSU自动阈值# ret,img_bin = cv2.threshold(img_bin, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)# 以下两种区域自适应阈值方法类似# 自适应阈值二值化(均值):第二个参数为领域内均值,第五个参数为规定正方形领域大小(11*11),第六个参数是常数C:阈值等于均值减去这个常数# img_bin = cv2.adaptiveThreshold(img_bin, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 2)# 自适应阈值二值化(高斯窗口)第二个参数为领域内像素点加权和,权重为一个高斯窗口,第五个参数为规定正方形领域大小(11*11),第六个参数是常数C:阈值等于加权值减去这个常数img_bin = cv2.adaptiveThreshold(img_bin_i, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 2 * data[0] + 1, data[1])# 膨胀# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))# img_bin = cv2.dilate(img_bin,kernel)  #膨胀# 中值滤波# img_bin = cv2.medianBlur(img_bin, 2*blur_size+1)cv2.imshow("bin regulate", img_bin)passimg_gray = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY)  # 转为灰度图img_bin_i = img_gray  # 复制灰度图cv2.namedWindow('bin regulate')  # 创建windowcv2.createTrackbar('auto size', 'bin regulate', 57, 400, bin_regulate)  # 自适应阈值的窗口size值cv2.createTrackbar('threshold', 'bin regulate', 30, 100, bin_regulate)  # 自适应阈值偏移量bin_regulate(0)  # 先运行一次回调函数while (1):k = cv2.waitKey(1) & 0xFFif k == 27:  # ECS键img_bin = cv2.adaptiveThreshold(img_bin_i, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,2 * data[0] + 1, data[1])cv2.destroyWindow('bin regulate')breakreturn img_bin# while (1):#     k = cv2.waitKey(1) & 0xFF##     if k == 27:  # ECS键#         cv2.destroyWindow('light & contrast regulate')#         img_bin = cv2.adaptiveThreshold(img_bin_i, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,#                                         2 * data[0] + 1, data[1])#         breakreturn img_bindef other():# 二值化# 对透视变换后的图像进行灰度处理img_gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)img_gray = gamma_trans(img_gray,1.2)    #伽马变换,增强对比度# 二值化阈值调节示例# 关于这个二值化,用身份证照片测试时,全局阈值进行二值化效果还可以,但如果存在灰度不均匀,会出现部分信息缺失# 使用区域自适应阈值时,对不同灰度的区域有很好的效果,但如果窗口过小,会有很多噪点被放大# 窗口调大到一定值时,效果等同于使用全局阈值,因此最终使用区域自适应阈值方法进行二值化# demo中使用滑块调节自适应阈值窗口的size# 为了消除噪点,尝试过高斯滤波、膨胀,效果不好# OTSU自动阈值法的效果也不错(效果不错的前提是图像灰度均匀,本质是一种全局最佳阈值的方法,依旧存在全局阈值的缺点)def bin_regulate(x):t = cv2.getTrackbarPos('auto size', 'bin regulate')if t == 0:t = 1   # 窗口最小大小为3# blur_size = cv2.getTrackbarPos('blursize', 'bin regulate')# img_bin = cv2.GaussianBlur(img_bin_regulate, ksize=(3, 3), sigmaX=100, sigmaY=100)  #高斯滤波# ret,img_bin =  cv2.threshold(img_bin_regulate, t, 255, cv2.THRESH_BINARY)    #进行固定阈值处理,得到二值图像img_bin = img_bin_regulate# 以下两种自适应阈值方法类似# 自适应阈值二值化(均值):第二个参数为领域内均值,第五个参数为规定正方形领域大小(11*11),第六个参数是常数C:阈值等于均值减去这个常数# img_bin = cv2.adaptiveThreshold(img_bin, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 2)# 自适应阈值二值化(高斯窗口)第二个参数为领域内像素点加权和,权重为一个高斯窗口,第五个参数为规定正方形领域大小(11*11),第六个参数是常数C:阈值等于加权值减去这个常数img_bin = cv2.adaptiveThreshold(img_bin,255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 2*t+1, 2)# OTSU自动阈值(效果还可以)# ret,img_bin = cv2.threshold(img_bin, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)# 膨胀# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))# img_bin = cv2.dilate(img_bin,kernel)  #膨胀cv2.imshow("bin regulate",img_bin)passimg_bin_regulate = img_gray     #复制灰度图cv2.namedWindow('bin regulate')    #创建windowcv2.createTrackbar('auto size', 'bin regulate', 1, 400, bin_regulate)     #自适应阈值的窗口size值# cv2.createTrackbar('blursize', 'bin regulate', 1, 100, bin_regulate)     #高斯滤波size滚动条bin_regulate(0)      #先运行一次回调函数# #对透视变换后的图像使用阈值进行约束获得扫描结果# # 使用固定阈值操作:threshold()函数:有四个参数:第一个是原图像,第二个是进行分类的阈值,第三个是高于(低于)阈值时赋予的新值,# # 第四个是一个方法选择参数:cv2.THRESH_BINARY(黑白二值)# # 该函数返回值有两个参数,第一个是retVal(得到的阈值值(在OTSU会用到)),第二个是阈值化后的图像# ret, th1 = cv2.threshold(dst, 132, 255, cv2.THRESH_BINARY)    #进行固定阈值处理,得到二值图像# # 使用Otsu's二值化,在最后一个参数加上cv2.THRESH_OTSU# ret2, th2 = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)# # 使用自适应阈值操作:adaptiveThreshold()函数# # 第二个参数为领域内均值,第五个参数为规定正方形领域大小(11*11),第六个参数是常数C:阈值等于均值减去这个常数# th3 = cv2.adaptiveThreshold(dst, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)# # 第二个参数为领域内像素点加权和,权重为一个高斯窗口,第五个参数为规定正方形领域大小(11*11),第六个参数是常数C:阈值等于加权值减去这个常数# th4 = cv2.adaptiveThreshold(dst,255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)#输出处理后的图像cv2.imshow("orig", orig)cv2.imshow("gray", gray)cv2.imshow("blurred", blurred)cv2.imshow("canny_edge", orig_edged)cv2.imshow("marked", image)# cv2.imshow("thre_constant", th1)# cv2.imshow("thre_ostu", th2)# cv2.imshow("thre_auto1", th3)# cv2.imshow("thre_auto2", th4)cv2.imshow("orig_mark", dst)# cv2.imwrite("orig.jpg",dst)# cv2.imshow('all-approxs',all_approxs)cv2.waitKey(0)cv2.destroyAllWindows()
3.辅助代码
import PIL.Image
import io
import base64global filenamedef convert_to_bytes(file_or_bytes, resize=None):'''Will convert into bytes and optionally resize an image that is a file or a base64 bytes object.Turns into  PNG format in the process so that can be displayed by tkinter:param file_or_bytes: either a string filename or a bytes base64 image object:type file_or_bytes:  (Union[str, bytes]):param resize:  optional new size:type resize: (Tuple[int, int] or None):return: (bytes) a byte-string object:rtype: (bytes)'''if isinstance(file_or_bytes, str):img = PIL.Image.open(file_or_bytes)else:try:img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))except Exception as e:dataBytesIO = io.BytesIO(file_or_bytes)img = PIL.Image.open(dataBytesIO)cur_width, cur_height = img.sizeif resize:new_width, new_height = resizescale = min(new_height/cur_height, new_width/cur_width)img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS)bio = io.BytesIO()img.save(bio, format="PNG")del imgreturn bio.getvalue()def save_pic(filename,type,id):mp_type = {'0': '原图翻转', '白元芳': 78, '狄仁杰': 82}

用python做一个简单GUI小软件相关推荐

  1. python小软件实例教程_【趣味案例】用Python做一个时光回忆录小软件,女朋友当场流泪说非我不嫁!...

    "时光回忆录",主要分为以下四部分,登录界面的设计,倒数日模块,照片模块以及甜言蜜语模块. 登录界面 因为照片,情话这种较为隐私的东西,我决定设计一定的登录门槛.但是与此同时,我给 ...

  2. 用python做一个简单的游戏,用python写一个小游戏

    大家好,本文将围绕如何用python做一个简单的小游戏展开说明,python编写的入门简单小游戏是一个很多人都想弄明白的事情,想搞清楚用python做一个简单的游戏需要先了解以下几个事情. 1.Pyt ...

  3. 利用Python做一个简单的对战小游戏

    利用Python做一个简单的文字对战小游戏 一.游戏介绍 1.大体介绍:文字版的对战小游戏,可以利用Python随机生成两个角色,角色带有各自的血量和攻击值两个指标.两人在对战时同时攻击对方,同时造成 ...

  4. 【Python】如何用python做一个简单的输入输出交互界面?

    看到知乎上有人在问,如何使用Python做一个简单的输入输出交互界面? 交互界面就涉及到GUI编程. Python有很多GUI框架,功能大同小异. 其中比较出名的有「PyQT」.**wxPython. ...

  5. python自己做个定时器_技术图文:如何利用 Python 做一个简单的定时器类?

    原标题:技术图文:如何利用 Python 做一个简单的定时器类? 背景 今天在B站上看有关 Python 最火的一个教学视频 -- "零基础入门学习 Python",这也是我们 P ...

  6. 老滚5初始化python失败_五分钟学会怎么用python做一个简单的贪吃蛇

    Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础上开发.我们今天将利用它来制作一款大家基本都玩过的小游戏--贪吃蛇. 一.需要导入的包 import pygame ...

  7. Python制作一个简单的抽奖软件(二)

    Python制作一个简单的抽奖软件(二) 认识QT 因为都对 QT和tkinter都没用过,之前简单使用tkinter后发现,界面调整不太好弄.然后度娘了之后,QT是强大GUI库之一,很多人都推荐它. ...

  8. 用c语言简单办法做一个字典_幼儿园手工,用废纸筒做一个简单的小蝴蝶,有教程...

    幼儿园的手工,除了用卡纸做各种简单的小制作外,纸筒也是常用的手工材料. 下面用纸筒做一个简单的小蝴蝶,做法很简单. 制作过程: 准备材料 废纸筒.剪刀.胶.水彩笔,纸板. 在纸筒上剪下五个圈圈 剪完的 ...

  9. 用Python代码做一个简单数字小游戏

    #作者是一个十三岁的小男孩. 编辑工具 电脑Python 需要模块 random #今日用代码做一个猜数小游戏 #话不多说,上代码!!! import random number = random.r ...

最新文章

  1. python爬取贴吧所有帖子-通过python爬取贴吧数据并保存为word
  2. Chisel_LLDB调试命令
  3. uniapp连接php,thinkphp5 对接手机uni-app的unipush推送(个推)
  4. android viewpager动态加载图片,Android使用ViewPager加载图片和轮播视频
  5. UI复习练习_优酷布局
  6. mysql可以存布尔_哪个MySQL数据类型用于存储布尔值
  7. python矩阵左除_matlab学习笔记
  8. mysql mongodb 事务_认识MongoDB 4.0的新特性——事务(Transactions)
  9. KnockoutJS(4)-控制文本和外观绑定
  10. Java测试题及答案(Java干货完整试卷)
  11. matlab的输出(命令窗口、fprint函数、disp函数)
  12. php开发自学经典.pdf_PHP从入门到精通(第4版) 明日科技 中文完整pdf扫描版[42MB]
  13. altera Cyclone V再认识
  14. Verdi快速使用技巧
  15. 2022年新一代最强开源UI自动化测试神器 ——Playwright(三)
  16. java随机获取字母,Java程序获取随机字母
  17. 老闪创业那些事儿(38)——小龙的离职面谈
  18. 常见的SQL面试题(学生表_课程表_成绩表_教师表)
  19. 初玩scrapy:爬取淘票票(1)
  20. GitFlow Workflow

热门文章

  1. visio的细的箭头
  2. 【第一个项目开发】创建domain包中的类
  3. 7-2 列出连通集 (25分)
  4. LTE网络PUCCH功控计算
  5. 中心计算机集中控制方式必须方式,「1」专科-8707信息技术与信息管理
  6. 读取.dwg文件“曲线救国”之读取.dxf
  7. 【全奖博士】香港大学招收计算机视觉方向博士/博后/研究助理
  8. 微信小程序添加音效createInnerAudioContext
  9. STM32F103单片机控制SG90舵机
  10. *6-3 节约小能手