开发者

Python RGB array to HSL and back [duplicate]

This question already has answers here: Detecting th开发者_运维知识库resholds in HSV color space (from RGB) using Python / PIL (4 answers) Closed 9 years ago.

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 ?


For 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 of colorsys being used to convert between colour modes on a per-pixel level: http://effbot.org/librarybook/colorsys.htm

colorsys also provides a function to convert HSL to RGB: http://docs.python.org/library/colorsys.html


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜