Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL Outline image increase thicknesses

I know how to find the edges from a picture. But I would like to have the outline edges be thicker for example width 9.

    from PIL import Image, ImageFilter

    image = Image.open('your_image.png')
    image = image.filter(ImageFilter.FIND_EDGES, width=9)
    image.save('new_name.png') 

is that possible?

like image 625
ben olsen Avatar asked Oct 14 '25 04:10

ben olsen


1 Answers

You can find edges and fatten them like this:

#!/usr/bin/env python3

from PIL import Image, ImageMorph, ImageFilter

# Open star image and ensure greyscale
im = Image.open('star.png').convert('L')

# Detect edges and save
edges = im.filter(ImageFilter.FIND_EDGES)
edges.save('DEBUG-edges.png')

# Make fatter edges and save
fatEdges = edges.filter(ImageFilter.MaxFilter)
fatEdges.save('DEBUG-fatEdges.png')

# Make very fat edges and save
veryFatEdges = edges.filter(ImageFilter.MaxFilter(7))
veryFatEdges.save('DEBUG-veryFatEdges.png')

enter image description here

Top-left=original, top-right=edges, bottom-left=fat edges, bottom-right=very fat edges.

You could use ImageMorph module for more controlled morphology, but the maximum filter is very effective as it is.

like image 93
Mark Setchell Avatar answered Oct 16 '25 17:10

Mark Setchell