Queenvictoria
Queenvictoria

Reputation: 401

Autolevels over an entire image while only sampling from a smaller area

I have some poorly scanned images (around 0.25M) that I need to adjust automatically. The outside edges (about 20% of the area) are throwing off the auto levels commands.

Is there a way to calculate the image histogram from a subset of the image data while executing autolevels over the entire area?

eg: convert **-samplearea [box]** -autolevels infile.png

Upvotes: 2

Views: 811

Answers (1)

emcconville
emcconville

Reputation: 24439

Imagemagick's -autolevels command will calculate the min/max of the image, and passes it to the -level command. Calculating what the autolevel values would be from an image's region would be just a -crop, -format, and info:.

convert source.png -crop $geometry -format "%[min],%[max]" info:

Where $geometry is defined.

So if I wanted apply level correction in respect to the area in the top-left corner of the image, my commands would read:

# Grab the area to analyze (top-left, but not on edge)
geometry="50x50+5+5"
# Grab values
levels=$(convert source.jpeg -crop $geometry -format "%[min],%[max]" info:)
# Apply to whole image
convert source.jpeg -level $levels out.jpeg

Upvotes: 2

Related Questions