How to extract dominant colours from an image?

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
joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

How to extract dominant colours from an image?

Post by joshuafinny » 2016-01-11T05:21:50-07:00

I tried using
convert tree.gif -unique-colors -depth 16 txt:-

Here's the situation:
I have a list of images with a background(textured or plain) and a product on it. The product colour is what I want to extract.
I want only the top 5 dominant colours, so that I can eliminate the first hex as the background colour and use the rest.The above script takes my image into a loop and creates about 50000+ hex codes. Also, I want this process to run in batch for all images in a folder not just one image.

Please help.

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to extract dominant colours from an image?

Post by Bonzo » 2016-01-11T05:39:19-07:00

You could reduce the amount of colours in your image first with -colors 5
Set the preferred number of colors in the image.

The actual number of colors in the image may be less than your request, but never more. Note that this a color reduction option. Images with fewer unique colors than specified by value will have any duplicate or unused colors removed. The ordering of an existing color palette may be altered. When converting an image from color to grayscale, it is more efficient to convert the image to the gray colorspace before reducing the number of colors. Refer to the color reduction algorithm for more details.

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to extract dominant colours from an image?

Post by fmw42 » 2016-01-11T11:00:36-07:00

Can you post and example image so we can see and test with it? You can post to some free service such as dropbox.com and put the URL here.

Always best to provide your exact command line, images, IM version and platform when asking questions, since syntax may differ.

See viewtopic.php?f=1&t=9620

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: How to extract dominant colours from an image?

Post by joshuafinny » 2016-01-14T09:10:00-07:00


snibgo
Posts: 12313
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to extract dominant colours from an image?

Post by snibgo » 2016-01-14T09:24:45-07:00

Code: Select all

convert 1_18.jpg +dither -colors 5 x.png
x.png is a simplified verson of the image, reduced to no more than 5 colours. "identify -verbose x.png" tells you the colours and their frequency. The most common is near white. The next is a darkish grayish red.
snibgo's IM pages: im.snibgo.com

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: How to extract dominant colours from an image?

Post by joshuafinny » 2016-01-16T06:29:33-07:00

snibgo wrote:

Code: Select all

convert 1_18.jpg +dither -colors 5 x.png
x.png is a simplified verson of the image, reduced to no more than 5 colours. "identify -verbose x.png" tells you the colours and their frequency. The most common is near white. The next is a darkish grayish red.
I want the same 5 colours to be extracted to a text file as hex values.

Please help.

snibgo
Posts: 12313
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to extract dominant colours from an image?

Post by snibgo » 2016-01-16T06:52:42-07:00

Code: Select all

convert 1_18.jpg +dither -colors 5 -unique-colors txt:
Then a script can extract the hex digits after "#".
snibgo's IM pages: im.snibgo.com

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: How to extract dominant colours from an image?

Post by joshuafinny » 2016-01-16T21:47:45-07:00

snibgo wrote:

Code: Select all

convert 1_18.jpg +dither -colors 5 -unique-colors txt:
Then a script can extract the hex digits after "#".
I think this is what I need. Can I include identify -verbose in this script to sort the colours in the order of frequency(high to low)? How do i run this in batch for all images in a folder?

snibgo
Posts: 12313
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to extract dominant colours from an image?

Post by snibgo » 2016-01-17T09:19:55-07:00

If you want the counts as well as the colours:

Code: Select all

f:\web\im>"%IM%convert" 1_18.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info:

     88596: ( 84, 75, 86) #544B56 srgb(84,75,86)
    142039: (178, 41, 56) #B22938 srgb(178,41,56)
     72690: (193, 56, 71) #C13847 srgb(193,56,71)
    128940: (213,172,165) #D5ACA5 srgb(213,172,165)
    377735: (241,239,236) #F1EFEC srgb(241,239,236)
Sorting will give you these in ascending or descending order of the count. To do all the files in a directory, I would always use a script with FOR, but perhaps mogrify can do the job.
snibgo's IM pages: im.snibgo.com

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: How to extract dominant colours from an image?

Post by joshuafinny » 2016-01-24T00:56:24-07:00

snibgo wrote:If you want the counts as well as the colours:

Code: Select all

f:\web\im>"%IM%convert" 1_18.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info:

     88596: ( 84, 75, 86) #544B56 srgb(84,75,86)
    142039: (178, 41, 56) #B22938 srgb(178,41,56)
     72690: (193, 56, 71) #C13847 srgb(193,56,71)
    128940: (213,172,165) #D5ACA5 srgb(213,172,165)
    377735: (241,239,236) #F1EFEC srgb(241,239,236)
Sorting will give you these in ascending or descending order of the count. To do all the files in a directory, I would always use a script with FOR, but perhaps mogrify can do the job.
convert *.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info: >hex.txt
Thanks! Did the trick for me! But I get all hex codes one after the other, so I don't know which image it corresponds to. Is there a way to get the image name and corresponding hex codes on the file?

snibgo
Posts: 12313
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to extract dominant colours from an image?

Post by snibgo » 2016-01-24T10:08:14-07:00

See http://www.imagemagick.org/script/escape.php

For example, you might use a format of:

Code: Select all

%f\n%c\n
snibgo's IM pages: im.snibgo.com

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: How to extract dominant colours from an image?

Post by joshuafinny » 2016-02-05T11:52:14-07:00

Great worked for me! Thanks a lot!

Post Reply