holoviz-topics / imagen

ImaGen: Generic Python library for 0D, 1D and 2D pattern distributions
https://imagen.holoviz.org/
BSD 3-Clause "New" or "Revised" License
31 stars 16 forks source link

Save to disk? #49

Closed benureau closed 7 years ago

benureau commented 7 years ago

I am considering using imagen to generate stimuli for an experiments. I would generate a lot of stimuli (> 100), and want to save them as individual .png, .svg or .jpg files. All the examples I found are using holoview in notebooks to display the file. Is there a minimal example somewhere that shows a way to get the patterns to disk?

jbednar commented 7 years ago

The call mechanism in ImaGen returns a bare NumPy array, and you can use any mechanism you like to convert that into an image for saving. For instance, using PIL you can do:

>>> import numpy as np, imagen as ig, PIL.Image as Image
>>> line=ig.Line()
>>> img = Image.fromarray(np.uint8(line()*255))
>>> img.save("line.png")
jbednar commented 7 years ago

I would recommend .png in this case, because .jpg will add artifacts to images of this type.

Supporting SVG output is an interesting possibility that I had not previously considered. In principle it could be feasible, because the specification for each of the pattern types has been declared in a resolution-independent way. I can imagine adding an SVG output mechanism for each pattern type, similar to the Numpy and HoloViews outputs currently supported, which for most types would generate vector-based SVG elements that are then combined to make the whole SVG figure. But that would definitely take some work, and some of the pattern types would still need to be rasterized even in SVG (e.g. the random-noise patterns that are only meaningful with an underlying grid).

benureau commented 7 years ago

Thanks for the quick response. That's exactly what I was looking for. Yes, SVG output does seem to involve some work, and rasterizing is inevitable in some cases. For my experiment, PNG is absolutely fine.