Skip to content

Commit 2d354ee

Browse files
authored
Merge pull request matplotlib#11318 from timhoffm/doc-imread
Improve docstring of imread() and imsave()
2 parents 40327be + 2d807cf commit 2d354ee

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

lib/matplotlib/image.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,24 +1291,33 @@ def imread(fname, format=None):
12911291
"""
12921292
Read an image from a file into an array.
12931293
1294-
*fname* may be a string path, a valid URL, or a Python
1295-
file-like object. If using a file object, it must be opened in binary
1296-
mode.
1297-
1298-
If *format* is provided, will try to read file of that type,
1299-
otherwise the format is deduced from the filename. If nothing can
1300-
be deduced, PNG is tried.
1301-
1302-
Return value is a :class:`numpy.array`. For grayscale images, the
1303-
return array is MxN. For RGB images, the return value is MxNx3.
1304-
For RGBA images the return value is MxNx4.
1305-
1306-
matplotlib can only read PNGs natively, but if `PIL
1307-
<http://www.pythonware.com/products/pil/>`_ is installed, it will
1308-
use it to load the image and return an array (if possible) which
1309-
can be used with :func:`~matplotlib.pyplot.imshow`. Note, URL strings
1310-
may not be compatible with PIL. Check the PIL documentation for more
1311-
information.
1294+
Parameters
1295+
----------
1296+
fname : str or file-like
1297+
The image file to read. This can be a filename, a URL or a Python
1298+
file-like object opened in read-binary mode.
1299+
format : str, optional
1300+
The image file format assumed for reading the data. If not
1301+
given, the format is deduced from the filename. If nothing can
1302+
be deduced, PNG is tried.
1303+
1304+
Returns
1305+
-------
1306+
imagedata : :class:`numpy.array`
1307+
The image data. The returned array has shape
1308+
1309+
- (M, N) for grayscale images.
1310+
- (M, N, 3) for RGB images.
1311+
- (M, N, 4) for RGBA images.
1312+
1313+
Notes
1314+
-----
1315+
Matplotlib can only read PNGs natively. Further image formats are
1316+
supported via the optional dependency on Pillow. Note, URL strings
1317+
are not compatible with Pillow. Check the `Pillow documentation`_
1318+
for more information.
1319+
1320+
.. _Pillow documentation: http://pillow.readthedocs.io/en/latest/
13121321
"""
13131322

13141323
def pilread(fname):
@@ -1374,26 +1383,29 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
13741383
Parameters
13751384
----------
13761385
fname : str or file-like
1377-
Path string to a filename, or a Python file-like object.
1378-
If *format* is *None* and *fname* is a string, the output
1379-
format is deduced from the extension of the filename.
1386+
The filename or a Python file-like object to store the image in.
1387+
The necessary output format is inferred from the filename extension
1388+
but may be explicitly overwritten using *format*.
13801389
arr : array-like
1381-
An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
1382-
vmin, vmax: [ None | scalar ]
1390+
The image data. The shape can be one of
1391+
MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).
1392+
vmin, vmax : scalar, optional
13831393
*vmin* and *vmax* set the color scaling for the image by fixing the
13841394
values that map to the colormap color limits. If either *vmin*
13851395
or *vmax* is None, that limit is determined from the *arr*
13861396
min/max value.
1387-
cmap : matplotlib.colors.Colormap, optional
1388-
For example, ``cm.viridis``. If ``None``, defaults to the
1389-
``image.cmap`` rcParam.
1390-
format : str
1391-
One of the file extensions supported by the active backend. Most
1392-
backends support png, pdf, ps, eps and svg.
1393-
origin : [ 'upper' | 'lower' ]
1394-
Indicates whether the ``(0, 0)`` index of the array is in the
1395-
upper left or lower left corner of the axes. Defaults to the
1396-
``image.origin`` rcParam.
1397+
cmap : str or `~matplotlib.colors.Colormap`, optional
1398+
A Colormap instance or registered colormap name. The colormap
1399+
maps scalar data to colors. It is ignored for RGB(A) data.
1400+
Defaults to :rc:`image.cmap` ('viridis').
1401+
format : str, optional
1402+
The file format, e.g. 'png', 'pdf', 'svg', ... . If not given, the
1403+
format is deduced form the filename extension in *fname*.
1404+
See `.Figure.savefig` for details.
1405+
origin : {'upper', 'lower'}, optional
1406+
Indicates whether the ``(0, 0)`` index of the array is in the upper
1407+
left or lower left corner of the axes. Defaults to :rc:`image.origin`
1408+
('upper').
13971409
dpi : int
13981410
The DPI to store in the metadata of the file. This does not affect the
13991411
resolution of the output image.

lib/matplotlib/pyplot.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from cycler import cycler
2929
import matplotlib
3030
import matplotlib.colorbar
31+
import matplotlib.image
3132
from matplotlib import style
3233
from matplotlib import _pylab_helpers, interactive
3334
from matplotlib.cbook import (
@@ -36,8 +37,6 @@
3637
from matplotlib.backend_bases import FigureCanvasBase
3738
from matplotlib.figure import Figure, figaspect
3839
from matplotlib.gridspec import GridSpec
39-
from matplotlib.image import imread as _imread
40-
from matplotlib.image import imsave as _imsave
4140
from matplotlib import rcParams, rcParamsDefault, get_backend
4241
from matplotlib import rc_context
4342
from matplotlib.rcsetup import interactive_bk as _interactive_bk
@@ -1998,14 +1997,14 @@ def set_cmap(cmap):
19981997
im.set_cmap(cmap)
19991998

20001999

2001-
@docstring.copy_dedent(_imread)
2000+
@docstring.copy_dedent(matplotlib.image.imread)
20022001
def imread(fname, format=None):
2003-
return _imread(fname, format)
2002+
return matplotlib.image.imread(fname, format)
20042003

20052004

2006-
@docstring.copy_dedent(_imsave)
2005+
@docstring.copy_dedent(matplotlib.image.imsave)
20072006
def imsave(fname, arr, **kwargs):
2008-
return _imsave(fname, arr, **kwargs)
2007+
return matplotlib.image.imsave(fname, arr, **kwargs)
20092008

20102009

20112010
def matshow(A, fignum=None, **kw):

0 commit comments

Comments
 (0)