Re: [yt-users] ImageArray.write_png() always rescales ...
Hi Stuart,
On Mon, Mar 30, 2015 at 3:59 PM, Stuart Levy
I'm trying to use the notebook interface to render, view image result, tweak parameters, etc. within a single yt session.
I want to disable auto-brightness-scaling of images, so am calling
ImageArray.write_png()
Although write_png() takes a rescale= parameter, it effectively always rescales. It passes its array to write_bitmap(), which does rescaling unless the image datatype is already numpy.uint8.
You're right. It does look like write_bitmap accepts a max_val, which will be used in lieu of computing the max, before converting to uint8. Would that work for your purposes?
But if you try to do that yourself, then write_png() crashes inside matplotlib, because write_bitmap "must be an rgba buffer". It's using a Python C-level routine to do this check, but I think the problem is that the array is not C-contiguous, since write_bitmap() has called swapaxes(0,1) on it and not copy()ied it afterward.
Demo program at:
http://paste.yt-project.org/show/5502/
and here:
import yt from yt.data_objects.image_array import ImageArray import numpy
# 3x4x4 array, ranging from 0 to about 0.5 v = (numpy.arange(12)/24.) .reshape(3,4,1) * numpy.ones((1,1,4)) # Write ImageArray(v).write_png('grays_rescaled.png', rescale=True) # Write, but asking not to rescale. It rescales anyway since yt.data_objects.image_array() calls write_bitmap() ImageArray(v).write_png('grays_unscaled.png', rescale=False)
# write_bitmap rescales internally unless the image's datatype is exactly "numpy.uint8". # Let's try doing that ourselves. vv = (v*255).astype(numpy.uint8) print vv.flags ImageArray(vv).write_png('grays8.png', background=None, rescale=False)
# Crashes! inside matplotlib/src/_png.cpp.
# Why? yt/visualization/image_writer.py's write_bitmap(), if it doesn't rescale the image, # simply calls swapaxes(0,1) on it and passes the result to matplotlib. # But the resulting array is no longer C-contiguous (write_bitmap.flags shows False).
# When rescaling *does* happen, write_bitmap() is constructed by np.concatenate() which makes a new contiguous array.
_______________________________________________ yt-users mailing list yt-users@lists.spacepope.org http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
_______________________________________________ yt-users mailing list yt-users@lists.spacepope.org http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
participants (1)
-
Matthew Turk