How do I -level an image based on the "darkest" pixel in a given area?
Posted: 2016-12-14T23:19:22-07:00
I have some photographs I shot against a theoretically white background. The reality is that the background is hardly white. I'd like to adjust the image so that the background is absolutely white. The remainder of the image should get the same adjustment, as it was subject to the same lighting. I've got a pile of these images, so I'd like to automate the process.
In my favor, I have a large block in the corner of every image that is guaranteed to be background, the upper right 160x160 pixels. So my theory is that I can grab that subset of the image, find the "darkest" pixel, and use that as the reference white point.
Here's one of my input images: https://dl.dropboxusercontent.com/u/380 ... estion.jpg (It's a miniature from the board game Tannhauser.)
After much experimentation, here's what I ended up with:
(I'm not terribly fluent in awk; my apologies if that makes you cringe.)
It seems to work. The backgrounds are absolutely white, although I don't know if I over-did it.
In my favor, I have a large block in the corner of every image that is guaranteed to be background, the upper right 160x160 pixels. So my theory is that I can grab that subset of the image, find the "darkest" pixel, and use that as the reference white point.
Here's one of my input images: https://dl.dropboxusercontent.com/u/380 ... estion.jpg (It's a miniature from the board game Tannhauser.)
After much experimentation, here's what I ended up with:
Code: Select all
#! /bin/sh
DARKLEVEL=`convert input.jpg -format %c -crop 160x160+0+0 -fx 'min(r,g,b)' histogram:info:- | head -n1 | awk -F'(' '{print $2}' | awk -F, '{print int(100*$1/255)}'`
convert input.jpg -level 0,$DARKLEVEL% output.jpg
It seems to work. The backgrounds are absolutely white, although I don't know if I over-did it.
- Is there a better way to accomplish my goal?
- Does what I'm doing actually accomplish my goal?
Code: Select all
Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-11-29 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib