如何从脚本的纯色图像中获取十六进制颜色代码? [英] How to get a Hex Color Code from a solid-color image for a script?

查看:313
本文介绍了如何从脚本的纯色图像中获取十六进制颜色代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,将我的Linux机器上的背景更改为仅包含纯色的一组图像中的随机图像。我想要做的就是将这个脚本扩展为主题相应的某些应用程序(主要是终端应用程序),至少要改变文本颜色,可能从黑暗背景切换到浅色背景等。我想知道我有什么选择获取图像中颜色的十六进制代码。在bash中有什么我可以做到这一点?我是否需要用更健壮的语言编写程序,并将十六进制代码作为输出?有没有更好的方法来完成这一切?我到目前为止的搜索一直有点不确定。

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.

推荐答案

我强烈建议使用 ImageMagick 文档提到了如何从图像中提取数据。

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":


图像的平均颜色可以很快找到通过使用缩放将图像缩小为单个像素。这里例如是内置玫瑰:图像的平均颜色。我使用FX Escape格式输出颜色,
返回一个颜色字符串,可以直接使用即时消息而不用改变。

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:-

输出: srgb(146,89,80)

在你的情况中,将 rose:带有您的图片文件,如 foo.png

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)}'

这篇关于如何从脚本的纯色图像中获取十六进制颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆