import os
def getImages(filePath):
result = {}
for filepath,dirnames,filenames in os.walk(filePath):
key = filepath.replace('.', '')
if key not in result.keys():
result[key] = []
for filename in filenames:
image_path = os.path.join(filepath,filename)
result[key].append({
'src': image_path,
'name': filename
})
return result
def write(images):
f = open("./image-view.html", "w+")
f.write('<html lang="en">\n' +
'<head>\n' +
'<meta charset="UTF-8">\n' +
'<meta http-equiv="X-UA-Compatible" content="IE=edge">\n' +
'<meta name="viewport" content="width=device-width, initial-scale=1.0">\n' +
'<title>Document</title>\n' +
'<style> body > div { margin: 0 40px 40px 40px; padding:40px; display: flex; flex-wrap:wrap; box-shadow: 0 0 20px rgb(0 0 0 / 30%); } ' +
'h2.title { padding: 40px 0 20px 40px; font-size: 20px; font-weight: bold; text-align: center; } ' +
'.img-block span { display: inline-block; vertical-align: top;width: 100px;word-break: break-all; margin-right: 24px; }' +
'.img-block img { display: inline-block; width: 50px; vertical-align: top; padding: 16px; background: rgba(0,0,0,0.1);margin-right: 16px; margin-bottom: 16px;} ' +
'.img-block { }' +
'</style>\n' +
'</head> \n' +
'<body>\n')
for key in images:
f.write('<h2 class="title"> ' + key + '</h2>\n')
f.write('<div> \n')
for item in images[key]:
f.write('<div class="img-block">')
f.write(' <img src="' + item['src'] + '" />\n' )
f.write(' <span>' + item['name'] + ' </span>\n')
f.write('</div>')
f.write('</div>\n')
f.write('</body></html>')
f.close()
if __name__ == 'main':
rootdir = '/Users/chencyn/images/'
write(getImages(rootdir))
python: 读取文件夹下的所有svg文件生成html以便预览
于 2021-11-23 11:12:43 首次发布