Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import errno
- import logging
- import multiprocessing as mp
- import os
- import os.path
- import shutil
- import sys
- import time
- import psycopg2
- import psycopg2.extras
- from PIL import Image
- from PIL.ExifTags import TAGS, GPSTAGS
- class Exif(object):
- def __init__(self, img):
- self.img = img
- def _set_img(self, value):
- self._img = value
- try:
- self.raw_tags = self._img._getexif()
- self.tags = dict((TAGS[tag], value) for (tag, value) in \
- self.raw_tags.iteritems() if tag in TAGS)
- self.gps_tags = dict((GPSTAGS[tag], value) for (tag, value) in\
- self.raw_tags.iteritems() if tag in GPSTAGS)
- except AttributeError as e:
- self.tags = {}
- self.gps_tags = {}
- def _get_img(self):
- return self._img
- img = property(_get_img, _set_img)
- class PImage(mp.Process):
- def __init__(self, queue, db, *args, **kwargs):
- self.queue = queue
- self.db = db
- self.log = mp.get_logger()
- self.log.setLevel(logging.DEBUG)
- handler = logging.FileHandler(LOG_FILENAME)
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
- handler.setFormatter(formatter)
- self.log.addHandler(handler)
- super(PImage, self).__init__(*args, **kwargs)
- def run(self):
- cursor = self.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
- while True:
- im_full_path = self.queue.get()
- if im_full_path == False:
- return True
- im_file_name = os.path.basename(im_full_path)
- im_rel_path = os.path.relpath(os.path.dirname(im_full_path), SRC)
- destination = os.path.realpath(os.path.join(TGT, im_rel_path))
- try:
- os.makedirs(destination)
- except OSError as e:
- if e.errno != errno.EEXIST:
- raise OSError(e)
- print im_file_name,
- try:
- im = Image.open(im_full_path)
- im_exif = Exif(im)
- if im.mode != 'RGB':
- im = im.convert('RGB')
- im.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
- im.save(os.path.join(destination, im_file_name), 'JPEG')
- except IOError as e:
- print '(!)',
- self.log.error('%s not imported', im_full_path)
- sys.stdout.flush()
- SRC = '/home/jcigar/mnt'
- TGT = '/home/jcigar/cr/images'
- THUMB_SIZE = (1024, 768)
- NUM_PROCESSES = 5
- DB_DSN = 'host=canis user=xxx dbname=xxx password=xxx'
- # Logging
- LOG_FILENAME = 'import.log'
- queue = mp.Queue(NUM_PROCESSES * 2)
- processes = [PImage(queue, psycopg2.connect(DB_DSN)) for i in\
- range(NUM_PROCESSES)]
- for p in processes:
- p.start()
- try:
- shutil.rmtree(TGT)
- print 'Deleting', TGT, 'recursively'
- except OSError as e:
- pass
- for (root, dirs, files) in os.walk(SRC):
- print '\n\nProcessing %s (%s files) (q=%s)' % (root, len(files),
- queue.qsize())
- for f in files:
- queue.put(os.path.join(root, f))
- queue.put(False)
- print "BEFORE JOIN"
- for p in processes:
- print "JOINING", p
- p.join()
- print "AFTER JOIN"
Add Comment
Please, Sign In to add comment