[Matplotlib-users] Why is canvas.print_figure to a png reduce the real size of my image

Warren Weckesser warren.weckesser at gmail.com
Sat Oct 21 08:27:09 EDT 2017


On Fri, Oct 20, 2017 at 6:36 PM, David Goldsmith <
eulergaussriemann at gmail.com> wrote:

> Thanks.  Does it have pylab’s range of built in color maps?  Does it even
> use colormaps, or do I have to write my own (or “copy” them from MPL).
>

No, numpngw doesn't use colormaps.  You'd have to map and scale the image
to an array of integers yourself.  You could use matplotlib for the
mapping.  For example, if foo is a 2-D array of floats with values between
0 and 1, you could do something like

    mapped_foo = mpl.cm.viridis(foo)  # Use matplotlib's 'viridis'
colormap.
    maxint = 2**16
    img = np.floor(maxint*mapped_foo)
    img[img == maxint] = maxint - 1
    numpngw.write_png("foo.png", img.astype(np.uint16))

That would create a 16 bit RGB PNG file.

Warren




> DLG
>
> On Fri, Oct 20, 2017 at 3:28 PM Warren Weckesser <
> warren.weckesser at gmail.com> wrote:
>
>> On Fri, Oct 20, 2017 at 5:38 PM, David Goldsmith <
>> eulergaussriemann at gmail.com> wrote:
>>
>>> Hi!  I spent a good part of yesterday trying to figure this out on my
>>> own, without success, so I'm posting.  I have a 3601x2401 pixel, 300 DPI
>>> figure I export to a png using print_figure; here's sample code:
>>>
>>>
>>
>> David,
>>
>> If you are trying to write the numpy array directly to a PNG file, an
>> alternative to matplotlib is a package that I created called numpngw:
>> https://pypi.python.org/pypi/numpngw (github: https://github.com/
>> WarrenWeckesser/numpngw).  Both those links show several examples of its
>> use.   numpngw uses just Python and NumPy, so it is easy to install.
>>
>> Warren
>>
>>
>> PLB.imshow(rslt) # rslt is a 3601x2401 int-valued array
>>> curfig = PLB.Figure
>>> fig = PLB.gcf()
>>> # next three lines are in my code; don't seem relevant, but I include them in case they are
>>>
>>> ax = fig.gca()
>>> ax.xaxis.set_ticks([])
>>> ax.yaxis.set_ticks([])
>>>
>>> fig.canvas.print_figure(fn + '.png',
>>>                         dpi=300,
>>>
>>>                         bbox_inches='tight',
>>>
>>>                         pad_inches=-1.0 / 72)
>>>
>>> The resulting png is neither the right number of pixels by pixels, nor the right number of inches by incheses; what's more the resulting png differs depending on whether I use imshow or matshow (which I also tried).
>>>
>>> What's going on, and how do I "fix" it?  (If I really am getting the full resolution figure, despite what the png is telling me are the pixel counts, why are the physical dimensions off, and why is the png telling me that the pixel counts are different?  If it has something to do with compression, is there some  undocumented way to say "no compression" and will that have the desired effect?)
>>>
>>> Thanks!
>>>
>>> DLG
>>>
>>>
>>>
>>> _______________________________________________
>>> Matplotlib-users mailing list
>>> Matplotlib-users at python.org
>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171021/a238b024/attachment.html>


More information about the Matplotlib-users mailing list