Skip to content

Instantly share code, notes, and snippets.

@PlastMan420
Created May 15, 2020 19:52
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 PlastMan420/8bd2fb5fe5816f96d567636a661ce5c5 to your computer and use it in GitHub Desktop.
Save PlastMan420/8bd2fb5fe5816f96d567636a661ce5c5 to your computer and use it in GitHub Desktop.
This applies a sketch effect to an image > python sketcher.py <input file arg>
from PIL import Image, ImageOps, ImageFilter
import os, sys
import numpy as np
from datetime import datetime
# Dodge blend effect
def dodge(front,back):
# The formula comes from http://www.adobe.com/devnet/pdf/pdfs/blend_modes.pdf
result = (back*256.0)/(256.0-front)
result[result>255]=255
result[front==255]=255
return result.astype('uint8')
def sketchEffect(srcdir):
# This opens an image and converts to grayscale
src = Image.open(srcdir).convert("L")
# Saves into a numpy array
src_arr = np.asarray(src)
# Invert the image
worksrc = ImageOps.invert(src)
# Histogram equalization
worksrc = ImageOps.equalize(worksrc, mask=None)
# Apply a gaussian filter
worksrc = worksrc.filter(ImageFilter.GaussianBlur(radius=90))
# Saves into a numpy array
worksrc_arr = np.asarray(worksrc)
# Apply color dodge
result = dodge(worksrc_arr, src_arr)
result = Image.fromarray(result)
# Histogram equalization (again)
result = ImageOps.equalize(result, mask=None)
result.save('sketch.jpg', quality=100)
input = sys.argv[1]
sketchEffect(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment