Python RGB array to HSL and back [duplicate] - python

This question already has answers here:
Detecting thresholds 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.

Related

Use cv2.imshow() to display an np.array [duplicate]

This question already has answers here:
Difference between plt.imshow and cv2.imshow?
(3 answers)
Closed 8 months ago.
so at the moment im accesing the BGR Channels of images and do a bit of calculation around them.
Like the mean or standard deviation.. stuff like that.
As far as i know i dont have to convert numPy Arrays to display them with cv2.imshow().
But when I display my array with this command:
#with the help of the PIL Libary
data = Image.fromarray(image_array)
data.save('SavedArrayAsPic.png')
My Output is correct. Its an Image with another color.
But when I write:
cv2.imshow("my Array as a Pic", image_array)
It shows the wrong image with an old color pattern.
I want to use cv2.imshow to display videos in RealTime. With the PIL Libary i just save the images.
So what could be the difference?
Thank you for reading
opencv has BGR channel ordering, PIL and matplotlib use RGB order
try not to mix different libraries with different paradigms

Display an image with pixel values shown numerically

I'm looking for OpenCV or other Python function that displays a NumPy array as an image like this:
Referenced from this tutorial.
What function creates this kind of grey-scale image with pixel values display?
Is there a color image equivalent?
MATLAB has a function called showPixelValues().
The best way to do this is to search "heat map" or "confusion matrix" rather than image, then there are two good options:
Using matplotlib only, with imshow() and text() as building blocks the solution is actually not that hard, and here are some examples.
Using seaborn which is a data visualisation package and the solution is essentially a one-liner using seaborn.heatmap() as shown in these examples.
My problem was really tunnel vision, coming at it from an image processing mindset, and not thinking about what other communities have a need to display the same thing even if they call it by a different name.

PNG to PGM conversion without quality loss

So, I have a PNG image file like the following example, and I need it to be converted into PGM format.
I'm using Ubuntu and Python, so any of terminal or Python tools would suit just fine. And there sure is a plenty of ways to do this: using ImageMagick convert command or pngtopam package or Python PIL library, etc.
But the point is, the quality of the image is essential in my case, and all of those failed in keeping it, always ending up with:
No need to mention this is totally not what I want to see. And the interesting thing is that when I tried to convert the same image into PGM manually using GIMP, it turned out quite well, looking exactly the way I'd like it to, i.e. the same as the PNG one.
So, that means it is possible to get a PGM image in fine quality after all, and now I'd really appreciate if someone can tell me how do I do that using terminal/Python tools. I guess, there should be some ImageMagick option that does the trick, it's just that I'm not aware of any.
You lost the antialiasing, which is conveyed via the alpha channel. To preserve it, use:
convert in.png -flatten out.pgm
Without -flatten, convert simply deletes the alpha channel; with -flatten it composites the input image against the background color, which is white by default.
Here are the results, magnified 10x so you can see what's going on:
Not flattened:
Flattened:

Python: Floating Point images / alternative to matlab's im2double?

I'm trying to convert some matlab code to python and the im2double function is giving me trouble. It takes an image and returns a matrix with the pixels using doubles instead of ints.
Currently I'm manipulating my images with PIL. It has a convert method that can take 'F' as parameter, but all it does is convert the integer value 255 to 255.0. Useless as far as I can tell.
The problem I have is that I'm doing some image manipulation and then have to save them. I can normalize my values so that they fall into the 0-255 range, but I lose some precision. It's small enough that it shouldn't normally matter, but here, it does.
I've tried using the 'tiff' file format and that didn't work out well. Though I can write/read to it, the results I get are not the right ones, which I can only get at the moment converting the pixels to 255 which results in a loss of precision, as I said previously. I also tried this 'SPIDER' file format thing I found on google previously that PIL supports though I couldn't open the image on an editor to check how it was doing.
The way to do this properly in Python will to use Numpy. You can read images via PIL into numpy arrays. At this point a wide range of Matlab like matrix operations become available to you via numpy/scipy. Changing the precision of the array is simply a matter of switching the arrays datatype via numpy. Recent releases of PIL include the patch from Travis Oliphant to allow you to do this without extra hackery.
Saving the data to a more commonly readable image format can be achieved by using a floating point TIFF without loss of precision. I use the GDAL library to interface to multiple image format writers/readers. If you want lossless compression TIFF can compress using zlib as well.

Best way to programmatically create image

I'm looking for a way to create a graphics file (I don't really mind the file type, as they are easily converted).
The input would be the desired resolution, and a list of pixels and colors (x, y, RGB color).
Is there a convenient python library for that? What are the pros\cons\pitfalls?
PIL is the canonical Python Imaging Library.
Pros: Everybody wanting to do what you're doing uses PIL. 8-)
Cons: None springs to mind.
Alternatively, you can try ImageMagick.
Last time I checked, PIL didn't work on Python 3, which is potentially a con. (I don't know about ImageMagick's API.) I believe an updated version of PIL is expected in the year.

Categories

Resources