Skip to content

Instantly share code, notes, and snippets.

@GabrielCastro

GabrielCastro/Pillow_profiling.py Secret

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.