Revision: 8300 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8300&view=rev Author: mdboom Date: 2010-05-06 19:59:32 +0000 (Thu, 06 May 2010)
Log Message: ----------- Save DPI metadata to the file produced by imsave(). Defaults to 100 dpi (for lack of a better default) Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/image.py trunk/matplotlib/lib/matplotlib/tests/test_image.py Modified: trunk/matplotlib/lib/matplotlib/image.py =================================================================== --- trunk/matplotlib/lib/matplotlib/image.py 2010-05-06 19:14:11 UTC (rev 8299) +++ trunk/matplotlib/lib/matplotlib/image.py 2010-05-06 19:59:32 UTC (rev 8300) @@ -1113,7 +1113,8 @@ return handler(fname) -def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, origin=None): +def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, + origin=None, dpi=100): """ Saves a 2D :class:`numpy.array` as an image with one pixel per element. The output formats available depend on the backend being used. @@ -1140,14 +1141,18 @@ [ 'upper' | 'lower' ] Indicates where the [0,0] index of the array is in the upper left or lower left corner of the axes. Defaults to the rc image.origin value. + *dpi* + The DPI to store in the metadata of the file. This does not affect the + resolution of the output image. """ from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure - fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False) + figsize = [x / float(dpi) for x in arr.shape] + fig = Figure(figsize=figsize, dpi=dpi, frameon=False) canvas = FigureCanvas(fig) - fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin) - fig.savefig(fname, dpi=1, format=format) + im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin) + fig.savefig(fname, dpi=dpi, format=format) def pil_to_array( pilImage ): Modified: trunk/matplotlib/lib/matplotlib/tests/test_image.py =================================================================== --- trunk/matplotlib/lib/matplotlib/tests/test_image.py 2010-05-06 19:14:11 UTC (rev 8299) +++ trunk/matplotlib/lib/matplotlib/tests/test_image.py 2010-05-06 19:59:32 UTC (rev 8300) @@ -3,6 +3,7 @@ from matplotlib.testing.decorators import image_comparison, knownfailureif import matplotlib.pyplot as plt from nose.tools import assert_raises +from numpy.testing import assert_array_equal import cStringIO import os @@ -66,6 +67,35 @@ # plt.imread(fname) # os.remove(fname) +def test_imsave(): + # The goal here is that the user can specify an output logical DPI + # for the image, but this will not actually add any extra pixels + # to the image, it will merely be used for metadata purposes. + + # So we do the traditional case (dpi == 1), and the new case (dpi + # == 100) and read the resulting PNG files back in and make sure + # the data is 100% identical. + from numpy import random + random.seed(1) + data = random.rand(256, 256) + + buff_dpi1 = cStringIO.StringIO() + plt.imsave(buff_dpi1, data, dpi=1) + plt.imsave("test_dpi1.png", data, dpi=1) + + buff_dpi100 = cStringIO.StringIO() + plt.imsave(buff_dpi100, data, dpi=100) + plt.imsave("test_dpi100.png", data, dpi=1) + + buff_dpi1.seek(0) + arr_dpi1 = plt.imread(buff_dpi1) + + buff_dpi100.seek(0) + arr_dpi100 = plt.imread(buff_dpi100) + + assert_array_equal(arr_dpi1, arr_dpi100) + + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ _______________________________________________ Matplotlib-checkins mailing list Matplotlib-checkins@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins