How to extract dominant colours from an image?
-
- Posts: 16
- Joined: 2016-01-11T05:12:11-07:00
- Authentication code: 1151
How to extract dominant colours from an image?
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.
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.
Re: How to extract dominant colours from an image?
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to extract dominant colours from an image?
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
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
-
- Posts: 16
- Joined: 2016-01-11T05:12:11-07:00
- Authentication code: 1151
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to extract dominant colours from an image?
Code: Select all
convert 1_18.jpg +dither -colors 5 x.png
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2016-01-11T05:12:11-07:00
- Authentication code: 1151
Re: How to extract dominant colours from an image?
I want the same 5 colours to be extracted to a text file as hex values.snibgo wrote: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.Code: Select all
convert 1_18.jpg +dither -colors 5 x.png
Please help.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to extract dominant colours from an image?
Code: Select all
convert 1_18.jpg +dither -colors 5 -unique-colors txt:
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2016-01-11T05:12:11-07:00
- Authentication code: 1151
Re: How to extract dominant colours from an image?
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 wrote:Then a script can extract the hex digits after "#".Code: Select all
convert 1_18.jpg +dither -colors 5 -unique-colors txt:
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to extract dominant colours from an image?
If you want the counts as well as the colours:
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.
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)
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2016-01-11T05:12:11-07:00
- Authentication code: 1151
Re: How to extract dominant colours from an image?
convert *.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info: >hex.txtsnibgo wrote:If you want the counts as well as the colours: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.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)
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to extract dominant colours from an image?
See http://www.imagemagick.org/script/escape.php
For example, you might use a format of:
For example, you might use a format of:
Code: Select all
%f\n%c\n
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2016-01-11T05:12:11-07:00
- Authentication code: 1151
Re: How to extract dominant colours from an image?
Great worked for me! Thanks a lot!