python 动态相册_Python编程:制作电子相册

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:17   3201   0

Python编程:制作电子相册

环境:

主机:WIN10

python版本:3.5

开发环境:pyCharm 5.0.2

说明:

家里有不用的windows平板me400c,用python编写一个脚本,实现电子相册功能。

功能:

1.每5s自动播放下一张

2.可以手动点击,播放下一张

效果:

fdf41753cd1f23d54c441b1b63daecce.png

源代码:

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()

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP