태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.


'파이썬'에 해당되는 글 1건

  1. 2007/08/20 HTML Gallery Python Script
2007/08/20 23:35

HTML Gallery Python Script

이미지 폴더에 그림이 많을 때 그림들을 웹 브라우저로 볼 수 있게 HTML Gallery 파일을 만들어주는 파이썬 스크립트 입니다.

폴더에 그림이 수백장씩 있으면 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.'
Trackback 0 Comment 0