Guest User

Untitled

a guest
Aug 13th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import errno
  2. import logging
  3. import multiprocessing as mp
  4. import os
  5. import os.path
  6. import shutil
  7. import sys
  8. import time
  9.  
  10. import psycopg2
  11. import psycopg2.extras
  12.  
  13. from PIL import Image
  14. from PIL.ExifTags import TAGS, GPSTAGS
  15.  
  16.  
  17. class Exif(object):
  18.  
  19. def __init__(self, img):
  20. self.img = img
  21.  
  22. def _set_img(self, value):
  23. self._img = value
  24. try:
  25. self.raw_tags = self._img._getexif()
  26. self.tags = dict((TAGS[tag], value) for (tag, value) in \
  27. self.raw_tags.iteritems() if tag in TAGS)
  28. self.gps_tags = dict((GPSTAGS[tag], value) for (tag, value) in\
  29. self.raw_tags.iteritems() if tag in GPSTAGS)
  30. except AttributeError as e:
  31. self.tags = {}
  32. self.gps_tags = {}
  33.  
  34. def _get_img(self):
  35. return self._img
  36.  
  37. img = property(_get_img, _set_img)
  38.  
  39.  
  40. class PImage(mp.Process):
  41.  
  42. def __init__(self, queue, db, *args, **kwargs):
  43. self.queue = queue
  44. self.db = db
  45. self.log = mp.get_logger()
  46. self.log.setLevel(logging.DEBUG)
  47.  
  48. handler = logging.FileHandler(LOG_FILENAME)
  49. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  50. handler.setFormatter(formatter)
  51.  
  52. self.log.addHandler(handler)
  53.  
  54. super(PImage, self).__init__(*args, **kwargs)
  55.  
  56. def run(self):
  57. cursor = self.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
  58. while True:
  59. im_full_path = self.queue.get()
  60. if im_full_path == False:
  61. return True
  62. im_file_name = os.path.basename(im_full_path)
  63. im_rel_path = os.path.relpath(os.path.dirname(im_full_path), SRC)
  64.  
  65. destination = os.path.realpath(os.path.join(TGT, im_rel_path))
  66. try:
  67. os.makedirs(destination)
  68. except OSError as e:
  69. if e.errno != errno.EEXIST:
  70. raise OSError(e)
  71.  
  72. print im_file_name,
  73.  
  74. try:
  75. im = Image.open(im_full_path)
  76. im_exif = Exif(im)
  77.  
  78. if im.mode != 'RGB':
  79. im = im.convert('RGB')
  80.  
  81. im.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
  82. im.save(os.path.join(destination, im_file_name), 'JPEG')
  83. except IOError as e:
  84. print '(!)',
  85. self.log.error('%s not imported', im_full_path)
  86.  
  87. sys.stdout.flush()
  88.  
  89.  
  90. SRC = '/home/jcigar/mnt'
  91. TGT = '/home/jcigar/cr/images'
  92. THUMB_SIZE = (1024, 768)
  93.  
  94. NUM_PROCESSES = 5
  95.  
  96. DB_DSN = 'host=canis user=xxx dbname=xxx password=xxx'
  97.  
  98. # Logging
  99.  
  100. LOG_FILENAME = 'import.log'
  101.  
  102. queue = mp.Queue(NUM_PROCESSES * 2)
  103.  
  104. processes = [PImage(queue, psycopg2.connect(DB_DSN)) for i in\
  105. range(NUM_PROCESSES)]
  106.  
  107. for p in processes:
  108. p.start()
  109.  
  110. try:
  111. shutil.rmtree(TGT)
  112. print 'Deleting', TGT, 'recursively'
  113. except OSError as e:
  114. pass
  115.  
  116. for (root, dirs, files) in os.walk(SRC):
  117. print '\n\nProcessing %s (%s files) (q=%s)' % (root, len(files),
  118. queue.qsize())
  119. for f in files:
  120. queue.put(os.path.join(root, f))
  121.  
  122. queue.put(False)
  123.  
  124. print "BEFORE JOIN"
  125. for p in processes:
  126. print "JOINING", p
  127. p.join()
  128. print "AFTER JOIN"
Add Comment
Please, Sign In to add comment