이미지 폴더에 그림이 많을 때 그림들을 웹 브라우저로 볼 수 있게 HTML Gallery 파일을 만들어주는 파이썬 스크립트 입니다.
폴더에 그림이 수백장씩 있으면 ACDSee나 알씨같은 프로그램으로 마우스 휠 돌려가면서 한장 한장씩 보는것도 고통스럽죠. 이 스크립트는 폴더 안의 모든 그림 파일을 '원본 크기'로 한 HTML파일(혹은 그림이 너무 많다면 나눠서)에 넣어줍니다. 그저 HTML파일을 열어서 스크롤만 하면 편하게 볼 수 있습니다.
아래 스크립트를 파이썬 스크립트 파일로 저장하고 실행하면 됩니다.
폴더에 그림이 수백장씩 있으면 ACDSee나 알씨같은 프로그램으로 마우스 휠 돌려가면서 한장 한장씩 보는것도 고통스럽죠. 이 스크립트는 폴더 안의 모든 그림 파일을 '원본 크기'로 한 HTML파일(혹은 그림이 너무 많다면 나눠서)에 넣어줍니다. 그저 HTML파일을 열어서 스크롤만 하면 편하게 볼 수 있습니다.
아래 스크립트를 파이썬 스크립트 파일로 저장하고 실행하면 됩니다.
import os
def getFileListInCurDir():
""" get file list """
path = os.getcwd()
return os.listdir(path)
def extractImageFileList(fileList, replacePercentSign = True):
""" get image list """
exts = ('.jpg', '.gif', '.bmp', '.png')
imageFileList = []
for ext in exts:
for fileName in fileList:
if fileName.find(ext) != -1:
if replacePercentSign and fileName.find('%') != -1:
newFileName = fileName.replace('%', '$')
os.rename(fileName, newFileName)
fileName = newFileName
imageFileList.append(fileName)
return imageFileList
class HtmlGalleryFile:
def __init__(self, fileNameOnly, fileCount, align):
self.fileName = fileNameOnly + repr(fileCount) + '.html'
self.file = open(self.fileName, 'w')
self.writeStartTag(align)
def close(self, notifyDone = True):
self.writeEndTag()
self.file.close()
if notifyDone:
print '%s successfully done' % self.fileName
def writeStartTag(self, align):
self.file.write('')
self.file.write('' % align)
def writeEndTag(self):
self.file.write('
')
self.file.write('')
def write(self, tag):
self.file.write(tag)
def getFile(self):
return self.file
def makeHtmlImageGallery(fileNameOnly, imageFileList, maxCountPerHtml = -1, align = 'center'):
fileCount = 0
imageCount = 0
html = HtmlGalleryFile(fileNameOnly, fileCount, align)
for fileName in imageFileList:
if maxCountPerHtml != -1 and imageCount >= maxCountPerHtml:
imageCount = 0
fileCount += 1
html.close()
html = HtmlGalleryFile(fileNameOnly, fileCount, align)
s = '
' % fileName
html.write(s)
imageCount += 1
html.close()
def getOptions():
options = {}
fileNameOnly = raw_input('html file name(without extension, default: image): ')
fileNameOnly = fileNameOnly.replace('\r', '')
if fileNameOnly == '':
fileNameOnly = 'image'
maxCountPerHtml = raw_input('max image count per html (default: unlimited): ')
maxCountPerHtml = maxCountPerHtml.replace('\r', '')
if maxCountPerHtml == '':
maxCountPerHtml = -1
else:
maxCountPerHtml = int(maxCountPerHtml)
replacePercentSign = True
align = 'center'
options['fileNameOnly'] = fileNameOnly
options['maxCountPerHtml'] = maxCountPerHtml
options['replacePercentSign'] = replacePercentSign
options['align'] = align
return options
if __name__ == "__main__":
options = getOptions()
fileList = getFileListInCurDir()
print 'getting file list done.'
imageList = extractImageFileList(fileList, options['replacePercentSign'])
print 'extract image file list done. %d image(s) found.' % len(imageList)
makeHtmlImageGallery(options['fileNameOnly'], imageList, options['maxCountPerHtml'], options['align'])
print 'all progress done.'
'Programming > Misc' 카테고리의 다른 글
| 파일 이름 바꾸기 - 파이썬 (0) | 2008/01/21 |
|---|---|
| 정규식 쓸때는 Regular Expression Workbench (2) | 2007/09/17 |
| HTML Gallery Python Script (0) | 2007/08/20 |
| C++ 생성자, 소멸자에서는 가상 함수를 호출하면 절대 안됩니다. (0) | 2007/08/01 |
| 최고의 오픈소스 프로젝트는?? (0) | 2007/08/01 |
| Multi Processing을 위한 OpenMP (0) | 2007/07/30 |



Prev
Rss Feed