I'm trying to automate the enhancement of some images that are to be transfered to a digital frame. I have code in place that resizes, adds a date/time to the least-significant (least details) corner of the image and pastes together pairs of portrait images to avoid displaying a single portrait in the frame's 41:20 low resolution screen.
I've implemented a brightness-stretching filter for those pictures where the lighting wasn't so good, using the colorsys.rgb_to_hsv
function to calculate H, S, V bands, operating on the V one and then converting back to RGB before saving a JPEG in the digital frame. Obviously, the conversion takes a lot of time, even using itertools
tricks; I managed to improve things using psyco
.
However, I noticed an example for the PIL Image.convert
where RGB can be converted to XYZ color space using a 4×4 matrix as a second argument to the convert
method, and I got to wonder:
How can I convert RGB to HSV (and then HSV back to RGB) using a custom matrix in the convert
method call? (Minor rounding errors are not important in this case, so I don't mind that each band will be expressed as a series of 0…255 integers)
Thank you in advance.
Although I've seen references[1] that claim HSV color-space is linear transformation from RGB, which would seem to imply that it could be done with a matrix, I have been unable to find or determine for myself just what such a matrix would look like. In a way this doesn't really surprise me based on all the [similar] non-matrix procedural implementations I've also seen -- the way they go about it doesn't look linear.
Anyway, while looking into this, I ran across a [somewhat dated] article in former SGI researcher Paul Haeberli's online computer graphics notebook titled Matrix Operations for Image Processing which describes how to do a number of different color transformations using 4x4 matrices which might help you. All of the examples given operate directly on RGB color images and, like geometric matrix transformations, any sequence of them can be combined into a single matrix using concatenation.
Hope this helps.
2.7.3 HSL (Hue Saturation and Lightness)
This represents a wealth of similar colour spaces, alternative names include HSI (intensity), HSV (value), HCI (chroma / colourfulness), HVC, TSD (hue saturation and darkness) etc. Most of these colour spaces are linear transforms from RGB and are therefore device dependent and non–linear. Their advantage lies in the extremely intuitive manner of specifying colour. It is very easy to select a desired hue and to then modify it slightly by adjustment of its saturation and intensity.