python做相册_Python编程:制作电子相册

Python编程:制作电子相册

环境:

主机:WIN10

python版本:3.5

开发环境:pyCharm 5.0.2

说明:

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

功能:

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

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

效果:

20160731203224864

源代码:

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值