Skip to content

Instantly share code, notes, and snippets.

@Neko288
Created November 29, 2021 23:16
Show Gist options
  • Save Neko288/064409cda179963986262b91bebe69b2 to your computer and use it in GitHub Desktop.
Save Neko288/064409cda179963986262b91bebe69b2 to your computer and use it in GitHub Desktop.
ImageViewer
import os,requests,math,tkinter as tk,subprocess
from tkinter import font
from tkinter import messagebox
from PIL import Image, ImageTk
"""#Help
Name : Simple image viewer
2021-August-30
-利用規約
-ライセンス
このソフトウェアの著作権は Neko288 が持っています。
-免責事項
このソフトウェアを使用することによって生じた損害に関して、作者は一切責任を負いません。
-使い方
-画像を表示
左上の入力欄にフォルダーのフルパスを入力する。
Nextボタンをクリックする。
-画像を見る
Nextボタンをクリックすると、次の画像が表示される。
Backボタンをクリックすると、前の画像が表示される。
画像をクリックすると、画像が単体で表示される。
-表示について
-Windowのタイトル
Windowのタイトルは現在開いているファイルの名前も表示している。
image_Viewer for 「ファイル名」
-場所
場所と表示されているところは現在見ているファイルがあるフォルダのフルパスを表示している。
-ファイル数
現在開いているフォルダーにあるフォルダ数と現在開いているファイルが何番目かを表示している。
-画像xy
表示されているファイルのx(横)とy(縦)のサイズを表示している。
(表示されている画像はリサイズされています。xyはリサイズ前のファイルのxyです。)
-画像サイズ
画像の大きさ。単位はMB。
"""
win = tk.Tk()
win.geometry("920x850")
win.title("View image")
label = tk.Label(
win,
font = ("游ゴシック", 18),
text = "画像ビューアー" )
label.place(x = 600, y=10,)
#pathだけどなんでも本来はURL
entry_text = tk.Entry(width=70)
entry_text.insert(tk.END,"D:/Mori/image/NO/")
entry_text.place(x = 600, y =50, )
#global
global img
canvas = tk.Canvas(win, width=550, height=800, bg="#c0c0c0")
global_num = 0
label_wh = tk.Label(
win,
font = ("游ゴシック", 12),
text = '',
bg='#dcdcdc')
label_wh.place(x = 550, y=820,)
label_size = tk.Label(
win,
font = ("游ゴシック", 12),
text = '',
bg='#dcdcdc')
label_size.place(x = 720, y=820,)
label_dir = tk.Label(
win,
font = ("游ゴシック", 12),
text = '',
bg='#dcdcdc')
label_dir.place(x = 720, y=770,)
label_where = tk.Label(
win,
font = ("游ゴシック", 12),
text = '',
bg='#dcdcdc')
label_where.place(x = 550, y=720,)
#Help button
help_button = tk.Button(win, text='Help', width=5, height=1,
bg='#ff0080',
foreground='white',
activeforeground='black',
command=lambda:subprocess.Popen(["start", 'C:/any\Python-P/imaeg_viewer/Help.txt'], shell=True))
help_button.place(x = 784, y = 15)
#画像表示
def show_image(back):
#resize
global canvas
global global_num
global img
global label_wh
canvas.place_forget()
if back == 'n':
global_num +=1
print('next')
if back == 'b':
global_num -=1
print('back')
if back == 's':
global_num = 0
dir_path = entry_text.get() + r'/' #パスの最後に/入れて
aru_nai = os.path.exists(dir_path)
if aru_nai == False:
messagebox.showerror('Error 404','指定されたパスが見つかりません。絶対パスを入力してください。')
all_img = os.listdir(dir_path)
if len(all_img) <= 1:
messagebox.showerror('Error', '画像が少ないようです...一枚以下では表示できません')
img = Image.open(dir_path + all_img[global_num])
print(' Image.open ..OK')
label_wh["text"] = '画像xy : ' + str(img.width) +'x'+ str((img.height))
image_size = os.path.getsize(dir_path + all_img[global_num]) / 1024
n = 2 # 切り捨てしたい桁
y = math.floor(image_size * 10 ** n) / (10 ** n)
label_size["text"] = '画像サイズ :' + str(y)+'MB'
label_dir["text"] = 'ファイル数 : ' + str(len(all_img)) +'/'+ str(global_num)
label_where["text"] = '場所 : ' + dir_path
win.title('image_Viewer for 「' + all_img[global_num] + '」')
print(' windows set ..OK')
w = img.width
h = img.height
size = 540
sizeh = 840
if w == h:
small_num_1 = (w / size)
w = math.ceil(w/small_num_1)
h = w
elif w >= size:
small_num_2 = (w / size)
w = math.ceil(w/small_num_2)
h = math.ceil(h/small_num_2)
elif w <= size:
small_num_3 = (size / w)
w = math.ceil(w*small_num_3)
h = math.ceil(h*small_num_3)
if h >= sizeh:
small_num_4 = (h / sizeh)
w = math.ceil(w/small_num_4)
h = math.ceil(h/small_num_4)
resize_img = img.resize((w,h))
#画像クリック時フォルダーを開く
def click(self):
subprocess.Popen([entry_text.get() +'/'+all_img[global_num]], shell=True)
img = ImageTk.PhotoImage(resize_img)
canvas = tk.Canvas(win, width=w, height=h, bg="#c0c0c0",)
canvas.place(x=0, y=0)
canvas.create_image(0,0, anchor=tk.NW, image=img)
canvas.bind("<Button-1>", click)
print(' create_image ..OK')
#Next button
next_image_button = tk.Button(win, text='Next', width=10, height=2,
bg='#00ffff',
foreground='black',
activeforeground='black',
command=lambda:show_image('n'),
repeatinterval=150,#長押し
repeatdelay=1,)#長押し
#引数付き関数をボタンで実行する際の注意点*2
next_image_button.place(x = 600, y = 100)
#Back button
back_image_button = tk.Button(win, text='Back', width=10, height=2,
bg='#ff0080',
foreground='white',
activeforeground='black',
command=lambda:show_image('b'),
repeatinterval=150,#長押し
repeatdelay=1,)
back_image_button.place(x = 750, y = 100)
#show_image('s')
print('___PROGRAM Setting ..OK___')
win.mainloop()
#*2 = https://duckduckgo.com/?q=command+tkinter+%E5%BC%95%E6%95%B0&t=ffab&ia=web
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment