I was writing a script to change my background on my Linux Machine to a random image from a set of images that contain only solid colors. What I would like to do is expand this script to also theme certain applications (mostly terminal ones) accordingly, at the very least to change the text color, possibly switch from dark to light background, etc. I was wondering what options I have to get the Hex Code for the color in the image. Is there something in bash I can do this with? Would I need to write a program in a more robust language and have the hex-code be the output? Is there a better way of doing this entirely? My searching thus far has been a bit inconclusive.
Asked
Active
Viewed 2,317 times
2
-
So if I understand you correctly, you want to take a picture (and an x,y coordinate pair) as input and output the colour as hex value? – MechMK1 Dec 26 '17 at 19:47
-
Yes, something like that, if there's a better way to get a color code that would work to. Like I said, in this case they are solid color images. Its probably something pretty simple, but my searches haven't really given me anything I can use in a script or programming language easily, and I haven't been able to find any pre-existing tools that do what I want. – Christopher Leggett Dec 26 '17 at 19:59
1 Answers
4
I would highly recommend to use ImageMagick for this task. The documentation mentions how to extract data from an image.
From "Extracting the average colour":
The average color of an image can be found very quickly by using "-scale" to reduce an image to a single pixel. Here for example is the average color of the built-in "rose:" image. I output the color using the FX Escape Format which returns a color string that can be used directly IM without change.
user@laptop:~$ convert rose: -scale 1x1\! -format '%[pixel:s]\n' info:-
Will output:
srgb(146,89,80)
In your case, replace rose:
with an image file of yours, like foo.png
.
If you want the output directly in hex notation, use this:
convert rose: -scale 1x1\! -format '%[pixel:s]\n' info:- | awk -F '[(,)]' '{printf("#%x%x%x\n",$2,$3,$4)}'
-
Thank You! Do you know of any way to have it output straight to hex value? I'm sure there is but if you know it offhand it might be faster than me searching the documentation for it. If there isn't I can probably figure out a way to convert it. – Christopher Leggett Dec 26 '17 at 20:21
-
Sorry, ImageMagick is an absolute bitch to work with. It's powerful, but has 0 documentation and the fact that the same words are used in several different contexts makes it extremely hard to google. – MechMK1 Dec 26 '17 at 20:42
-
@ChristopherLeggett I also asked a new question [here](https://stackoverflow.com/questions/47983587/how-can-imagemagick-output-hex-colours-instead-of-srgb), so we can try and find a solution together. – MechMK1 Dec 26 '17 at 20:52
-
-
Nice, Thanks! That's very helpful! I was also able to find a way to convert back and forth pretty easily with a bit of python [here](https://stackoverflow.com/a/4296263/9142730). – Christopher Leggett Dec 26 '17 at 21:21