Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

colors - Process image with Imagemagick to "default palette" (for example 16 or 256 colours)

Currently I'm processing image to extract main colors from it with such:

-resize '50x50' -colors '8' -colorspace 'RGB' -quantize 'RGB' '/tmp/downsampled20190502-27373-iqgqom.png'

However, it returns me a lot of colours, so I have to limit them to the main palette (like white/black/red/etc), so I guess 8 or 16 colours would be enough for me.

I thought that -colors '8' should process it, however, it only returns primary 8 colours from the image.

Do you have any ideas about how I could extract colours and convert them to 3-bit (8-color palette)

I though convert it to GIF, however GIF contains 256 colour palette.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you want to map all colours to one of 8 "primaries". So, let's make a palette of acceptable colours:

convert xc:red xc:lime xc:blue xc:cyan xc:magenta xc:yellow xc:white xc:black +append palette.gif

And enlarge it and look at it (because at the moment it is only 8x1 pixels):

enter image description here

Now take this colorwheel:

enter image description here

and remap all the colours to your "acceptable" palette without dithering:

convert colorwheel.png +dither -remap palette.gif result.png

enter image description here

and now remap with dithering:

convert colorwheel.png -remap palette.gif result.png

enter image description here


You can make your own palette - you don't have to use my colours, and you can make any RGB/HSL, hex colour you like, e.g.:

convert xc:"rgb(10,20,200)" xc:"#ff7832" xc:"hsl(10,40,90)" +append palette.gif

If you want the names and hex values of the colours in the resulting images:

convert result.png -unique-colors txt:

Sample Output

# ImageMagick pixel enumeration: 7,1,65535,srgb
0,0: (65535,0,0)  #FF0000  red
1,0: (0,65535,0)  #00FF00  lime
2,0: (65535,65535,0)  #FFFF00  yellow
3,0: (0,0,65535)  #0000FF  blue
4,0: (65535,0,65535)  #FF00FF  magenta
5,0: (0,65535,65535)  #00FFFF  cyan
6,0: (65535,65535,65535)  #FFFFFF  white

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...