resize-photos

resize-photos permet créer des miniatures de photos en conservant les données “EXIF” (pour notamment conserver l'information de l'orientation des photos).
Ce script python a principalement été écrit pour fonctionner sous Windows.

Elements requis :
python 2.7
PIL
pyexiv 2

# -*- coding: utf-8 -*-
 
import os
import sys
import Image
import exiftool
import pyexiv2
from PIL import Image
from PIL.ExifTags import TAGS
 
albumdir = 'album'
thumbdir = albumdir + '/thumbs'
 
def mkdir(dirname):
  try:
    os.mkdir(dirname)
  except:
    pass
 
def maxSize(image, maxSize, method = 3):
  imAspect = float(image.size[0])/float(image.size[1])
  outAspect = float(maxSize[0])/float(maxSize[1])
 
  if imAspect >= outAspect:
    return image.resize((maxSize[0], int((float(maxSize[0])/imAspect) + 0.5)), method)
  else:
    return image.resize((int((float(maxSize[1])*imAspect) + 0.5), maxSize[1]), method)
 
 
 
def copy_metadata(filename_src, filename_dst):
  metadata_src = pyexiv2.ImageMetadata(filename_src)
  metadata_src.read()
  metadata_dest = pyexiv2.ImageMetadata(filename_dst)
  metadata_dest.read()
  metadata_src.copy(metadata_dest,exif=True)
  metadata_dest.write()
 
 
 
 
def processImage(filename_src, filename_dst):
 
  img_src = Image.open(filename_src)
 
  print filename_src
  print filename_dst
  img_src = maxSize(img_src, (1024, 768), Image.ANTIALIAS)
  img_src.save(filename_dst, 'JPEG', quality=85) 
 
  copy_metadata(filename_src, filename_dst)
 
 
 
 
 
 
def main():
  if len(sys.argv) < 2:
    print "Usage: resize-photos.py img-SRC-dir img-DST-dir"
    exit(0)
  else:
    img_SRC_dir = sys.argv[1] + '/'
    img_DST_dir = sys.argv[2] + '/'
 
  files = os.listdir(img_SRC_dir)
 
  for dirname, dirnames, filenames in os.walk(img_SRC_dir):
    for filename in filenames:
      # Récupérer le chemin complet du répertoire source 
      dirname_src = dirname
      # Récupérer le chemin complet du répertoire de destination 
      dirname_dst = dirname_src.replace(img_SRC_dir,img_DST_dir)
      # Récuperer le chemin complet du fichier source
      filename_src = os.path.join(dirname, filename)
      #récupére le chemin complet du fichier de destination
      filename_dst = filename_src.replace(img_SRC_dir,img_DST_dir)
      # Armoniser les chemins
      filename_src = filename_src.replace('/','\\')
      filename_dst = filename_dst.replace('/','\\')
      dirname_src = dirname_src.replace('/','\\')
      dirname_dst = dirname_dst.replace('/','\\')
      is_exist = os.path.isfile(filename_dst)
      if is_exist is False:
        if filename_src.lower().endswith('.jpg'):
          mkdir(dirname_dst)
          processImage(filename_src, filename_dst)
 
  print 'done'
 
if __name__ == "__main__":
  main()

 
asr/photos.txt · Dernière modification: 2016/03/29 23:31 (édition externe)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki