#!/usr/bin/python
#
#  ofr.py - script to generate O.R.I.G.I.N.A.L gallery structure on MS Windows
#  Author: Josef 'cornelius' Vybíral http://blog.vybiral.info
#
# USAGE:
# Copy this script to directory with photos you want to make gallery of and run it
#
# REQUIREMENTS:
# You need Python > 2.4 and Python Imaging Library
#
# ----------------------------------  
# LICENSE
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from PIL import Image
import glob, os

os.system("mkdir webgallery")
os.system("mkdir webgallery\\comments")
os.system("mkdir webgallery\\lq")
os.system("mkdir webgallery\\mq")
os.system("mkdir webgallery\\hq")
os.system("mkdir webgallery\\thumbs")

mq = 800, 600
lq = 640, 480
thumb = 120, 90
imgid = 1

for infile in glob.glob("*.jpg"):
    # processing thumbnail
    thumbnail = Image.open(infile)
    thumbnail.thumbnail(thumb, Image.ANTIALIAS)
    thumbnail.save("webgallery\\thumbs\\img-"+ str(imgid) + ".jpg", "JPEG")

    # processing LQ image
    lqimg = Image.open(infile)
    lqimg.thumbnail(lq, Image.ANTIALIAS)
    lqimg.save("webgallery\\lq\\img-"+ str(imgid) + ".jpg", "JPEG")

    # processing MQ image
    mqimg = Image.open(infile)
    mqimg.thumbnail(mq, Image.ANTIALIAS)
    mqimg.save("webgallery\\mq\\img-"+ str(imgid) + ".jpg", "JPEG")

    # processing HQ image (recompressing)
    hqimg = Image.open(infile)
    hqimg.save("webgallery\\hq\\img-"+ str(imgid) + ".jpg", "JPEG")

    # creating text files for comments
    name = "webgallery\\comments\\" + str(imgid) + ".txt"
    comment_file = file(name, 'w')
    data = "<span>image " + str(imgid) + "</span>"
    comment_file.write(data)
    comment_file.close()

    print "Image no. ", imgid, " has been processed.\n"

    # raising image number
    imgid = imgid + 1

# creating text files for comments
name = "webgallery\\info.txt"
comment_file = file(name, 'w')
data = "date|\nname|\ndescription|\nauthor|\n"
comment_file.write(data)
comment_file.close()
print "Conversion DONE. Now update info.txt file and upload your gallery :)"
