Python RGB 数组到 HSL 以及返回
我已经看到了一些将 RGB 转换为 HSL 的代码;但如何在 python 中快速完成它。
对我来说很奇怪,例如 Photoshop 在图像上在一秒钟内完成此操作,而在 Python 中这通常需要很长时间。好吧,至少我使用的代码;所以认为我使用了错误的代码来做到这
一点就我而言,我的图像是一个简单但大的原始数组 [r,g,b,r,g,b,r,g,b ....]
我想要这个成为 [h,s,l,h,s,l,h,s,l .......]
另外我希望能够将 hsl 转换为 rgb
图像实际上是 640x 480 像素; 是否需要一些 C 代码的库或包装器(我从未创建过包装器)才能快速完成?
well i've seen some code to convert RGB to HSL; but how to do it fast in python.
Its strange to me, that for example photoshop does this within a second on a image, while in python this often takes forever. Well at least the code i use; so think i'm using wrong code to do it
In my case my image is a simple but big raw array [r,g,b,r,g,b,r,g,b ....]
I would like this to be [h,s,l,h,s,l,h,s,l .......]
Also i would like to be able to do hsl to rgb
the image is actually 640x 480 pixels;
Would it require some library or wrapper around c code (i never created a wrapper) to get it done fast ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了操作图像数据,许多人使用 Python 图像库。但是,它不处理 HSL 颜色。幸运的是,Python 附带了一个名为
colorsys
的库。以下是colorsys
用于在每个像素级别的颜色模式之间进行转换的示例:http://effbot.org/librarybook/colorsys.htmcolorsys
还提供了将HSL转换为RGB的函数:http://docs.python.org/library/colorsys.htmlFor manipulating image data, many use the Python Imaging Library. However, it doesn't handle HSL colour. Luckily, Python comes with a library called
colorsys
. Here's an example ofcolorsys
being used to convert between colour modes on a per-pixel level: http://effbot.org/librarybook/colorsys.htmcolorsys
also provides a function to convert HSL to RGB: http://docs.python.org/library/colorsys.html我写了这个 RGB 到 HSV转换器不久前。它以 PIL 图像开始,但使用 numpy 有效地执行数组操作。它可以很容易地修改为 HSL。如果您想要修改版本,请告诉我。
I wrote this RGB to HSV converter a little while back. It starts with a PIL image but uses numpy to do the array operations efficently. It could very easily be modified to do HSL. Let me know if you want the modified version.
一种选择是使用 OpenCV。他们的 Python 绑定非常好(尽管并不令人惊奇)。好处是它是一个非常强大的库,所以这只是冰山一角。
您也可以使用 numpy 非常有效地完成此操作。
One option is to use OpenCV. Their Python bindings are pretty good (although not amazing). The upside is that it is a very powerful library, so this would just be the tip of the iceberg.
You could probably also do this very efficiently using numpy.