パーソナルブログメモリ

a = [1, 1]
for _ in "*" * 999: a += [sum(a[-2:])]
print(a)

tkinterで画像ビューワーを作る2

2020-08-21 | Python

ツールの概要

画像のたくさん入ったツリー構造のフォルダを閲覧する。

画像を左クリックでプログラムを実行したフォルダからのパス+ファイル名と元画像のサイズを表示。

 

起動までの下準備

1)画像のたくさん入ったフォルダーを端末で開いて

tree -f > tree.txt

を実行してtreeのテキストを作成しておく。(treeコマンドのインストールが必要)

2)そのフォルダにプログラムを置いてpython3で実行する。

 

使い方

開始番号を入れることで指定位置からの検索ができる。(番号はtreeの表示順 範囲を超えると最初から)

next_imageボタンで画像の一覧を作成

画像を左クリックするとその画像の情報を表示する

 

開発環境

Ubuntu 18.04.4 LTS

Python 3.7.5

 

ソースリスト

# $ tree -f > tree.txt で作成したファイル名から画像のものを表示するツールを作る
import tkinter as tk
from PIL import Image, ImageTk

def get_image_check(i):
    i=i.replace("\n","").lower()
    for j in [".jpg", ".png", ".jpeg"]:
        pos = i.find(j)
        if pos>-1 and pos+len(j)==len(i):return True
    return False

def get_pathfile_name(i):
    pos=i.find(".")
    return i[pos:].replace("\n", "")

class Application(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()
    def create_widgets(self):
        global wx, wy
        self.b_next = tk.Button(self)
        self.b_next["text"] = "next_image\n(click me)"
        self.b_next["command"] = self.image_next
        self.b_next.pack(side="top")
        self.b_quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
        self.b_quit.pack(side="bottom")
        self.canvas = tk.Canvas(width = wx, height = wy-100)
        self.canvas.place(x=0, y = 100)
        self.canvas.bind('<ButtonRelease-1>', self.stop_pickup)
        self.lbl2 = tk.Label(text = "次の開始番号")
        self.lbl2.place(x=700, y=10)
        self.text = tk.Entry(width=10)
        self.text.place(x=700, y=30)
        self.lbl = tk.Label(text = "左ボタンでパス+ファイル名と元サイズを表示します")
        self.lbl.place(x=30, y=30)
    def image_next(self):
        global wx, wy, sx, sy, imgs, filenames, hover_sw, image_num
        for _ in "1"*1:
            hover_sw = 0
            imgs = []
            filenames = []
            num = self.text.get()
            if num.isnumeric():image_num = int(num)
            for y in range(0, wy-100 - sy + 1,sy):
                for x in range(0, wx - sx + 1, sx):
                    size_x, size_y, tile_img = image_chenge()
                    imgs += [tile_img]
                    filenames[-1] = (filenames[-1], x, y, size_x, size_y)
                    self.canvas.create_image(x, y, image = imgs[-1], anchor=tk.NW)
            self.lbl["text"] = "左ボタンでパス+ファイル名と元サイズを表示します"
            self.text.delete(0, tk.END)
            self.text.insert(tk.END, str(image_num))
            self.master.update()

            hover_sw = 1
            self.canvas.after(2000)
    def stop_pickup(self, event):
        global sx, sy, filenames, hover_sw
        if hover_sw == 0:return
        for name,fx,fy,size_x, size_y in filenames:
            if fx<=event.x<=fx+sx and fy<=event.y<=fy+sy:
                self.lbl["text"] = name + " (" + str(size_x) +", "+str(size_y) + ")"
                self.master.update()

def image_chenge():
    global image_num, filenames, sx, sy
    if image_num > len(pathfile_list)-1:image_num = 0
    img = Image.open(pathfile_list[image_num])
    filenames += [pathfile_list[image_num]]
    #print(pathfile_list[image_num])
    x, y = img.size
    rx,ry = sx, y/x*sx
    if x/sx < y/sy:
        rx, ry = x/y*sy, sy
    image_num += 1
    return x, y, ImageTk.PhotoImage(img.resize((int(rx), int(ry)), Image.LANCZOS))

with open("./tree.txt") as f:
    tree_list = f.readlines()

pathfile_list = [get_pathfile_name(i) for i in tree_list if i != "\n" and get_image_check(i)]

image_num = 0
wx, wy = 1200, 900 #window_size
sx, sy = 150,100 #mini window_size
imgs = [] #ここにないと表示されるまでの間にガベージコレクトされるらしい
filenames = []
hover_sw = 0

root = tk.Tk()
app = Application(master=root)

app.master.title("tree view Application")
app.master.minsize(wx, wy)
app.master.maxsize(wx, wy)
app.mainloop()


最新の画像もっと見る

コメントを投稿

ブログ作成者から承認されるまでコメントは反映されません。