edit · print · PDF

Please note that all the SIEpedia's articles address specific issues or questions raised by IAC users, so they do not attempt to be rigorous or exhaustive, and may or may not be useful or applicable in different or more general contexts.

Resizing a list of images with python

japp


#! /usr/bin/env python


#*******************************************************************************
#  Reduce el tamaño de imagenes JPEG, GIF o PNG
#  Necesita Python Image Library (PIL)
#
#  USO:  python miniaturas.py -f imagen_1.jpg imagen_2.gif ...
#        python miniaturas.py -a       # reduce todas las imagenes del directorio
#*******************************************************************************

import os, sys
import Image, math
from glob import glob

def printHelp():

   print " ----------------------------------------------------------"
   print " Reduce imagenes jpg, png o gif al tamaño indicado"
   print " USO: miniaturas imagen_1.jpg imagen_2.png ..."
   print "      miniaturas -a  (Todas las imagenes)"
   print " ----------------------------------------------------------"

options=sys.argv[1:]
files=''

if len(options)==0: printHelp()

else:
 optionNum=0      
 for option in options:
        optionNum=optionNum + 1
        if option[0]=='-':
           if option[1]=='a':     
                imagenes_jpg = glob.glob('*.jpg')
                imagenes_gif = glob.glob('*.gif')
                imagenes_png = glob.glob('*.png')
                files = imagenes_jpg + imagenes_gif + imagenes_png
                break
           elif option[1]=='f':
                files=sys.argv[optionNum + 1:]
                break
           else: printHelp()
        else: printHelp()       

# Crea el directorio "mini" si no existe
 if os.path.exists('mini')==0: os.mkdir('mini')

 if files!='':
   tamanho=int(raw_input('Tamaño máximo de lado (pixel): '))
   size= tamanho,tamanho
   num_imagenes=0
   for name in files:

        outfile = name + ".tmp"  
        if name != outfile:
          try:
            imagen = Image.open(name)
            formato = imagen.format
            imagen.thumbnail(size, Image.ANTIALIAS)

            if formato=='JPEG':
                imagen.save(outfile, "JPEG")   
                os.rename(outfile,'mini/' + outfile[:-4])
            elif formato=='PNG':
                imagen.save(outfile, "PNG")     
                os.rename(outfile,'mini/' + outfile[:-4])
            elif formato=='GIF':
                imagen.save(outfile, "GIF")     
                os.rename(outfile,'mini/' + outfile[:-4])       
            print "Reduciendo imagen: " + name

            num_imagenes=num_imagenes+1

          except IOError:
            print "Error en la imagen: ", name


   print '------------------------------------------------------------'
   print 'Reducidas(s) ' + str(num_imagenes) + ' imagen(es) en el directorio "mini/".'
   print ' '
 

Section: HOWTOs

edit · print · PDF
Page last modified on April 30, 2008, at 10:08 AM