각 폴더에 저장된 JPG 파일 리스트 HTML로 만들기
페이지 정보
본문
시간 날때 마다 보려고 웹툰을 미리 다운 받아서 저장 해 놓았으나 기존 만화 파일과는 다르게 일반 뷰어에서는 보기 힘들어서 폴더내의 JPG 파일 리스트를 추출해서 html로 저장하는 프로그램을 간단히 만들어 봤습니다.
파이썬입니다.
# -*- coding: utf-8 -*-
import os
import tkinter
from tkinter import filedialog
from PIL import Image
root = tkinter.Tk()
root.withdraw()
def createDirectory(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print("Error: Failed to create the directory.")
def create_list(main_path,path) :
file_list=os.listdir(path)
html_path = main_path+"/html"
createDirectory(html_path)
html_file_name = html_path+"/"+path.split("\\")[-1]+".html"
f = open(html_file_name, 'w')
f.write("<center>")
get_jpg_size = 1
file_list_jpg=[file for file in file_list if file.endswith(".jpg")]
for file_name in file_list_jpg:
if get_jpg_size == 1 :
jpg_file_path = os.path.join(path,file_name).replace("\\","/")
jpg_image = Image.open(jpg_file_path)
jpg_w,jpg_h = jpg_image.size
get_jpg_size = 0
html_file_name = '<img src="..\\..\\'+path.split("/")[-1]+"\\"+file_name+'" width='+str(jpg_w)+'><br>'
f.write(html_file_name)
f.write("</center>")
f.close()
main_path=filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
dirs = os.listdir(main_path)
f_list = open(main_path+"/list.html", 'w')
f_list.write('<!DOCTYPE html><html><head><title>'+main_path+'</title></head><body><center>')
first_file_name = 0
for file in dirs:
root_path = os.path.join(main_path, file)
if os.path.isdir(root_path):
dir_name = root_path.split("\\")[-1]
if first_file_name == 0:
first_file_name = dir_name
if dir_name != "html":
create_list(main_path,root_path)
f_list.write('<a href=".\\html\\'+dir_name+'.html" target=cartoon>'+dir_name+'</a><br>')
last_file_name = dir_name
f_list.write('</center></body></html>')
f_list.close()
f_list = open(main_path+"/main.html", 'w')
f_list.write('<!DOCTYPE html><html><head><title>'+main_path+'</title></head><body><h3>'+main_path.split('/')[-1]+' 총'+last_file_name+'회</h3><center>')
f_list.write('<iframe src="list.html" width=15% height=1024 frameborder=0 name=list align=left></iframe>')
f_list.write('<iframe src="./html/'+first_file_name+'.html" width=80% height=2048 name=cartoon frameborder=0 align=right></iframe>')
f_list.write('</center></body></html>')
f_list.close()