|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我做了一个打开并查看图片的tkinter的界面如下
- from tkinter import *
- import tkinter as tk
- from tkinter import filedialog
- from PIL import Image, ImageTk
- import cv2
- import numpy as np
- import os
- if __name__ == "__main__":
- root = Tk()
- root.minsize(700,500)
- #setting up a tkinter canvas with scrollbars
- frame = Frame(root, bd=2, relief=SUNKEN)
- frame.grid_rowconfigure(0, weight=1)
- frame.grid_columnconfigure(0, weight=1)
- xscroll = Scrollbar(frame, orient=HORIZONTAL)
- xscroll.grid(row=1, column=0, sticky=E+W)
- yscroll = Scrollbar(frame)
- yscroll.grid(row=0, column=1, sticky=N+S)
- canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- canvas.grid(row=0, column=0, sticky=N+S+E+W)
- xscroll.config(command=canvas.xview)
- yscroll.config(command=canvas.yview)
- frame.pack(fill=BOTH,expand=1)
- #function to be called when mouse is clicked
- def printcoords():
- file = filedialog.askopenfilename(parent=root, initialdir="D:/", title='Choose an image.')
- filename = ImageTk.PhotoImage(Image.open(file))
- canvas.image = filename # <--- keep reference of your image
- canvas.create_image(0, 0, anchor='nw', image=filename)
- if file != '':
- lb.config(text = "您选择的文件是:"+file);
- return file
- print(file)
- lb = Label(root, text='')
- lb.pack()
- def co():
- str = ('python picture_cut.py')
- p = os.system(str)
- Button(root,text='选择图片',command=printcoords).pack()
- btn1=Button(root,text='计算',command=co)
- btn1.place(x=100, y=450)
- Button(root, text='清除').place(x=600, y=450)
- root.mainloop()
复制代码
我想在另外一个文件中导入 printcoords():函数中file这个文件路径应该如何导入下面这个文件的img = cv2.imread(file)中
- import cv2
- import numpy as np
- import os
- from asd import printcoords
- root = 'd:\\TensorFlow\\picture\\'
- dsize = 56 #归一化处理的图像大小
- [color=Red]img = cv2.imread([size=5]file[/size])[/color]
- data = np.array(img)
- len_x = data.shape[0]
- len_y = data.shape[1]
- min_val = 30#设置最小的文字像素高度,防止切分噪音字符
- start_i = -1
- end_i = -1
- rowPairs = [] #存放每行的起止坐标
- #行分割
- for i in range(len_x):
- if(not data[i].all() and start_i < 0):
- start_i = i
- elif(not data[i].all()):
- end_i = i
- elif (data[i].all() and start_i >= 0):
- #print(end_i - start_i)
- if(end_i - start_i >= min_val):
- rowPairs.append((start_i, end_i))
- start_i, end_i = -1, -1
- #print(rowPairs)
- #列分割
- start_j = -1
- end_j = -1
- min_val_word = 20 #最小文字像素长度
- number = 0 #分割后保存编号
- for start, end in rowPairs:
- for j in range(len_y):
- if(not data[start: end, j].all() and start_j < 0):
- start_j = j
- elif(not data[start: end, j].all()):
- end_j = j
- elif(data[start: end, j].all() and start_j >= 0):
- if(end_j - start_j >= min_val_word):
- #print(end_j - start_j)
- tmp = data[start: end, start_j: end_j]
- im2save = cv2.resize(tmp, (dsize,dsize)) #归一化处理
- cv2.imwrite(root + '%d.jpg' % number, im2save)
- number += 1
- #print("%d pic" % number)
- start_j, end_j = -1, -1
复制代码
感谢大佬 |
|