PYTHON Script, image resize

Convertir des images pour le web de façon récursive et en reproduisant a la sortie la même arborescence que dans le répertoire d’origine,
Ce script est appeler a être modifier souvent, pour finir par arriver vers ce que je veux faire multi-threader la conversion et que çe soit le plus efficace possible ^^

import os
import string
import mimetypes
import Image
import ImageFilter
 
class pix:
    def __init__(self, pixdir, outputdir):
        self.pixdir = pixdir
        self.outputdir = outputdir
        self.mimeArray = ['image/jpeg', 'image/png']
        self.prepare()
        self.overwrite = 'N'
 
    def prepare(self):
        if(os.path.exists(self.outputdir) == 0):
            print("mkdir %s"%(self.outputdir))
            os.mkdir(self.outputdir)
        pixArray = {}
        i = 0
        for path, dirs, files in os.walk(self.pixdir):
            tmp = os.path.join(self.outputdir , path.replace(self.pixdir, ''))
            if os.path.exists(tmp) == 0:
                os.mkdir(tmp)
                print("mkdir %s"%(tmp))
            for file in files:
                f_path = os.path.join(path, file)
                f_mime = mimetypes.guess_type(f_path)
                if f_mime[0] in self.mimeArray:
                    f_size = os.path.getsize(f_path)
                    pixArray[i] = {'oldpath': os.path.join(path, file),
                                   'newpath': os.path.join(tmp, file.lower()),
                                   'size': self.o2ko(f_size),
                                   'mimetype': f_mime[0]}
                    i += 1
        self.count = i
        self.pixArray = pixArray
 
    def o2ko(self, o):
        return o/1024
 
    def convert(self, oldpath, newpath):
        try:
            bf_size = self.o2ko(os.path.getsize(oldpath))
            bi = Image.open(oldpath)
 
            if bi.size[0] > 2500:
                w = bi.size[0] * 50 / 100
                h = bi.size[1] * 50 / 100
            else:
                w = bi.size[0]
                h = bi.size[1]
 
            bi = bi.resize((w, h), Image.ANTIALIAS)
            bi.save(newpath, 'JPEG')     
            ai = Image.open(newpath)
            af_size = self.o2ko(os.path.getsize(newpath))
            print("\n[%ix%i %sKo %s] %s\n   to \n[%ix%i %sKo %s] %s"%(bi.size[0],
                                                                      bi.size[1],
                                                                      bf_size,
                                                                      bi.mode,
                                                                      oldpath,
                                                                      ai.size[0],
                                                                      ai.size[1],
                                                                      af_size,
                                                                      ai.mode,
                                                                      newpath))
        except IOError:
            print("\nimage file %s is endomaged"%(oldpath))
 
    def run(self):
        for i in self.pixArray:
            if os.path.exists(self.pixArray[i]['newpath']) == 1:
                if self.overwrite != 'ALL' and self.overwrite != 'NALL':
                    self.overwrite = raw_input("Overwrite %s [Y,N,ALL,NALL]: [N] "%(self.pixArray[i]['newpath']))
                if self.overwrite not in ['Y', 'N', 'ALL', 'NALL']:
                    self.overwrite = raw_input("Please answer [Y,N,ALL,NALL]: [N] ")
 
            if self.overwrite == 'Y' or self.overwrite == 'ALL':
                self.convert(self.pixArray[i]['oldpath'], self.pixArray[i]['newpath'])
            if self.overwrite == 'NALL' or os.path.exists(self.pixArray[i]['newpath']) == 0:
                self.convert(self.pixArray[i]['oldpath'], self.pixArray[i]['newpath'])
 
    def stats(self, dir):
        n_files = 0
        d_size = 0
        for path, dirs, files in os.walk(dir):
            for file in files:
                f_path = os.path.join(path, file)
                n_files += 1
                d_size = d_size + os.path.getsize(f_path)
        return {'n_files': n_files, 'd_size': d_size}
 
    def __del__(self):
        before = self.stats(self.pixdir)
        after = self.stats(self.outputdir)
        print('Before: %i files for %sKo'%(before['n_files'], self.o2ko(before['d_size'])))
        print('After : %i files for %sKo'%(after['n_files'], self.o2ko(after['d_size'])))
 
 
pix = pix('/home/hio/Photos/', # from
          '/home/hio/Photos_Web/') # to
pix.run()

Comments are closed.