动感网页相册 python编写简单文件夹内图片浏览工具

yipeiwu_com6年前Python基础

不知道大家有没有这样的体验,windows电脑上查看一张gif图,默认就把IE给打开了,还弹出个什么询问项,好麻烦的感觉。所以为了解决自己的这个问题,写了个简单的文件夹内图片浏览工具。

效果图

以E盘某一文件夹为例

效果图

效果图

实现思路

业务代码

# coding:utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')
#  __author__ = '郭 璞'
#  __date__ = '2016/8/5'
#  __Desc__ = 自动生成网页相册
import os


# 呵呵了,原来有标准库中的walk方法。那么这个方法就获得一个文件夹下的图片文件吧
def getFiles(filepath):
  files = []
  if os.path.isdir(filepath):
    for file in os.listdir(filepath):
      if os.path.isdir(file):
        getFiles(file)
      elif file.endswith('.jpg') or file.endswith('.png') or file.endswith('.gif'):
        files.append(filepath + str(file))
  elif os.path.isfile(filepath):
    files.append(filepath)
  return files


# 获取给定目录下所有以.jpg .png .gif结尾的文件,并补全路径保存到列表中输出
def recourse(filepath):
  files = []
  for fpathe, dirs, fs in os.walk(filepath):
    for f in fs:
      if f.endswith('.jpg') or f.endswith('.png') or f.endswith('.gif'):
        files.append(os.path.join(fpathe, f))
  return files


# 生成网页源码文件,指定
def generate(files, shuffle=False):
  template_start = '''
  <html><head><meta charset='utf-8'><title>网页版相册</title><link rel="stylesheet" type="text/css" href="csshake-slow.min.css">
  <link rel="stylesheet" type="text/css" href="http://csshake.surge.sh/csshake-slow.min.css"></script></head><body>
  '''
  template_body = ''
  # 如果指定乱序,就乱序列表中的数据
  if shuffle == True:
    from random import shuffle
    shuffle(files)
  for file in files:
    template_body += '<a href="' + file + '"><img class="shake-slow" src="' + file + '" style="width:64px;height:auto;"></a>'

  template_end = '''
  </body></html>
  '''%
  html = template_start + template_body + template_end
  return html

# 生成html文件,并输出到指定的目录
def write2File(filepath, data):
  file = open(filepath, 'wb')
  file.write(data)
  file.close()
  print 'Write to file Scuuess!'


if __name__ == "__main__":
  # E:\\Picture\\LOFTER\\
  filepath = 'E:\\Picture\\LOFTER\\'
  files = recourse(filepath=filepath)
  for item in files:
    print item
  html = generate(files, True)
  output_path = r'C:\Users\Administrator\Desktop\test.html'
  write2File(filepath=output_path, data=html)
  print 'HTML相册文件已生成在桌面,请查看'

总结
 •首先说一下缺点:
◦缺点很明显,对于中文支持的不够好,因为查看图片大图的时候是以超链接的形式出现的,所以会发生乱码的情况。
◦然后是优点:
优点不是很明显,因为如果一个文件夹下面有很多的子文件夹,或者图片很多的时候,就会很慢了。

 •然后说一下可以改进的地方
◦引入JQuery,添加双击事件相应,实现双击删除不想要的图片

 ◦使用多线程的方式运行代码,加快网页的生成速度

最后,我想说的是,虽然这是个娱乐性质的小东西,但是多发挥一下想象力,不断地完善,对我们开发而言,一定会有帮助的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pandas 快速处理 date_time 日期格式方法

当数据很多,且日期格式不标准时的时候,如果pandas.to_datetime 函数使用不当,会使得处理时间变得很长,提升速度的关键在于format的使用。下面举例进行说明: 示例数据:...

python基于socket实现网络广播的方法

本文实例讲述了python基于socket实现网络广播的方法。分享给大家供大家参考。具体实现方法如下: import socket, sys dest = ('<broadca...

django的登录注册系统的示例代码

django的登录注册系统的示例代码

摘要 django框架本身自带有登录注册,也可以自己写登录注册,下面将介绍这这2种方式实登录注册 一、自己写登录注册登出 1.注册regist 注册采用的是form表单,提交到数据库,在...

python字符串string的内置方法实例详解

下面给大家分享python 字符串string的内置方法,具体内容详情如下所示: #__author: "Pizer Wang" #__date: 2018/1/28 a = "Le...

python实现大战外星人小游戏实例代码

主程序 import pygame from pygame.sprite import Group from settings import Settings from game_...