Download code

Jump to: navigation, search

Back to Photo_to_drawing_(Python)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

drawing.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Photo_to_drawing_(Python)?oldid=18737
14 
15 import Image
16 import ImageFilter
17 import ImageOps
18 import ImageDraw
19 
20 def dodge(a, b, alpha):
21     return min(int(a*255/(256-b*alpha)), 255)
22 def drawing(infile, outfile, blur=25, alpha=1.0):
23     im1 = Image.open(infile).convert("L")
24     im2 = im1.copy()
25     im2 = ImageOps.invert(im2)
26     for i in range(blur):
27         im2 = im2.filter(ImageFilter.BLUR)
28     width, height = im1.size
29     for x in range(width):
30         for y in range(height):
31             a = im1.getpixel((x, y))
32             b = im2.getpixel((x, y))
33             im1.putpixel((x, y), dodge(a, b, alpha))
34     im1.save(outfile)


hijacker
hijacker
hijacker
hijacker