Python编程:制作电子相册
本文博客鏈接:http://blog.csdn.net/jdh99,作者:jdh,轉載請注明.
環境:
主機:WIN10
python版本:3.5
開發環境:pyCharm 5.0.2
說明:
家里有不用的windows平板me400c,用python編寫一個腳本,實現電子相冊功能。
功能:
1.每5s自動播放下一張
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("<Return>", callback)
root.bind("<Button-1>", 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()
?
————————————————
版權聲明:本文為CSDN博主「jdh99」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/jdh99/article/details/52080514
總結
以上是生活随笔為你收集整理的Python编程:制作电子相册的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手把手教你如何用Python制作一个电子
- 下一篇: 土豆粉家常做法?