Skip to content

Instantly share code, notes, and snippets.

@GabrielCastro
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GabrielCastro/585cc5cbfd98b12b4ed7 to your computer and use it in GitHub Desktop.
Save GabrielCastro/585cc5cbfd98b12b4ed7 to your computer and use it in GitHub Desktop.
import os
from PIL import Image
from PIL import ImageFilter
from PIL import ImageOps
import cProfile
import pstats
def profile(name, profiling_callable):
pr = cProfile.Profile()
pr.enable()
profiling_callable()
pr.disable()
stats = pstats.Stats(pr)
stats.sort_stats('time')
stats.stream = open(os.path.join('out', name + '.stats.txt'), 'w')
stats.print_stats()
stats.stream.close()
# the image we are working on
im = None
im = Image.open("paris.jpg")
im.load()
if not os.path.exists('out'):
os.makedirs('out')
def gaussian_blur():
global im
processed = im.filter(ImageFilter.GaussianBlur(2))
processed.save(os.path.join('out', 'paris_gaussian_blur.jpeg'), 'JPEG')
def blur():
global im
processed = im.filter(ImageFilter.BLUR)
processed.save(os.path.join('out', 'paris_blur.jpeg'), 'JPEG')
def auto_contrast():
global im
processed = ImageOps.autocontrast(im)
processed.save(os.path.join('out', 'paris_auto_contrast.jpeg'), 'JPEG')
def grey_scale():
global im
processed = ImageOps.grayscale(im)
processed.save(os.path.join('out', 'paris_grey_scale.jpeg'), 'JPEG')
if __name__ == "__main__":
profile('gaussian_blur', gaussian_blur)
profile('blur', blur)
profile('auto_contrast', auto_contrast)
profile('grey_scale', grey_scale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment