|
1 | 1 | """
|
2 | 2 | The image module supports basic image loading, rescaling and display
|
3 | 3 | operations.
|
4 |
| -
|
5 | 4 | """
|
6 | 5 |
|
7 | 6 | from io import BytesIO
|
|
17 | 16 | from matplotlib import rcParams
|
18 | 17 | import matplotlib.artist as martist
|
19 | 18 | from matplotlib.artist import allow_rasterization
|
| 19 | +from matplotlib.backend_bases import FigureCanvasBase |
20 | 20 | import matplotlib.colors as mcolors
|
21 | 21 | import matplotlib.cm as cm
|
22 | 22 | import matplotlib.cbook as cbook
|
@@ -1447,81 +1447,58 @@ def pil_to_array(pilImage):
|
1447 | 1447 | def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
|
1448 | 1448 | preview=False):
|
1449 | 1449 | """
|
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*. |
1461 | 1451 |
|
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`. |
1473 | 1453 |
|
| 1454 | + Parameters |
| 1455 | + ---------- |
| 1456 | + infile |
| 1457 | + The image file -- must be PNG, Pillow-readable if you have `Pillow |
| 1458 | + <http://python-pillow.org/>`_ installed. |
1474 | 1459 |
|
1475 |
| - See examples/misc/image_thumbnail.py. |
| 1460 | + thumbfile |
| 1461 | + The thumbnail filename. |
1476 | 1462 |
|
1477 |
| - .. only:: html |
| 1463 | + scale |
| 1464 | + The scale factor for the thumbnail. |
1478 | 1465 |
|
1479 |
| - :ref:`sphx_glr_gallery_misc_image_thumbnail_sgskip.py` |
| 1466 | + interpolation |
| 1467 | + The interpolation scheme used in the resampling. |
1480 | 1468 |
|
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. |
1482 | 1475 |
|
| 1476 | + Returns |
| 1477 | + ------- |
| 1478 | + figure : |
| 1479 | + The figure instance containing the thumbnail. |
1483 | 1480 | """
|
1484 |
| - basedir, basename = os.path.split(infile) |
1485 |
| - baseout, extout = os.path.splitext(thumbfile) |
1486 | 1481 |
|
1487 | 1482 | im = imread(infile)
|
1488 | 1483 | rows, cols, depth = im.shape
|
1489 | 1484 |
|
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. |
1492 | 1486 | dpi = 100
|
1493 | 1487 |
|
1494 | 1488 | height = rows / dpi * scale
|
1495 | 1489 | width = cols / dpi * scale
|
1496 | 1490 |
|
1497 |
| - extension = extout.lower() |
1498 |
| - |
1499 | 1491 | if preview:
|
1500 |
| - # let the UI backend do everything |
| 1492 | + # Let the UI backend do everything. |
1501 | 1493 | import matplotlib.pyplot as plt
|
1502 | 1494 | fig = plt.figure(figsize=(width, height), dpi=dpi)
|
1503 | 1495 | 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 |
| - |
1517 | 1496 | from matplotlib.figure import Figure
|
1518 | 1497 | fig = Figure(figsize=(width, height), dpi=dpi)
|
1519 |
| - FigureCanvas(fig) |
| 1498 | + FigureCanvasBase(fig) |
1520 | 1499 |
|
1521 | 1500 | ax = fig.add_axes([0, 0, 1, 1], aspect='auto',
|
1522 | 1501 | frameon=False, xticks=[], yticks=[])
|
1523 |
| - |
1524 |
| - basename, ext = os.path.splitext(basename) |
1525 | 1502 | ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation)
|
1526 | 1503 | fig.savefig(thumbfile, dpi=dpi)
|
1527 | 1504 | return fig
|
0 commit comments