views:

880

answers:

4

I want to take an image of a document that was photographed and make it look like it was scanned. Since a scanner will put a constant light source over the whole document, I want to achieve that effect on a photo of a document. The desired effect would be to remove any shadows or areas of low light (or at least make them less noticeable) and have the whole photo be fairly bright.

My first thought would be to locate the brightest part of the target image, and them make the whole image that brightness. Assuming that's even the right algorithm, how would I do it in PIL? Is there a get brightness method? etc?

(This is a follow-up to this earlier question.)

+2  A: 

As a first attempt, try thresholding the image. Dark areas become black, light areas become white. I haven't used PIL, but I imagine there's any easy way to do it.

Mr Fooz
+2  A: 

Try ImageChops.screen(image1, image2) with 2 copies of the image. If that's not satisfactory, try some of the other functions in the ImageChops module.

Also, you may want to convert it to grayscale first: ImageOps.grayscale(image).

cdonner
A: 

First try it manually in a image editing program, like GIMP. I think what you're looking for is adjusting brightness and contrast.

Jack Ha
no, it's more than since a darker area should get more brightness than an already bright area.
Greg
A: 

What type of image? If it's supposed to be ideally pure black and white, as with text pages, then your raw data probably is something like a grayscale gradient with varying levels of not-quite-black letters. Thresholding against a constant may give good results, but not if the illumination is too uneven or lens glare interferes. Threshold the image against a smoothed version of itself. Smooth it using PIL_usm.gblur(image, radius) where radius (in pixels) is something like ten, twenty or some value comparable to the width of elements of the letters. Quick hackcode from old notes just for illustration:

import Image
import PIL_usm
# see http://www.cazabon.com/pyCMS/PIL_usm.html for PIL_usm

img = Image.open(...)
sm = PIL_usm(img, 10)
thr = Image.ImageChops.subtract(img,sm, .001, 128)  
# or whatever works 4u...

OTOH, if these documents have photographs or other non-bilevel graphics, you'll need to be more clever.

DarenW
Interesting suggestion---have you tried it?
Norman Ramsey