Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import Image, sys, os
- def load(path):
- img = Image.open(path)
- # Obtenemos el tamano de la imagen
- width, height = img.size
- # Llamamos a la funcion
- gray(img, width, height)
- def gray(img, width, height):
- # Convertimos la imagen a RGB
- img = img.convert("RGB")
- # Se recorre la imagen en una matriz
- for i in range(width):
- for j in range(height):
- # Obtenemos los colores RGB pixel por pixel
- r, g, b = img.getpixel((i,j))
- # Cambiamos el RGB del pixel
- img.putpixel((i,j), ((r+g+b)/3, (r+g+b)/3, (r+g+b)/3))
- # Salvamos la imagen en un archivo nuevo
- img.save("nueva.png", "PNG")
- def main():
- # Llama la imagen desde un directorio
- try:
- path = sys.argv[1]
- if os.path.isfile(path):
- load(path)
- else:
- print "La imagen no existe"
- path = raw_input("ingresa el directorio de una imagen: ")
- load(path)
- except:
- path = raw_input("ingresa el directorio de una imagen: ")
- load(path)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement