Crop image based on brightest colour

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
makrin
Posts: 2
Joined: 2013-03-24T13:21:04-07:00
Authentication code: 6789

Crop image based on brightest colour

Post by makrin »

Hi all,

I've been looking around the forum for a few hours now to try and find a possible answer to my problem. Haven't found anything... yet.

Problem:
I need to create individual colour swatch images from product images.

1000 x 1000px image of a nail varnish bottle (face on) Sample: http://asda.scene7.com/is/image/Asda/46 ... 0&hei=1000
The images vary in width and height, but all have been trimmed to remove as much white space.

Is it possible (using cmd line) to evaluate the image, find the brightest colour and then crop a 50x50px section and save as new image?

At the moment, I'm cropping the images by hand... only 1660 to go! :(

Thanks in advance if anyone can help :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop image based on brightest colour

Post by snibgo »

I don't understand what you want. Can you supply a sample showing what 50x50 you would want from this?
snibgo's IM pages: im.snibgo.com
makrin
Posts: 2
Joined: 2013-03-24T13:21:04-07:00
Authentication code: 6789

Re: Crop image based on brightest colour

Post by makrin »

Sorry, I'll explain a bit better.

I have a lot of product images (nail varnishes, lipsticks, eye shadow etc) and each product picture shows the different colour of the product. So what I'm trying to do (and failing hah) is to create a 50x50 crop of the product colour.

At the moment, I'm going through each picture and cropping a small portion of the colour and saving that as new image.

Sample:
Main product image: http://asda.scene7.com/is/image/Asda/4656345_main
Swatch colour: http://asda.scene7.com/is/image/Asda/46 ... =20&hei=20

The product images are all 2000px in width with varying heights, and the actual colour of the product is shown within different places within the image.

As you'll see, if I place fixed cooridnates to crop the colour for the nail colour, those coordinates won't work for the lipbalm, it'll crop somewhere different, thus not picking up the product colour.
Lip Balm: http://asda.scene7.com/is/image/Asda/4656247_main
Nail Colour: http://asda.scene7.com/is/image/Asda/4656345_main


I was wondering if IM has a feature whereby it can ignore the colours white and black to find the most prominent colour and then create crop of the colour and save as a new image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop image based on brightest colour

Post by snibgo »

I can't help suspecting that MaxFactor have already done this work for you.

The bottle cap and sides aren't black; they are various shades of grey. Fortunately they are exactly grey, with no colour cast at all. You also need to ignore the lettering on the bottle.

IM can readily produce a histogram of the image colours (Windows command):

Code: Select all

convert asda2.jpg -format %c histogram:info:- | sort /R /O asdaHist.txt
By scanning down the resulting text file, I can see the first non-grey colour is (58,17,13).

Another approach: create a HALD file that transforms all pixels that are some shade of grey into white, then trim the image. Then the top entry in the histogram will be "genuine".

Once you know the predominant colour in the image, you can find where it occurs with "compare -subimage-search", then crop at + or - 25 pixels around that location. You should then check that this swatch doesn't contain any colours that are too far from the central one. You might do this with fuzz, or by calculating the standard deviation of the swatch.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image based on brightest colour

Post by fmw42 »

Is it possible (using cmd line) to evaluate the image, find the brightest colour and then crop a 50x50px section and save as new image?
what do you mean by brightest color?

any white from the background would be the brightest. even some other color on the bottle might be brighter than the color you really want.

I think you need to generate a reduce color histogram and filter it out for colors that are not grayish. Then find the most frequent color (though some other part of the item may be more frequent). Then once you get the color, you can list out all the similar colors within some fuzz value. Then find some region that is contiguous with that color. Then crop at the center of that region.

Even this may not work well. You somehow need to know what color is the important color. If it is only red hues you want, you can process the image into HSL or HCL and use the hue channel to locate some good region of that shade.
Post Reply