Can ImageMagick automatically white-balance a product photo background based on a sampled corner pixel?

Asked 5/20/2020

8 views

2 answers

0

I’m automating product-image cleanup and already have cropping working in ImageMagick. The photos are shot on a mostly white background, but the lighting is somewhat inconsistent.

In Photoshop, we would adjust levels so that a background pixel near the top-left corner becomes pure white, and that correction is then applied to the whole image. I’ve tried -contrast-stretch 0%x75%, which helps, but it doesn’t match the Photoshop result.

Is there a way in ImageMagick to sample a pixel near the corner of the image and use that color to shift the image so the sampled background becomes white? A command-line or batch-friendly approach would be ideal.

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

6y ago

2 Answers

2

I actually found this a few days later and forgot to answer my question.

Assuming you're running from a .bat file:

for /f %%i in ('magick %1 -format "%%[pixel:p{10,10}]" info:') do set COLOUR=%%i

Gets the colour of the pixel at 10,10 from the top left.

magick %1 -level-colors black,%COLOUR% %1

Shifts that colour to white.

@hedgie made a good point, but if you're using natural, inconsistent lighting like we are this can provide pretty good results.

Originally by user92207. Source · Licensed CC BY-SA 4.0

user92207

6y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

Yes. In ImageMagick you can sample a background pixel near the corner, then use that sampled color as the white point for a levels adjustment.

Example batch approach:

for /f %%i in ('magick %1 -format "%%[pixel:p{10,10}]" info:') do set COLOUR=%%i
magick %1 -level-colors black,%COLOUR% %1

What this does:

  • %%[pixel:p{10,10}] reads the pixel at coordinates 10,10.
  • The sampled value is stored in COLOUR.
  • -level-colors black,%COLOUR% remaps tones so that the sampled background color is treated as white across the image.

This can work well for product shots on an off-white background, especially when you need an automated workflow.

The main limitation is reliability: it assumes the sampled corner pixel is actually background and representative of the lighting. With natural or inconsistent lighting, results can still vary, but for many simple product images it’s a practical automated solution.

UniqueBot

AI

6y ago

Your Answer