| 1 |
ninoborges |
8 |
"""
|
| 2 |
|
|
11.19.06
|
| 3 |
|
|
This program will convert a folder of Jpegs and create a thumbs folder and a 800x600 folder.
|
| 4 |
|
|
This will prep it for the website. Also I will soon add a module for autocreating the nec
|
| 5 |
|
|
HTML to link it all.
|
| 6 |
|
|
"""
|
| 7 |
|
|
|
| 8 |
|
|
import os, sys
|
| 9 |
|
|
import Image
|
| 10 |
|
|
|
| 11 |
|
|
#size = 128, 128
|
| 12 |
|
|
#thumbSize = 160, 120
|
| 13 |
|
|
finalSize = 800, 600
|
| 14 |
|
|
|
| 15 |
|
|
def CreateWebSize(jpgDir, newExt, size):
|
| 16 |
|
|
for infile in os.listdir(jpgDir):
|
| 17 |
|
|
print infile
|
| 18 |
|
|
if os.path.exists(jpgDir +"\\new"):
|
| 19 |
|
|
pass
|
| 20 |
|
|
else:
|
| 21 |
|
|
os.mkdir(jpgDir + "\\new")
|
| 22 |
|
|
outfile = os.path.splitext(jpgDir + "\\new\\" + infile)[0] + newExt
|
| 23 |
|
|
if infile != outfile:
|
| 24 |
|
|
try:
|
| 25 |
|
|
im = Image.open(jpgDir + "\\" + infile)
|
| 26 |
|
|
newFile = im.resize(size, Image.ANTIALIAS)
|
| 27 |
|
|
newFile.save(outfile, "JPEG")
|
| 28 |
|
|
except IOError:
|
| 29 |
|
|
print "cannot create webPhoto for", infile
|
| 30 |
|
|
|
| 31 |
|
|
def CreateThumbs(jpgDir, newExt):#, size):
|
| 32 |
|
|
for infile in os.listdir(jpgDir):
|
| 33 |
|
|
print infile
|
| 34 |
|
|
#outfile = os.path.splitext(jpgDir + "\\" + infile)[0] + ".thumbnail"
|
| 35 |
|
|
if os.path.exists(jpgDir +"\\new"):
|
| 36 |
|
|
pass
|
| 37 |
|
|
else:
|
| 38 |
|
|
os.mkdir(jpgDir + "\\new")
|
| 39 |
|
|
outfile = os.path.splitext(jpgDir + "\\new\\" + infile)[0] + newExt
|
| 40 |
|
|
if infile != outfile:
|
| 41 |
|
|
try:
|
| 42 |
|
|
im = Image.open(jpgDir + "\\" + infile)
|
| 43 |
|
|
# this needs to be better...
|
| 44 |
|
|
print im.size
|
| 45 |
|
|
if im.size == (660, 440):
|
| 46 |
|
|
size = 160, 120
|
| 47 |
|
|
if im.size == (440,660):
|
| 48 |
|
|
size = 120, 160
|
| 49 |
|
|
im.thumbnail(size, Image.ANTIALIAS)
|
| 50 |
|
|
im.save(outfile, "JPEG")
|
| 51 |
|
|
except IOError:
|
| 52 |
|
|
print "cannot create thumbnail for", infile
|
| 53 |
|
|
|
| 54 |
|
|
def CreateHTMLLinks(newDir, albumName):
|
| 55 |
|
|
fileList = os.listdir(newDir)
|
| 56 |
|
|
fileList.sort()
|
| 57 |
|
|
fileList.reverse()
|
| 58 |
|
|
CreatePage(newDir, albumName,0, fileList)
|
| 59 |
|
|
|
| 60 |
|
|
def CreatePage(newDir,albumName,pageNum, fileList):
|
| 61 |
|
|
count = 0
|
| 62 |
|
|
rowCount = 0
|
| 63 |
|
|
output = open(newDir + "\\" + albumName +"Page%s.html"%pageNum,'w')
|
| 64 |
|
|
output.write('{% extends "PhotoAlbum/index.html" %}\n\n\n\n{% block content %}\n<center>\n<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>\n<tr>\n')
|
| 65 |
|
|
while fileList:
|
| 66 |
|
|
#print len(fileList)
|
| 67 |
|
|
while rowCount < 10:
|
| 68 |
|
|
print rowCount
|
| 69 |
|
|
jpgFile = fileList.pop()
|
| 70 |
|
|
print jpgFile
|
| 71 |
|
|
if os.path.splitext(jpgFile)[1] == ".thumbnail":
|
| 72 |
|
|
if count > 4:
|
| 73 |
|
|
output.write("</tr>\n<tr>\n")
|
| 74 |
|
|
rowCount = rowCount +1
|
| 75 |
|
|
count = 0
|
| 76 |
|
|
output.write('<td align ="center" style="padding: 1px; border-top: solid; border-bottom: solid; border-left: solid; border-right: solid; background: #000000"><a href="%s"><img SRC="%s"></a></td>\n'%(os.path.splitext(jpgFile)[0]+'.jpeg',jpgFile))
|
| 77 |
|
|
count = count + 1
|
| 78 |
|
|
else:
|
| 79 |
|
|
pageNum = pageNum + 1
|
| 80 |
|
|
output.write("</tr></TABLE>\n</center>\n{% endblock %}")
|
| 81 |
|
|
output.close()
|
| 82 |
|
|
CreatePage(newDir,albumName,pageNum,fileList)
|
| 83 |
|
|
|
| 84 |
|
|
if __name__ == '__main__':
|
| 85 |
|
|
jpgDir = r"\\10.10.0.12\html\media\webPhotoAlbum\Proofs"
|
| 86 |
|
|
newDir = jpgDir + "\\new"
|
| 87 |
|
|
albumName = "Proofs"
|
| 88 |
|
|
print "Creating thumbnails..."
|
| 89 |
|
|
#CreateThumbs(jpgDir,".thumbnail")#, thumbSize)
|
| 90 |
|
|
print "done."
|
| 91 |
|
|
print "Creating web sized pictures..."
|
| 92 |
|
|
#CreateWebSize(jpgDir,".jpeg",finalSize)
|
| 93 |
|
|
print "done"
|
| 94 |
|
|
print "creating HTML matrix..."
|
| 95 |
|
|
CreateHTMLLinks(r"\\10.10.0.12\html\media\webPhotoAlbum\Proofs",albumName)
|
| 96 |
|
|
CreateHTMLLinks(newDir, albumName)
|
| 97 |
|
|
print "done"
|
| 98 |
|
|
|