From 1a41b02852c9cf35c7b27a67c65cd4f35eb0a26c Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 15 Oct 2010 23:52:51 +0200 Subject: [PATCH] split pywiUpload.py in modules (image, utils) --- lib/image.py | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/utils.py | 32 +++++++++ pywiUpload.py | 169 ++-------------------------------------------- 3 files changed, 220 insertions(+), 164 deletions(-) create mode 100644 lib/image.py create mode 100644 lib/utils.py diff --git a/lib/image.py b/lib/image.py new file mode 100644 index 0000000..4c68a76 --- /dev/null +++ b/lib/image.py @@ -0,0 +1,183 @@ +#! /usr/bin/env python +# -*- coding:utf-8 -*- + +# pywiUpload - Piwigo gallery generator +# Copyright (C) 2009-2010 Simon - saimon.org +# +# 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, see http://www.gnu.org/licenses/ + +"""Create a gallery of images. + +Resize images, create thumbnails with some options (rename images, squared +thumbs, ...). +""" + +import os +import Image +import ImageDraw +from utils import get_filelist + + +class Gallery: + "Prepare a gallery of images for Piwigo" + + def __init__(self, params): + self.filepath = "" + self.galname = "" + self.params = params + + def getpath(self, pathname): + "return abslolute path from params dict" + return os.path.join(self.filepath, self.params[pathname]) \ + if self.params.has_key(pathname) else "" + + def create_gallery(self, path): + "create image gallery" + imglist = get_filelist(path, self.params['fileExtList']) + print "Found %i images in %s" % (len(imglist), path) + + while self.galname == "": + self.galname = raw_input('Enter gallery name: ') + + self.filepath = os.path.join(os.path.dirname(imglist[0]), + self.galname) + + print "Create output dir ..." + try: + os.makedirs(self.getpath('thumb_dir')) + except OSError: + pass + + if self.params['bigimg']: + try: + os.mkdir(self.getpath('bigimg_dir')) + except OSError: + pass + + return (self.galname, self.process_images(imglist)) + + def process_images(self, imglist): + "prepare images" + imglist.sort() + out_imglist = [] + out_thumblist = [] + out_bigimglist = [] + + imrename = raw_input("Rename images ('name01.jpg') ? (y/[n]) ") + if imrename == 'y': + count = 1 + imgname = raw_input('Enter new image name: ') + nfill = 2 if (len(imglist)<100) else 3 + + if self.params['copyright']: + copyrightmsg = raw_input('Enter copyright message: ') + copyrightmsg = '\xa9 ' + copyrightmsg + + # loop on images + for f in imglist: + filename = os.path.split(f)[1] + im = Image.open(f) + + if imrename == 'y': + filename = imgname+str(count).zfill(nfill)+'.jpg' + print "%s > %s" % (os.path.split(f)[1], filename) + count += 1 + else: + print "%s" % filename + + if self.params['bigimg']: + im.save(os.path.join(self.getpath('bigimg_dir'), filename), + quality=self.params['jpgquality']) + out_bigimglist.append(os.path.join(self.getpath('bigimg_dir'), + filename)) + + # resize image + if im.size[0] > im.size[1]: + im_size = (self.params['im_width'], self.params['im_height']) + else: + im_size = (self.params['im_height'], self.params['im_width']) + im2 = im.resize(im_size, Image.ANTIALIAS) + + # create thumbnail + if self.params['squarethumb']: + if im.size[0] > im.size[1]: + offset = (im.size[0] - im.size[1])/2 + box = (offset, 0, im.size[0]-offset, im.size[1]) + else: + offset = (im.size[1] - im.size[0])/2 + box = (0, offset, im.size[0], im.size[1]-offset) + + im = im.crop(box) + thumbsize = (self.params['thumb_width'], + self.params['thumb_width']) + else: + thumbsize = (self.params['thumb_width'], + self.params['thumb_height']) + + im.thumbnail(thumbsize, Image.ANTIALIAS) + + # copyright + if self.params['copyright']: + draw = ImageDraw.Draw(im2) + draw.text((5, im2.size[1]-15), copyrightmsg) + + # save + im.save(os.path.join(self.getpath('thumb_dir'), + self.params['thumb_prefix']+filename), + quality=self.params['jpgquality']) + im2.save(os.path.join(self.filepath, filename), + quality=self.params['jpgquality']) + + out_thumblist.append(os.path.join(self.getpath('thumb_dir'), + self.params['thumb_prefix']+ + filename)) + out_imglist.append(os.path.join(self.filepath, filename)) + + if self.params['exif']: + self.process_exif(f, os.path.join(self.filepath, filename)) + + return [out_imglist, out_thumblist, out_bigimglist] + + def process_exif(self, srcfile, dstfile): + "copy exif metadatas from src to dest images" + try: + import pyexiv2 + except ImportError: + self.params['exif'] = 0 + print "Error: install pyexiv2 module to use exif metadatas." + return + + if pyexiv2.version_info[1] == 1: + src = pyexiv2.Image(srcfile) + dst = pyexiv2.Image(dstfile) + src.readMetadata() + dst.readMetadata() + try: + src.copyMetadataTo(dst) + except: + print "Error: metadata not copied for %s." % srcfile + return + dst.writeMetadata() + else: + src = pyexiv2.ImageMetadata(srcfile) + dst = pyexiv2.ImageMetadata(dstfile) + src.read() + dst.read() + try: + src.copy(dst) + except: + print "Error: metadata not copied for %s." % srcfile + return + dst.write() + diff --git a/lib/utils.py b/lib/utils.py new file mode 100644 index 0000000..b2fb812 --- /dev/null +++ b/lib/utils.py @@ -0,0 +1,32 @@ +#! /usr/bin/env python +# -*- coding:utf-8 -*- + +# pywiUpload - Piwigo gallery generator +# Copyright (C) 2009-2010 Simon - saimon.org +# +# 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, see http://www.gnu.org/licenses/ + +"""Various stuff. + +- Fonctions used to manage files (get_filelist). +""" + +import os + +def get_filelist(directory, extensions): + "get list of files of particular extensions" + filelist = [os.path.normcase(f) for f in os.listdir(directory)] + return [os.path.join(directory, f) for f in filelist \ + if os.path.splitext(f)[1] in extensions] + diff --git a/pywiUpload.py b/pywiUpload.py index 388d11e..59e1757 100755 --- a/pywiUpload.py +++ b/pywiUpload.py @@ -2,7 +2,7 @@ # -*- coding:utf-8 -*- # pywiUpload - Piwigo gallery generator -# Copyright (C) 2009 saimon.org +# Copyright (C) 2009 - saimon.org # # 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 @@ -26,174 +26,16 @@ This script resize images, create thumbnails with some options __author__ = "Saimon (contact at saimon dot org)" __version__ = "0.8" __date__ = "20100722" -__copyright__ = "Copyright (C) 2009 saimon.org" +__copyright__ = "Copyright (C) 2009 - saimon.org" __license__ = "GPL" import os import sys -import Image -import ImageDraw from configobj import ConfigObj +from optparse import OptionParser from lib.ftp import FtpUpload +from lib.image import Gallery -class PywiUpload: - "Prepare a gallery of images for Piwigo" - - def __init__(self, params): - self.filepath = "" - self.galname = "" - self.params = params - - def getpath(self, pathname): - "return abslolute path from params dict" - return os.path.join(self.filepath, self.params[pathname]) \ - if self.params.has_key(pathname) else "" - - def create_gallery(self, path): - "create image gallery" - imglist = list_files_in_dir(path, self.params['fileExtList']) - print "Found %i images in %s" % (len(imglist), path) - - while self.galname == "": - self.galname = raw_input('Enter gallery name: ') - - self.filepath = os.path.join(os.path.dirname(imglist[0]), - self.galname) - - print "Create output dir ..." - try: - os.makedirs(self.getpath('thumb_dir')) - except OSError: - pass - - if self.params['bigimg']: - try: - os.mkdir(self.getpath('bigimg_dir')) - except OSError: - pass - - return (self.galname, self.process_images(imglist)) - - def process_images(self, imglist): - "prepare images" - imglist.sort() - out_imglist = [] - out_thumblist = [] - out_bigimglist = [] - - imrename = raw_input("Rename images ('name01.jpg') ? (y/[n]) ") - if imrename == 'y': - count = 1 - imgname = raw_input('Enter new image name: ') - nfill = 2 if (len(imglist)<100) else 3 - - if self.params['copyright']: - copyrightmsg = raw_input('Enter copyright message: ') - copyrightmsg = '\xa9 ' + copyrightmsg - - # loop on images - for f in imglist: - filename = os.path.split(f)[1] - im = Image.open(f) - - if imrename == 'y': - filename = imgname+str(count).zfill(nfill)+'.jpg' - print "%s > %s" % (os.path.split(f)[1], filename) - count += 1 - else: - print "%s" % filename - - if self.params['bigimg']: - im.save(os.path.join(self.getpath('bigimg_dir'), filename), - quality=self.params['jpgquality']) - out_bigimglist.append(os.path.join(self.getpath('bigimg_dir'), - filename)) - - # resize image - if im.size[0] > im.size[1]: - im_size = (self.params['im_width'], self.params['im_height']) - else: - im_size = (self.params['im_height'], self.params['im_width']) - im2 = im.resize(im_size, Image.ANTIALIAS) - - # create thumbnail - if self.params['squarethumb']: - if im.size[0] > im.size[1]: - offset = (im.size[0] - im.size[1])/2 - box = (offset, 0, im.size[0]-offset, im.size[1]) - else: - offset = (im.size[1] - im.size[0])/2 - box = (0, offset, im.size[0], im.size[1]-offset) - - im = im.crop(box) - thumbsize = (self.params['thumb_width'], - self.params['thumb_width']) - else: - thumbsize = (self.params['thumb_width'], - self.params['thumb_height']) - - im.thumbnail(thumbsize, Image.ANTIALIAS) - - # copyright - if self.params['copyright']: - draw = ImageDraw.Draw(im2) - draw.text((5, im2.size[1]-15), copyrightmsg) - - # save - im.save(os.path.join(self.getpath('thumb_dir'), - self.params['thumb_prefix']+filename), - quality=self.params['jpgquality']) - im2.save(os.path.join(self.filepath, filename), - quality=self.params['jpgquality']) - - out_thumblist.append(os.path.join(self.getpath('thumb_dir'), - self.params['thumb_prefix']+ - filename)) - out_imglist.append(os.path.join(self.filepath, filename)) - - if self.params['exif']: - self.process_exif(f, os.path.join(self.filepath, filename)) - - return [out_imglist, out_thumblist, out_bigimglist] - - def process_exif(self, srcfile, dstfile): - "copy exif metadatas from src to dest images" - try: - import pyexiv2 - except ImportError: - self.params['exif'] = 0 - print "Error: install pyexiv2 module to use exif metadatas." - return - - if pyexiv2.version_info[1] == 1: - src = pyexiv2.Image(srcfile) - dst = pyexiv2.Image(dstfile) - src.readMetadata() - dst.readMetadata() - try: - src.copyMetadataTo(dst) - except: - print "Error: metadata not copied for %s." % srcfile - return - dst.writeMetadata() - else: - src = pyexiv2.ImageMetadata(srcfile) - dst = pyexiv2.ImageMetadata(dstfile) - src.read() - dst.read() - try: - src.copy(dst) - except: - print "Error: metadata not copied for %s." % srcfile - return - dst.write() - - -def list_files_in_dir(directory, file_ext_list): - "get list of files of particular extensions" - filelist = [os.path.normcase(f) for f in os.listdir(directory)] - return [os.path.join(directory, f) for f in filelist \ - if os.path.splitext(f)[1] in file_ext_list] def read_params(config_file): "Read params from a config file" @@ -215,7 +57,6 @@ def read_params(config_file): def main(): "main program" - from optparse import OptionParser # command line options usage = "usage: %prog [options]" @@ -239,7 +80,7 @@ def main(): params = read_params(config_file) # create gallery - gallery = PywiUpload(params) + gallery = Gallery(params) path = options.imgpath if options.imgpath else os.getcwd() (galleryname, out_filelist) = gallery.create_gallery(path)