How to equalize background illumination?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
nicolai.rostov
Posts: 18
Joined: 2015-10-06T10:42:17-07:00
Authentication code: 1151

How to equalize background illumination?

Post by nicolai.rostov »

I've found what seems a very effective method here http://stackoverflow.com/a/4632685 to remove (or equalize, I'm not sure what technical term to use) background illumination from photographs of drawings or manuscripts, in order to arrive at a clean black-and-white image of that drawing by means of a simple threshold.

(The assumption here is that simply adjusting brightness and contrast will not do because of shadows, etc.)

Code: Select all

from PIL import Image
from PIL import ImageFilter
im = Image.open(r'c:\temp\temp.png')
white = im.filter(ImageFilter.BLUR).filter(ImageFilter.MaxFilter(15))

grey = im.convert('L')
width,height = im.size
impix = im.load()
whitepix = white.load()
greypix = grey.load()
for y in range(height):
    for x in range(width):
        greypix[x,y] = min(255, max(255 + impix[x,y][0] - whitepix[x,y][0], 255 + impix[x,y][7] - whitepix[x,y][8], 255 + impix[x,y][9] - whitepix[x,y][10]))

This allows us to go from this:
Image

to this:
Image

However, I don't know how to use the Python interface the author of the answer is using. Is it possible to achieve the same result using the convert tool from the command line? How?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to equalize background illumination?

Post by Bonzo »

fmw42 has a script you may be able to use: whiteboard
nicolai.rostov
Posts: 18
Joined: 2015-10-06T10:42:17-07:00
Authentication code: 1151

Re: How to equalize background illumination?

Post by nicolai.rostov »

Excellent. Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to equalize background illumination?

Post by fmw42 »

Besides my script whiteboard and textcleaner, see the IM function -lat. see http://www.imagemagick.org/script/comma ... ns.php#lat
Post Reply