Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Python: Convertir todos las las imágenes de una carpeta a otro tamaño.

Tags: print

Pequeño script que convierte todas las imágenes de una cierta carpeta a un nuevo tamaño que se especifica en los argumentos de la línea de comandos.

# -*- coding:utf-8 -*-
# Author: Jesús Mager
# Licence: GPL v.3


import os, sys
import Image

if len(sys.argv) != 3:
print "Syntaxis: imgsize.py xsize ysize"
exit()
else:
try:
x = int(sys.argv[1])
y = int(sys.argv[2])
except ValueError:
print "The arguments must be integers."
exit()
size = x,y

def makethumb(inf):
out = inf + ".new"
if inf.split(".")[-1]=="new":
print "%s: Skiped" % inf
return 0

if inf is not out:
try:
im = Image.open(inf)
im.thumbnail(size, Image.ANTIALIAS)
im.save(out, "JPEG")
print "%s: Done" % inf
except IOError:
print "Error opening %s" % inf


if __name__ == "__main__":
files = [ f for f in os.listdir(".") if os.path.isfile(os.path.join(".",f)) ]
for f in files:
makethumb(f)


This post first appeared on H1n1-Al, please read the originial post: here

Share the post

Python: Convertir todos las las imágenes de una carpeta a otro tamaño.

×

Subscribe to H1n1-al

Get updates delivered right to your inbox!

Thank you for your subscription

×