8000 Simplify image.thumbnail. · matplotlib/matplotlib@c564e6f · GitHub
[go: up one dir, main page]

Skip to content

Commit c564e6f

Browse files
committed
Simplify image.thumbnail.
Per-filetype backend selection is already implemented by FigureCanvasBase.print_figure, no need to reinvent the wheel.
1 parent ebac433 commit c564e6f

File tree

2 files changed

+38
-50
lines changed

2 files changed

+38
-50
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Change to the output of `.image.thumbnail`
2+
``````````````````````````````````````````
3+
4+
When called with ``preview=False``, ``image.thumbnail`` previously returned an
5+
figure whose canvas class was set according to the output file extension. It
6+
now returns a figure whose canvas class is the base `FigureCanvasBase` (and
7+
relies on `FigureCanvasBase.print_figure`) to handle the canvas switching
8+
properly).
9+
10+
As a side effect of this change, `image.thumbnail` now also supports .ps, .eps,
11+
and .svgz output.

lib/matplotlib/image.py

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
The image module supports basic image loading, rescaling and display
33
operations.
4-
54
"""
65

76
from io import BytesIO
@@ -17,6 +16,7 @@
1716
from matplotlib import rcParams
1817
import matplotlib.artist as martist
1918
from matplotlib.artist import allow_rasterization
19+
from matplotlib.backend_bases import FigureCanvasBase
2020
import matplotlib.colors as mcolors
2121
import matplotlib.cm as cm
2222
import matplotlib.cbook as cbook
@@ -1447,81 +1447,58 @@ def pil_to_array(pilImage):
14471447
def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
14481448
preview=False):
14491449
"""
1450-
make a thumbnail of image in *infile* with output filename
1451-
*thumbfile*.
1452-
1453-
*infile* the image file -- must be PNG or Pillow-readable if you
1454-
have `Pillow <http://python-pillow.org/>`_ installed
1455-
1456-
*thumbfile*
1457-
the thumbnail filename
1458-
1459-
*scale*
1460-
the scale factor for the thumbnail
1450+
Make a thumbnail of image in *infile* with output filename *thumbfile*.
14611451
1462-
*interpolation*
1463-
the interpolation scheme used in the resampling
1464-
1465-
1466-
*preview*
1467-
if True, the default backend (presumably a user interface
1468-
backend) will be used which will cause a figure to be raised
1469-
if :func:`~matplotlib.pyplot.show` is called. If it is False,
1470-
a pure image backend will be used depending on the extension,
1471-
'png'->FigureCanvasAgg, 'pdf'->FigureCanvasPdf,
1472-
'svg'->FigureCanvasSVG
1452+
See :doc:`/gallery/misc/image_thumbnail_sgskip`.
14731453
1454+
Parameters
1455+
----------
1456+
infile
1457+
The image file -- must be PNG, Pillow-readable if you have `Pillow
1458+
<http://python-pillow.org/>`_ installed.
14741459
1475-
See examples/misc/image_thumbnail.py.
1460+
thumbfile
1461+
The thumbnail filename.
14761462
1477-
.. only:: html
1463+
scale
1464+
The scale factor for the thumbnail.
14781465
1479-
:ref:`sphx_glr_gallery_misc_image_thumbnail_sgskip.py`
1466+
interpolation
1467+
The interpolation scheme used in the resampling.
14801468
1481-
Return value is the figure instance containing the thumbnail
1469+
preview
1470+
If True, the default backend (presumably a user interface
1471+
backend) will be used which will cause a figure to be raised if
1472+
`~matplotlib.pyplot.show` is called. If it is False, the figure is
1473+
created using `FigureCanvasBase` and the drawing backend is selected
1474+
as `~matplotlib.figure.savefig` would normally do.
14821475
1476+
Returns
1477+
-------
1478+
figure :
1479+
The figure instance containing the thumbnail.
14831480
"""
1484-
basedir, basename = os.path.split(infile)
1485-
baseout, extout = os.path.splitext(thumbfile)
14861481

14871482
im = imread(infile)
14881483
rows, cols, depth = im.shape
14891484

1490-
# this doesn't really matter, it will cancel in the end, but we
1491-
# need it for the mpl API
1485+
# This doesn't really matter (it cancels in the end) but the API needs it.
14921486
dpi = 100
14931487

14941488
height = rows / dpi * scale
14951489
width = cols / dpi * scale
14961490

1497-
extension = extout.lower()
1498-
14991491
if preview:
1500-
# let the UI backend do everything
1492+
# Let the UI backend do everything.
15011493
import matplotlib.pyplot as plt
15021494
fig = plt.figure(figsize=(width, height), dpi=dpi)
15031495
else:
1504-
if extension == '.png':
1505-
from matplotlib.backends.backend_agg \
1506-
import FigureCanvasAgg as FigureCanvas
1507-
elif extension == '.pdf':
1508-
from matplotlib.backends.backend_pdf \
1509-
import FigureCanvasPdf as FigureCanvas
1510-
elif extension == '.svg':
1511-
from matplotlib.backends.backend_svg \
1512-
import FigureCanvasSVG as FigureCanvas
1513-
else:
1514-
raise ValueError("Can only handle "
1515-
"extensions 'png', 'svg' or 'pdf'")
1516-
15171496
from matplotlib.figure import Figure
15181497
fig = Figure(figsize=(width, height), dpi=dpi)
1519-
FigureCanvas(fig)
1498+
FigureCanvasBase(fig)
15201499

15211500
ax = fig.add_axes([0, 0, 1, 1], aspect='auto',
15221501
frameon=False, xticks=[], yticks=[])
1523-
1524-
basename, ext = os.path.splitext(basename)
15251502
ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation)
15261503
fig.savefig(thumbfile, dpi=dpi)
15271504
return fig

0 commit comments

Comments
 (0)
0