Best way to programmatically create image - python

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.

Related

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:

Accessing PIL image data from C extension

What is the most efficient way in terms of speed to access the pixel data of a PIL image from a C extension? I only need read-only access to it, if that makes a difference.
C-level bindings for PIL are available, but there is very little documentation for them. You will need to consult the source for usage information.
Besides C extension, you can try numpy. It takes a bit to learn though. To get started, check Convert RGBA PNG to RGB with PIL , and http://effbot.org/zone/pil-numpy.htm .
In my experience, numpy performance is great if the code is properly written. Processing image data can still be slow using C extension. But numpy uses SIMD instructions such as SSE2, which dramatically improves operation such as histogram elevating or alpha blending.

Rewriting 2D array of integers to bitmap in Python using PyQT

I want to do convert array of integers into some sort of 'picture' using PyQt (I've decided to do my app in Qt). I have array like this:
Array = [
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0]]
Now I want to rewrite it into picture, by replacing each integer by for example square 10x10 pixels. I have definition for each value in array in RGB. What's more This is some kind of game of life, so it must refresh on each step and shouldn't be slow. Maybe somethinf similar to OpenCV?
Thanks in advance!
Cheers,
Mateusz
You could easily do the above with QGraphicsScene and QGraphicsView. In order to get good performance, you'll want to call setViewport(QGLWidget()) on your QGraphicsView instance. Create a subclass of QGraphicsItem to represent an element in your array. You'll then even be able to animate the changes if you want.
If you do want animations or are demonstrating some progression such as in Conway's Game of Life you might also want to take a look at QTimeLine.
You can look up the equivalent python-based documentation on either the PyQt* or PySide websites. Both PyQt and PySide use a nearly identical API so for most everything you can use them interchangeably.
*Note: The PyQt website is inaccessible at the time of this writing
You should probably use QT’s graphics libraries for performance. Another, maybe simpler way could be to use PIL (Python Imaging Library) or some Python bindings to the ImageMagick or MagickWand library (I haven't found a good and current one) and use NumPy’s arrays for calculations and manipulation, and draw on a surface or canvas using PyGame, QT or some other GUI toolkit.
In PIL there is PIL.Image.fromarray(np_array, 'RGBA'), that reads suitable NumPy arrays – the datatype must usually be dtype=int8 and the shape is (height, width, n_channels).
For a very simple graphics format that uses ascii byte values, see NetPBM.

Python RGB array to HSL and back [duplicate]

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.

Image color recognition, the general color

I want to get the most prominent color of an image, and the language can be in either python or ruby.
Is this easily done?
I don't know if this is what you mean, but maybe it will be helpful:
require 'rubygems'
require 'RMagick'
include Magick
image = Image.read("stack.png")[0]
hash = image.color_histogram
color, number = hash.max{|a,b| a[1] <=> b[1]}
puts color.to_color
This worked like a charm for very simple image (only 5 colors), but should work for more complex images too (I did not tested that; returned hash will be quite big in that case, so you might want to use quantize on your image before using color_histogram).
Some resources :
color_histogram
quantize
I hope this was useful to you. :)
OK. Let me introduce the library for Ruby.
Using Camellia, http://camellia.sourceforge.net/examples.html, you can label the area with the most prominent color.
Not sure if this is what you mean, but the Python PIL has im.histogram() and im.getcolors() functions. http://effbot.org/imagingbook/image.htm

Categories

Resources