convert mayavi scene into e.g. PIL image without rendering it first - python

I am looking for a way to convert a 3D scene from mayavi into a python image format like PIL. I read the documentation but nothing really fits my need. Is it possible to do that in an efficient manner, e.g. a loop over scenes without rendering every one of it first?

Related

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.

Draw multiple PIL.Image in python

I have a python funtion that draws a Fractal to a PIL.Image, but i want to vary the parameters of the function in realtime and to plot it to the screen. How can i plot the image and keep updating the ploted image each time the parametes of the function vary
Use matplotlib, wxPython, PyQt, PyGame, Tk/TCL or some other lib to display the image.
Draw as many images as you need, whenever you need, using any lib you need, and then display it on a screen using one of above mentioned or some other GUI libs.
If you are working with plots and math functions, matplotlib will help you most. You might even totally use it, forgoing PIL completely.
If you want to stick to PIL only, you will have to write your own show() function, that will use some external imaging software which will seemlessly change to show another image when you send it. Perhaps Irfan View would do.

Convert SVG to png or other?

Since it's very easy to display the content of a SVG file inside the iPython notebook, is there also a way (easy too) to get what we see inside a png file or other ?
from IPython.display import SVG
SVG(filename='../images/python_logo.svg')
If I do svg = SVG(filename='../images/python_logo.svg')
How can I save it to a png file ?
SVG are vectors images (the drawings are saved as commands to draw lines, circles, etc). PNGs are bitmaps. So to convert SVG to PNG, you need a renderer.
The most obvious solution is ImageMagick, a library you have already installed, as it is used in several programs. A less obvious approach is using Inkscape. Using the commandline options, it's possible to use Inkscape as a conversion program. As Inkscape is vector oriented, I suspect quality to be better than ImageMagick (which is more bitmap-minded).
As a vector image (SVG) is a text file containing drawing instructions, it's easier to understand. PNGs contain just pixel information, and, to make things worse, they are compressed with a fairly complicated algorithm. Making sense of them is not as easy.
Have a look at the Inkscape man page, it's fairly obvious how to use it. This is the IMagick convert help.

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.

Change texture on 3D object and export 2D image

I would like to generate 2D images of 3D books with custom covers on demand.
Ideally, I'd like to import a 3D model of a book (created by an artist), change the cover texture to the custom one, and export a bitmap image (jpeg, png, etc...). I'm fairly ignorant about 3D graphics, so I'm not sure if that's possible or feasible, but it describes what I want to do. Another method would be fine if it accomplishes something similar. Like maybe I could start with a rendered 2D image and distort the custom cover somehow then put it in the right place over the original image?
It would be best if I could do this using Python, but if that's not possible, I'm open to other solutions.
Any suggestions on how to accomplish this?
Sure it's possible.
Blender would probably be overkill, but you can script blender with python, so that's one solution.
The latter solution is (I'm pretty sure) what most of those e-book cover generators do, which is why they always look a little off.
The PIL is an excellent tool for manipulating images and pixel data, so if you wanted to distort your own, that would be a great tool to look at, and if it goes too slow it's trivial to convert the image to a numpy array so you can get some speedup.

Categories

Resources