お試し投稿1

import numpy as np
import cv2
import os
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
from PIL import Image,ImageTk
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

window = tk.Tk()
window.geometry("1000x650")
window.title("Showing images")

#create a canvas
canvas_size = (400,600)
canvas = tk.Canvas(bg = "gray",width = canvas_size[0],height = canvas_size[1])
canvas.place(x=0,y=0)

#Create a file selection button and select command
def File_select():
    select_window = tk.Tk()
    select_window.withdraw()
    fType = [("","*.jpg")]
    iDir = os.path.abspath(os.path.dirname(__file__))
    tk.messagebox.showinfo('program : Please select a file')
    file = tk.filedialog.askopenfilename(filetypes = fType,initialdir=iDir)
    tk.messagebox.showinfo('program',file)
    file = file.split('/')[-1]
    file_select_entry.delete(0,tk.END)
    file_select_entry.insert(tk.END,file)
    select_window.destroy()

file_select_entry = tk.Entry(window,width=20)
file_select_entry.place(x=canvas_size[0]+10,y=10)
file_select_button = tk.Button(window,text='Select File',command=File_select)
file_select_button.place(x=canvas_size[0]+200,y=10)

#Read the selected file and draw on canvas
def Read_file():
    filename = file_select_entry.get()
    dealing_image  = Image_file(filename)
    resized_image_tk = dealing_image.Given_width_resized_tk(canvas_size[0])
    canvas.create_image(0,0,image=resized_image_tk, anchor=tk.NW)
    window.mainloop()

read_button = tk.Button(window,text='Read',command=Read_file)
read_button.place(x=canvas_size[0]+300,y=10)

#Create a position Button
start_point_label = tk.Label(window,text='Start Point')
start_point_label.place(x=canvas_size[0]+10,y=50)
start_x_label = tk.Label(window,text='x')
start_x_label.place(x=canvas_size[0]+20,y=70)
start_x_entry = tk.Entry(window,width=7)
start_x_entry.place(x=canvas_size[0]+30,y=70)
start_y_label = tk.Label(window,text='y')
start_y_label.place(x=canvas_size[0]+120,y=70)
start_y_entry = tk.Entry(window,width=7)
start_y_entry.place(x=canvas_size[0]+130,y=70)
end_point_label = tk.Label(window,text='End Point')
end_point_label.place(x=canvas_size[0]+10,y=100)
end_x_label = tk.Label(window,text='x')
end_x_label.place(x=canvas_size[0]+20,y=120)
end_x_entry = tk.Entry(window,width=7)
end_x_entry.place(x=canvas_size[0]+30,y=120)
end_y_label = tk.Label(window,text='y')
end_y_label.place(x=canvas_size[0]+120,y=120)
end_y_entry = tk.Entry(window,width=7)
end_y_entry.place(x=canvas_size[0]+130,y=120)

#Create parameters input buttons

def clicked(event):
    filename = file_select_entry.get()
    image = cv2.imread(filename)
    px = int(event.x*image.shape[1]/canvas_size[0])
    py = int(event.y*image.shape[1]/canvas_size[0])
    start_x_entry.delete(0,tk.END)
    start_y_entry.delete(0,tk.END)
    start_x_entry.insert(tk.END,str(px))
    start_y_entry.insert(tk.END,str(py))
    canvas.delete('red_oval')
    canvas.create_oval(event.x-3,event.y-3,event.x+3,event.y+3,fill='red',tags='red_oval')
    window.mainloop()

def dragged(event):
    filename = file_select_entry.get()
    image = cv2.imread(filename)
    px = int(start_x_entry.get())
    py = int(start_y_entry.get())
    x1 = int(px*canvas_size[0]/image.shape[1])
    y1 = int(py*canvas_size[0]/image.shape[1])
    canvas.delete('rect')
    canvas.create_rectangle(x1,y1,event.x,event.y,tags='rect')
    window.mainloop()
    
def released(event):
    filename = file_select_entry.get()
    image = cv2.imread(filename)
    px = int(event.x*image.shape[1]/canvas_size[0])
    py = int(event.y*image.shape[1]/canvas_size[0])
    end_x_entry.delete(0,tk.END)
    end_y_entry.delete(0,tk.END)
    end_x_entry.insert(tk.END,str(px))
    end_y_entry.insert(tk.END,str(py))
    canvas.delete('blue_oval')
    canvas.create_oval(event.x-3,event.y-3,event.x+3,event.y+3,fill='blue',tags='blue_oval')
    create_graph()
    window.mainloop()
    
canvas.bind('<Button-1>',clicked)
canvas.bind('<B1-Motion>',dragged)
canvas.bind('<ButtonRelease-1>',released)
canvas.bind()

class Parameter_input:

    def __init__(self,window,label_text,default,position_x,position_y):

        self.window = window
        self.label_text = label_text
        self.default = default
        self.px = position_x
        self.py = position_y

    def set(self):

        self.label = tk.Label(self.window,text=self.label_text)
        self.label.place(x=self.px,y=self.py)
        self.entry = tk.Entry(self.window,width=7)
        self.entry.insert(0,str(self.default))
        self.entry.place(x=self.px+70,y=self.py)

parameter_label = tk.Label(window,text='Parameters')
parameter_label.place(x=canvas_size[0]+10,y=160)
num1 = Parameter_input(window,'num1',10,canvas_size[0]+20,190)
num1.set()
num2 = Parameter_input(window,'num2',10,canvas_size[0]+20,220)
num2.set()
num3 = Parameter_input(window,'num3',10,canvas_size[0]+20,250)
num3.set()

#Calculation Program
def calculation():
    return 0
    
calculation_button = tk.Button(window,text='Calculation',command=calculation)
calculation_button.place(x=canvas_size[0]+200,y=250)

#Graph space
def create_graph():
    
    filename = file_select_entry.get()
    image = cv2.imread(filename)
    image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    x1 = int(start_x_entry.get())
    y1 = int(start_y_entry.get())
    x2 = int(end_x_entry.get())
    y2 = int(end_y_entry.get())
    a = (y2-y1)/(x2-x1)

    x_span = np.array([i for i in range(x1,x2)])
    x_color = np.array([image[int(a*(i-x1)+y1)][i] for i in range(x1,x2)])
    y_span = np.array([i for i in range(y1,y2)])
    y_color = np.array([image[i][int(a*(i-y1)+x1)] for i in range(y1,y2)])
    
    
    figure = plt.figure(figsize=(6,3))
    y1 = np.array([np.sin(i) for i in range(100)])
    y2 = np.array([np.cos(i) for i in range(100)])
    ax1 = figure.add_subplot(1,2,1)
    ax2 = figure.add_subplot(1,2,2)
    ax1.plot(x_span,x_color)
    ax2.plot(y_span,y_color)
    graph_canvas = FigureCanvasTkAgg(figure,master=window)
    plot_widget = graph_canvas.get_tk_widget()
    plot_widget.place(x=canvas_size[0]+5,y=300)
    window.mainloop()

    
class Image_file:

    def __init__(self,filename):
        
        self.filename = filename
        self.image_bgr = cv2.imread(self.filename)
        self.image_rgb = cv2.cvtColor(self.image_bgr,cv2.COLOR_BGR2RGB)
        self.image_gray = cv2.cvtColor(self.image_bgr,cv2.COLOR_BGR2GRAY)
        self.image_pil = Image.fromarray(self.image_bgr)
        self.image_tk = ImageTk.PhotoImage(self.image_pil)

    def Given_width_resized_tk(self,width):
        self.width = width
        self.image_width = self.image_bgr.shape[1]
        self.scale = self.width/self.image_width
        self.resized_image_rgb = cv2.resize(self.image_rgb,dsize=None,fx=self.scale,fy=self.scale)
        self.resized_image_pil = Image.fromarray(self.resized_image_rgb)
        self.resized_image_tk = ImageTk.PhotoImage(self.resized_image_pil)
        return self.resized_image_tk

#Create destroy button
def cancel():
    window.quit()
    exit()
cancel = tk.Button(text='Terminate Program',command=cancel)
cancel.place(x=0,y=610)

window.mainloop()