Skip to content

Instantly share code, notes, and snippets.

@benawad
Last active August 29, 2015 14:08
Show Gist options
  • Save benawad/f179b789239dd9dda57d to your computer and use it in GitHub Desktop.
Save benawad/f179b789239dd9dda57d to your computer and use it in GitHub Desktop.
Command line script that takes a path to an image, grayscales image, and then saves the picture in the directory the original image was found in.
from PIL import Image
import os, shutil
print "Please enter location of image"
input = raw_input('> ')
path = os.path.abspath(input)
img = Image.open(path)
size = img.size
new_img = Image.new("L", size)
img.show()
for x in range(0, size[0]):
for y in range(0, size[1]):
loc = (x, y)
pixel = img.getpixel(loc)
val = (pixel[0] + pixel[1] + pixel[2]) / 3
new_img.putpixel(loc, val)
filename = "grey_" + os.path.basename(path)
save_to = os.path.dirname(path)
stored = os.getcwd() + '\\' + filename
new_img.show()
new_img.save(filename)
shutil.move(stored, save_to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment