Creating an image splitting app with Tkinter

Creating an image splitting app with Tkinter

This time I will write an article about Tkinter.

I made a GUI that divides one image into four vertically and horizontally and saves the divided image.

Completed image Screenshot 2020-04-26 15.22.50.png

This time I wanted to click and save the image, so the image is displayed on the button Switch to another image in the folder with "Previous image" and "Next image" below.

Place the button.

python


self.Button = tkinter.Button(master,width=sep,height=sep,text='Unselected') #Generate a button.
self.Button["bd"] = "7"
self.Button["compound"] = "top"
self.Button["command"]=dvd_clicked
self.Button["image"]= self.image_tk
self.Button.grid(row=0, column=0)

compound = character position when the image is placed command = event when the button is pressed image = Image to be displayed

Event

def dvd_clicked():
            self.Button["text"] = "Choice"
            self.Button["fg"] = "RED"
            self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'0.jpg', quality=95)

Here, the image is saved when each button is clicked. In order to save each part, I added location information such as 0_1 and saved the file.

After selecting the selected area, the text will be "selected" in red.

If you press "Previous image" or "Next image", each image will be updated.

Whole code

import tkinter as tk
import tkinter
import cv2
from PIL import Image, ImageTk
import os
import glob

os.chdir('./')


class Application(tk.Frame):
  def __init__(self,master):
    super().__init__(master)
    master.title("Base window")
    global i
    self.window = []
    self.user = []
    
    self.seq=0
    i=0
    
    self.size=64 #Split size
    sep = 70#Button size determination
    
    self.Clp=[0 ,self.size*1,self.size*2,self.size*3,self.size*4]

    def dvd_clicked():
            self.Button["text"] = "Choice"
            self.Button["fg"] = "RED"
            self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'0.jpg', quality=95)
    def dvd_clicked0_1():
            self.Button0_1["text"] = "Choice"
            self.Button0_1["fg"] = "RED"
            self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_1.jpg', quality=95)
    def dvd_clicked0_2():
            self.Button0_2["text"] = "Choice"
            self.Button0_2["fg"] = "RED"
            self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_2.jpg', quality=95)
    def dvd_clicked0_3():
            self.Button0_3["text"] = "Choice"
            self.Button0_3["fg"] = "RED"
            self.im_crop = self.image_pil.crop((192, 0, 256, 64))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_3.jpg', quality=95)
    def dvd_clicked1():
            self.Button1["text"] = "Choice"
            self.Button1["fg"] = "RED"
            self.im_crop = self.image_pil.crop((0, 64, 64, 128))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'1.jpg', quality=95)
    def dvd_clicked1_1():
            self.Button1_1["text"] = "Choice"
            self.Button1_1["fg"] = "RED"
            self.im_crop = self.image_pil.crop((64, 64, 128, 128))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_1.jpg', quality=95)
    def dvd_clicked1_2():
            self.Button1_2["text"] = "Choice"
            self.Button1_2["fg"] = "RED"
            self.im_crop = self.image_pil.crop((128, 64, 192, 128))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_2.jpg', quality=95)
    def dvd_clicked1_3():
            self.Button1_3["text"] = "Choice"
            self.Button1_3["fg"] = "RED"
            self.im_crop = self.image_pil.crop((192, 64, 256, 128))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_3.jpg', quality=95)
    def dvd_clicked2():
            self.Button2["text"] = "Choice"
            self.Button2["fg"] = "RED"
            self.im_crop = self.image_pil.crop((0, 128, 64, 192))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'2.jpg', quality=95)
    def dvd_clicked2_1():
            self.Button2_1["text"] = "Choice"
            self.Button2_1["fg"] = "RED"
            self.im_crop = self.image_pil.crop((64, 128, 128, 192))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_1.jpg', quality=95)
    def dvd_clicked2_2():
            self.Button2_2["text"] = "Choice"
            self.Button2_2["fg"] = "RED"
            self.im_crop = self.image_pil.crop((128, 128, 192, 192))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_2.jpg', quality=95)
    def dvd_clicked2_3():
            self.Button2_3["text"] = "Choice"
            self.Button2_3["fg"] = "RED"
            self.im_crop = self.image_pil.crop((192, 128, 256, 192))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_3.jpg', quality=95)
    def dvd_clicked3():
            self.Button3["text"] = "Choice"
            self.Button3["fg"] = "RED"
            self.im_crop = self.image_pil.crop((0, 192, 64, 256))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'3.jpg', quality=95)
    def dvd_clicked3_1():
            self.Button3_1["text"] = "Choice"
            self.Button3_1["fg"] = "RED"
            self.im_crop = self.image_pil.crop((64, 192, 128, 256))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_1.jpg', quality=95)
    def dvd_clicked3_2():
            self.Button3_2["text"] = "Choice"
            self.Button3_2["fg"] = "RED"
            self.im_crop = self.image_pil.crop((128, 192, 192, 256))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_2.jpg', quality=95)
    def dvd_clicked3_3():
            self.Button3_3["text"] = "Choice" 
            self.Button3_3["fg"] = "RED"
            self.im_crop = self.image_pil.crop((192, 192, 256, 256))
            self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_3.jpg', quality=95)

    def dvd():
        global i
        """File name generation"""
        i+=1
        if(i>=len(filename)):
            i=0
        self.Button.grid_remove()
        
        #filename[i] = "tihou.jpg "
        image_bgr = cv2.imread(filename[i])
        image_bgr = cv2.resize(image_bgr , (256, 256))
        image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #Since imread is BGR, it is converted to RGB
        self.image_pil = Image.fromarray(image_rgb) #Convert from RGB to PIL format
        
        self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
        self.image_tk  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
        self.image_tk0_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
        self.image_tk0_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[0],self.Clp[4],self.Clp[1]))
        self.image_tk0_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        
        self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[1],self.Clp[1],self.Clp[2]))
        self.image_tk1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[1],self.Clp[2],self.Clp[2]))
        self.image_tk1_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[1],self.Clp[3],self.Clp[2]))
        self.image_tk1_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[1],self.Clp[4],self.Clp[2]))
        self.image_tk1_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format

        self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[2],self.Clp[1],self.Clp[3]))
        self.image_tk2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[2],self.Clp[2],self.Clp[3]))
        self.image_tk2_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[2],self.Clp[3],self.Clp[3]))
        self.image_tk2_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[2],self.Clp[4],self.Clp[3]))
        self.image_tk2_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        
        self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[3],self.Clp[1],self.Clp[4]))
        self.image_tk3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[3],self.Clp[2],self.Clp[4]))
        self.image_tk3_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[3],self.Clp[3],self.Clp[4]))
        self.image_tk3_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
        self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[3],self.Clp[4],self.Clp[4]))
        self.image_tk3_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    
    
        #Update the button.
        self.Button["image"]= self.image_tk
        self.Button0_1["image"]= self.image_tk0_1
        self.Button0_2["image"]= self.image_tk0_2
        self.Button0_3["image"]= self.image_tk0_3
        
        #Update the button.
        self.Button1["image"]= self.image_tk1
        self.Button1_1["image"]= self.image_tk1_1
        self.Button1_2["image"]= self.image_tk1_2
        self.Button1_3["image"]= self.image_tk1_3
        
        #Update the button.
        self.Button2["image"]= self.image_tk2
        self.Button2_1["image"]= self.image_tk2_1
        self.Button2_2["image"]= self.image_tk2_2
        self.Button2_3["image"]= self.image_tk2_3  
        
        #Update the button.
        self.Button3["image"]= self.image_tk3
        self.Button3.grid(row=0, column=0)
        self.Button3_1["image"]= self.image_tk3_1
        self.Button3_2["image"]= self.image_tk3_2
        self.Button3_3["image"]= self.image_tk3_3
        
        self.Button.grid(row=0, column=0)
        self.Button0_1.grid(row=0, column=1)
        self.Button0_2.grid(row=0, column=2)
        self.Button0_3.grid(row=0, column=3)
        
        self.Button1.grid(row=1, column=0)
        self.Button1_1.grid(row=1, column=1)
        self.Button1_2.grid(row=1, column=2)
        self.Button1_3.grid(row=1, column=3)
        
        self.Button2.grid(row=2, column=0)
        self.Button2_1.grid(row=2, column=1)
        self.Button2_2.grid(row=2, column=2)
        self.Button2_3.grid(row=2, column=3)
        
        self.Button3.grid(row=3, column=0)
        self.Button3_1.grid(row=3, column=1)
        self.Button3_2.grid(row=3, column=2)
        self.Button3_3.grid(row=3, column=3)
  
    basename = []
    basename_without=[]

    filename =glob.glob('*.jpg')
    for x in range(len(filename)):
        #file name(With extension)For reading images
        basename1 = os.path.basename(filename[x])
        basename.append(basename1)
        #file name(No extension)Used when saving images
        basename_without_ext = os.path.splitext(os.path.basename(filename[x]))[0]
        basename_without.append(basename_without_ext)

        
    #filename[i] = "tihou.jpg "
    image_bgr = cv2.imread(filename[i])
    image_bgr = cv2.resize(image_bgr , (256, 256))
    image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #Since imread is BGR, it is converted to RGB
    self.image_pil = Image.fromarray(image_rgb) #Convert from RGB to PIL format

    self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
    self.image_tk  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    #Place the button.
    self.Button = tkinter.Button(master,width=sep,height=sep,text='Unselected') #Generate a button.
    self.Button["bd"] = "7"
    self.Button["compound"] = "top"
    self.Button["command"]=dvd_clicked
    self.Button["image"]= self.image_tk
    self.Button.grid(row=0, column=0)


    self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
    self.image_tk0_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button0_1= tkinter.Button(master,width=sep,height=sep,text='Unselected',image = self.image_tk0_1) #Generate a button.
    self.Button0_1["bd"] = "7"
    self.Button0_1["compound"] = "top"
    self.Button0_1["command"]=dvd_clicked0_1
    self.Button0_1.grid(row=0, column=1)

    self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
    self.image_tk0_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button0_2= tkinter.Button(master,width=sep,height=sep,text='Unselected',image = self.image_tk0_2) #Generate a button.
    self.Button0_2["bd"] = "7"
    self.Button0_2["compound"] = "top"
    self.Button0_2["command"]=dvd_clicked0_2
    self.Button0_2.grid(row=0, column=2)

    canvas0_3 = tkinter.Canvas(master, width=sep, height=sep) #Canvas creation
    #canvas.pack()
    self.im_crop = self.image_pil.crop((192, 0, 256, 64))
    self.image_tk0_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button0_3= tkinter.Button(master,width=sep,height=sep,text='Unselected',image = self.image_tk0_3) #Generate a button.
    self.Button0_3["bd"] = "7"
    self.Button0_3["compound"] = "top"
    self.Button0_3["command"]=dvd_clicked0_3
    self.Button0_3.grid(row=0, column=3)

    self.im_crop = self.image_pil.crop((0, 64, 64, 128))
    self.image_tk1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button1= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk1) #Generate a button.
    self.Button1["bd"] = "7"
    self.Button1["compound"] = "top"
    self.Button1["command"]=dvd_clicked1
    self.Button1.grid(row=1, column=0)

    self.im_crop = self.image_pil.crop((64, 64, 128, 128))
    self.image_tk1_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button1_1= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk1_1) #Generate a button.
    self.Button1_1["bd"] = "7"
    self.Button1_1["compound"] = "top"
    self.Button1_1["command"]=dvd_clicked1_1
    self.Button1_1.grid(row=1, column=1)


    self.im_crop = self.image_pil.crop((128, 64, 192, 128))
    self.image_tk1_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button1_2= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk1_2) #Generate a button.
    self.Button1_2["bd"] = "7"
    self.Button1_2["compound"] = "top"
    self.Button1_2["command"]=dvd_clicked1_2
    self.Button1_2.grid(row=1, column=2)

    canvas1_3 = tkinter.Canvas(master, width=sep, height=sep) #Canvas creation
    #canvas.pack()
    self.im_crop = self.image_pil.crop((192 ,64, 256, 128))
    self.image_tk1_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button1_3= tkinter.Button(master,width=sep,height=sep,text='Unselected',image = self.image_tk1_3) #Generate a button.
    self.Button1_3["bd"] = "7"
    self.Button1_3["compound"] = "top"
    self.Button1_3["command"]=dvd_clicked1_3
    self.Button1_3.grid(row=1, column=3)


    self.im_crop = self.image_pil.crop((0, 128, 64, 192))
    self.image_tk2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button2= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk2) #Generate a button.
    self.Button2["bd"] = "7"
    self.Button2["compound"] = "top"
    self.Button2["command"]=dvd_clicked2
    self.Button2.grid(row=2, column=0)


    self.im_crop = self.image_pil.crop((64, 128, 128, 192))
    self.image_tk2_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button2_1= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk2_1) #Generate a button.
    self.Button2_1["bd"] = "7"
    self.Button2_1["compound"] = "top"
    self.Button2_1["command"]=dvd_clicked2_1
    self.Button2_1.grid(row=2, column=1)


    self.im_crop = self.image_pil.crop((128, 128, 192, 192))
    self.image_tk2_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button2_2= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk2_2) #Generate a button.
    self.Button2_2["bd"] = "7"
    self.Button2_2["compound"] = "top"
    self.Button2_2["command"]=dvd_clicked2_2
    self.Button2_2.grid(row=2, column=2)


    self.im_crop = self.image_pil.crop((192, 128, 256, 192))
    self.image_tk2_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button2_3= tkinter.Button(master,width=sep,height=sep,text='Unselected',image = self.image_tk2_3) #Generate a button.
    self.Button2_3["bd"] = "7"
    self.Button2_3["compound"] = "top"
    self.Button2_3["command"]=dvd_clicked2_3
    self.Button2_3.grid(row=2, column=3)


    self.im_crop = self.image_pil.crop((0, 192, 64, 256))
    self.image_tk3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button3= tkinter.Button(master,width=sep,height=sep,text='Unselected',image = self.image_tk3) #Generate a button.
    self.Button3["bd"] = "7"
    self.Button3["compound"] = "top"
    self.Button3["command"]=dvd_clicked3
    self.Button3.grid(row=3, column=0)

    self.im_crop = self.image_pil.crop((64, 192, 128, 256))
    self.image_tk3_1  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button3_1= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk3_1) #Generate a button.
    self.Button3_1["bd"] = "7"
    self.Button3_1["compound"] = "top"
    self.Button3_1["command"]=dvd_clicked3_1
    self.Button3_1.grid(row=3, column=1)


    self.im_crop = self.image_pil.crop((128, 192, 192, 256))
    self.image_tk3_2  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button3_2= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk3_2) #Generate a button.
    self.Button3_2["bd"] = "7"
    self.Button3_2["compound"] = "top"
    self.Button3_2["command"]=dvd_clicked3_2
    self.Button3_2.grid(row=3, column=2)

    self.im_crop = self.image_pil.crop((192, 192, 256, 256))
    self.image_tk3_3  = ImageTk.PhotoImage(self.im_crop) #Convert to ImageTk format
    self.Button3_3= tkinter.Button(master,width=sep,height=sep,text='Unselected' ,image = self.image_tk3_3) #Generate a button.
    self.Button3_3["bd"] = "7"
    self.Button3_3["compound"] = "top"
    self.Button3_3["command"]=dvd_clicked3_3
    self.Button3_3.grid(row=3, column=3)

    #Place the label.

    label1 = tkinter.Label(master,text='Please select a storage area')
    label1["font"] = ("",18)
    label1["height"] = 2
    label1.grid(row=4, column=0,columnspan=4)


    self.SButton1= tkinter.Button(master,text='<<Previous image' ) #Generate a button.
    self.SButton1["bd"] = "7"
    self.SButton1.grid(row=5, column=0,columnspan=2)

    self.SButton2= tkinter.Button(master,text='Next image>>' ) #Generate a button.
    self.SButton2["bd"] = "7"
    self.SButton2["command"]=dvd
    self.SButton2.grid(row=5, column=2,columnspan=2)


def main():
    win = tk.Tk()
    app = Application(win)
    app.mainloop()

if __name__ == '__main__':
  main()


Recommended Posts

Creating an image splitting app with Tkinter
Create an image composition app with Flask + Pillow
Creating an egg with python
Create Image Viewer with Tkinter
Cut out an image with python
Easy GUI app with Tkinter Text
Creating a simple app with flask
[Python] Creating multiple windows with Tkinter
Creating an unknown Pokemon with StyleGAN2 [Part 1]
Create an image processing viewer with PySimpleGUI
Note when creating an environment with python
Creating an unknown Pokemon with StyleGAN2 [Part 2]
Create a GUI app with Python's Tkinter
Create an English word app with python
Try to generate an image with aliasing
Create an app that guesses students with python
GUI image cropping tool made with Python + Tkinter
How to crop an image with Python + OpenCV
MVC with Tkinter
Create an image with characters in python (Japanese)
Post an article with an image to WordPress with Python
I want to convert an image to WebP with lollipop
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Create a native GUI app with Py2app and Tkinter
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Create an app that guesses students with python-GUI version
Let's make a Mac app with Tkinter and py2app
Make an application using tkinter an executable file with cx_freeze
Image processing with MyHDL
Image recognition with keras
Creating an authentication function
Tab app in Tkinter
Become Santa with Tkinter
linux with termux app
Image processing with Python
Image Processing with PIL
"Garbage classification by image!" App creation diary day2-Fine-tuning with VGG16-
Solution when the image cannot be displayed with tkinter [python]
Creating an environment that automatically builds with Github Actions (Android)
Let's create an app that authenticates with OIDC with Azure AD
How to run an app built with Python + py2app built with Anaconda
Creating a GUI as easily as possible with python [tkinter edition]
Create a Todo app with Django ① Build an environment with Docker