1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Tkinterで画像分割アプリの作成

Posted at

#Tkinterで画像分割アプリの作成
今回はTkinterの記事を書いていこうと思います.

画像一枚から縦横4分割して,分割した画像を保存するGUIを作りました.
※Tkinter初心者なので,とっても拙いコードなのです

完成像
 スクリーンショット 2020-04-26 15.22.50.png

今回はクリックして,画像を保存したかったので,画像はボタンに表示しています
下にある「前の画像」,「次の画像」でフォルダ内にある他の画像に切り替えを行います.

#ボタンを配置する。

self.Button = tkinter.Button(master,width=sep,height=sep,text='未選択') #ボタンを生成する。
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 = 画像を配置したときの文字の位置
command = ボタンを押したときのイベント
image = 表示する画像

#イベント

def dvd_clicked():
            self.Button["text"] = "選択"
            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)

ここでは各ボタンをクリックした際に画像を保存しています
部分ごとに保存するため,0_1などの場所の情報を加えてファイルを保存しました

選択した領域は選択後,赤文字でテキストが「選択」になります.

「前の画像」「次の画像」を押した場合は各画像を更新します.
※ここは引用すると長いので,def dvdを参照してください

#全体コード

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("ベースウィンドウ")
    global i
    self.window = []
    self.user = []
    
    self.seq=0
    i=0
    
    self.size=64 #分割サイズ
    sep = 70#Buttonサイズの決定
    
    self.Clp=[0 ,self.size*1,self.size*2,self.size*3,self.size*4]

    def dvd_clicked():
            self.Button["text"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択"
            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"] = "選択" 
            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
        """ ファイル名生成 """
        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) # imreadはBGRなのでRGBに変換
        self.image_pil = Image.fromarray(image_rgb) # RGBからPILフォーマットへ変換
        
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換

        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
        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) # ImageTkフォーマットへ変換
    
    
        #ボタンを更新する。
        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
        
        #ボタンを更新する。
        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
        
        #ボタンを更新する。
        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  
        
        #ボタンを更新する。
        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)):
        #ファイル名(拡張子あり)画像読み込み用 
        basename1 = os.path.basename(filename[x])
        basename.append(basename1)
        #ファイル名(拡張子なし)画像保存時に使用
        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) # imreadはBGRなのでRGBに変換
    self.image_pil = Image.fromarray(image_rgb) # RGBからPILフォーマットへ変換

    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) # ImageTkフォーマットへ変換
    #ボタンを配置する。
    self.Button = tkinter.Button(master,width=sep,height=sep,text='未選択') #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button0_1= tkinter.Button(master,width=sep,height=sep,text='未選択',image = self.image_tk0_1) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button0_2= tkinter.Button(master,width=sep,height=sep,text='未選択',image = self.image_tk0_2) #ボタンを生成する。
    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作成
    #canvas.pack()
    self.im_crop = self.image_pil.crop((192, 0, 256, 64))
    self.image_tk0_3  = ImageTk.PhotoImage(self.im_crop) # ImageTkフォーマットへ変換
    self.Button0_3= tkinter.Button(master,width=sep,height=sep,text='未選択',image = self.image_tk0_3) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button1= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk1) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button1_1= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk1_1) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button1_2= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk1_2) #ボタンを生成する。
    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作成
    #canvas.pack()
    self.im_crop = self.image_pil.crop((192 ,64, 256, 128))
    self.image_tk1_3  = ImageTk.PhotoImage(self.im_crop) # ImageTkフォーマットへ変換
    self.Button1_3= tkinter.Button(master,width=sep,height=sep,text='未選択',image = self.image_tk1_3) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button2= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk2) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button2_1= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk2_1) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button2_2= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk2_2) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button2_3= tkinter.Button(master,width=sep,height=sep,text='未選択',image = self.image_tk2_3) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button3= tkinter.Button(master,width=sep,height=sep,text='未選択',image = self.image_tk3) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button3_1= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk3_1) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button3_2= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk3_2) #ボタンを生成する。
    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) # ImageTkフォーマットへ変換
    self.Button3_3= tkinter.Button(master,width=sep,height=sep,text='未選択' ,image = self.image_tk3_3) #ボタンを生成する。
    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)

    #ラベルを配置する。

    label1 = tkinter.Label(master,text='保存領域を選択してください')
    label1["font"] = ("",18)
    label1["height"] = 2
    label1.grid(row=4, column=0,columnspan=4)


    self.SButton1= tkinter.Button(master,text='<< 前の画像' ) #ボタンを生成する。
    self.SButton1["bd"] = "7"
    self.SButton1.grid(row=5, column=0,columnspan=2)

    self.SButton2= tkinter.Button(master,text='次の画像  >>' ) #ボタンを生成する。
    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()
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?