Python编程:制作电子相册
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:
主机:WIN10
python版本:3.5
开发环境:pyCharm 5.0.2
源代码:
import os
import threading
import tkinter as tk
import time
from PIL import ImageTk, Image
#分辨率
resolution = (1366, 768)
# 路径
Path = 'd:\photo'
# 播放间隔.单位:s
Interval = 5
# 当前照片计数
Index = 0
scaler = Image.ANTIALIAS
root = tk.Tk()
img_in = Image.open("load.jpg")
w, h = img_in.size
size_new = ((int)(w * resolution[1] / h), resolution[1])
img_out = img_in.resize(size_new, scaler)
img = ImageTk.PhotoImage(img_out)
# img = ImageTk.PhotoImage(Image.open("load.jpg"))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
global Index
files = os.listdir(Path)
i = 0
for x in files:
# 判断文件是否存在
if not os.path.isfile(Path + '\%s' % x):
break
if i < Index:
i += 1
continue
print('手动处理图片', x, Index)
if not (x.endswith('.jpg') or x.endswith('.JPG')):
i += 1
Index += 1
if Index >= len(files):
Index = 0
continue
img_in = Image.open(Path + '\%s' % x)
print(img_in)
w, h = img_in.size
size_new = ((int)(w * resolution[1] / h), resolution[1])
img_out = img_in.resize(size_new, scaler)
img2 = ImageTk.PhotoImage(img_out)
# img2 = ImageTk.PhotoImage(Image.open(Path + '\%s' % x))
panel.configure(image=img2)
panel.image = img2
Index += 1
if Index >= len(files):
Index = 0
break
# root.bind("", callback)
root.bind("", callback)
def image_change():
global Index
time.sleep(3)
while True:
files = os.listdir(Path)
i = 0
for x in files:
# 判断文件是否存在
if not os.path.isfile(Path + '\%s' % x):
break
if i < Index:
i += 1
continue
print('自动处理图片', x, Index)
if not (x.endswith('.jpg') or x.endswith('.JPG')):
i += 1
Index += 1
if Index >= len(files):
Index = 0
continue
img_in = Image.open(Path + '\%s' % x)
w, h = img_in.size
size_new = ((int)(w * resolution[1] / h), resolution[1])
img_out = img_in.resize(size_new, scaler)
img2 = ImageTk.PhotoImage(img_out)
# img2 = ImageTk.PhotoImage(Image.open(Path + '\%s' % x))
panel.configure(image=img2)
panel.image = img2
Index += 1
if Index >= len(files):
Index = 0
time.sleep(Interval)
# 图片切换线程
t = threading.Thread(target=image_change)
t.start()
root.mainloop()