Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c31886

Browse files
committed
added image.thumbnail function
svn path=/trunk/matplotlib/; revision=6282
1 parent 7524345 commit 1c31886

7 files changed

Lines changed: 118 additions & 13 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2008-10-20 Added image thumbnail generating function
2+
matplotlib.image.thumbnail. See
3+
examples/misc/image_thumbnail.py - JDH
4+
15
2008-10-20 Applied scatleg patch based on ideas and work by Erik
26
Tollerud and Jae-Joon Lee. - MM
37

doc/_static/logo_sidebar_horiz.png

-123 Bytes
Loading

examples/tests/backend_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
'alignment_test.py',
2929
'arctest.py',
3030
'arrow_demo.py',
31-
'auto_layout.py',
31+
#'auto_layout.py',
3232
'axes_demo.py',
3333
'axhspan_demo.py',
3434
'bar_stacked.py',

lib/matplotlib/axes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5453,10 +5453,12 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54535453
If *None*, default to rc ``image.aspect`` value.
54545454
54555455
*interpolation*:
5456-
Acceptable values are *None*, 'nearest', 'bilinear', 'bicubic',
5457-
'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',
5458-
'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc',
5459-
'lanczos', 'blackman'
5456+
5457+
Acceptable values are *None*, 'nearest', 'bilinear',
5458+
'bicubic', 'spline16', 'spline36', 'hanning', 'hamming',
5459+
'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian',
5460+
'bessel', 'mitchell', 'sinc', 'lanczos',
5461+
54605462
54615463
If *interpolation* is *None*, default to rc
54625464
``image.interpolation``. See also the *filternorm* and

lib/matplotlib/image.py

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,21 @@ def get_interpolation(self):
319319
"""
320320
Return the interpolation method the image uses when resizing.
321321
322-
One of
323-
324-
'bicubic', 'bilinear', 'blackman100', 'blackman256', 'blackman64',
325-
'nearest', 'sinc144', 'sinc256', 'sinc64', 'spline16', 'spline36'
322+
One of 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning',
323+
'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian',
324+
'bessel', 'mitchell', 'sinc', 'lanczos',
326325
"""
327326
return self._interpolation
328327

329328
def set_interpolation(self, s):
330329
"""
331330
Set the interpolation method the image uses when resizing.
332331
333-
ACCEPTS: ['bicubic' | 'bilinear' | 'blackman100' | 'blackman256' |
334-
'blackman64', 'nearest' | 'sinc144' | 'sinc256' | 'sinc64' |
335-
'spline16' | 'spline36']
332+
ACCEPTS: ['nearest' | 'bilinear' | 'bicubic' | 'spline16' |
333+
'spline36' | 'hanning' | 'hamming' | 'hermite' | 'kaiser' |
334+
'quadric' | 'catrom' | 'gaussian' | 'bessel' | 'mitchell' |
335+
'sinc' | 'lanczos' | ]
336+
336337
"""
337338
if s is None: s = rcParams['image.interpolation']
338339
s = s.lower()
@@ -764,3 +765,100 @@ def toarray(im):
764765
x = toarray(im)
765766
x.shape = im.size[1], im.size[0], 4
766767
return x
768+
769+
def thumbnail(fname, scale=0.1, interpolation='bilinear', prefix='thumb_',
770+
outdir=None, preview=False, extension='png'):
771+
"""
772+
make a thumbnail of image in fname with output filename
773+
[OUTDIR]/[PREFIX][BASENAME].EXTENSION where BASENAME is the
774+
filename part of fname without the directory ar extension
775+
776+
*fname* the image file -- must be PNG or PIL readable if you
777+
have `PIL <http://www.pythonware.com/products/pil/>`_ installed
778+
779+
*scale*
780+
the scale factor for the thumbnail
781+
782+
*interpolation*
783+
the interpolation scheme used in the resampling
784+
785+
*prefix*
786+
the PREFIX of the output file name
787+
788+
*outdir*
789+
the OUTDIR of the output filenamet - by default same as directory the source image lives in
790+
791+
*extension*
792+
the EXTENSION for the output filename, one of 'png', 'pdf' or 'ps'
793+
794+
*preview*
795+
if True, the default backend (presumably a user interface
796+
backend) will be used which will cause a figure to be raised
797+
if :func:`~matplotlib.pyplot.show` is called. If it is False,
798+
a pure image backend will be used depending on the extension,
799+
'png'->FigureCanvasAgg, 'pdf'->FigureCanvasPDF,
800+
'svg'->FigureCanvasSVG
801+
802+
803+
See examples/misc/image_thumbnail.py.
804+
805+
.. htmlonly::
806+
807+
:ref:`misc-image_thumbnail`
808+
809+
Return value is the output filename of the thumbnail
810+
811+
"""
812+
813+
basedir, basename = os.path.split(fname)
814+
if outdir is None:
815+
if not basedir:
816+
basedir = os.curdir
817+
outdir = basedir
818+
819+
if not os.path.exists(outdir) or not os.path.isdir(outdir):
820+
raise IOError('output directory "%s" must be a directory'%outdir)
821+
822+
im = imread(fname)
823+
rows, cols, depth = im.shape
824+
825+
# this doesn't really matter, it will cancel in the end, but we
826+
# need it for the mpl API
827+
dpi = 100
828+
829+
830+
height = float(rows)/dpi*scale
831+
width = float(cols)/dpi*scale
832+
833+
extension = extension.lower()
834+
835+
if preview:
836+
# let the UI backend do everything
837+
import matplotlib.pyplot as plt
838+
fig = plt.figure(figsize=(width, height), dpi=dpi)
839+
else:
840+
if extension=='png':
841+
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
842+
from matplotlib.figure import Figure
843+
elif extension=='pdf':
844+
from matplotlib.backends.backend_pdf import FigureCanvasPDF as FigureCanvas
845+
from matplotlib.figure import Figure
846+
elif extension=='svg':
847+
from matplotlib.backends.backend_svg import FigureCanvasSVG as FigureCanvas
848+
from matplotlib.figure import Figure
849+
else:
850+
raise ValueError("Can only handle extension 'png', 'svg' or 'pdf'")
851+
852+
fig = Figure(figsize=(width, height), dpi=dpi)
853+
canvas = FigureCanvas(fig)
854+
855+
856+
857+
858+
ax = fig.add_axes([0,0,1,1], aspect='auto', frameon=False, xticks=[], yticks=[])
859+
860+
basename, ext = os.path.splitext(basename)
861+
ax.imshow(im, aspect='auto', resample=True, interpolation='bilinear')
862+
outfile = os.path.join(outdir, '%s%s.%s'%(prefix, basename, extension))
863+
fig.savefig(outfile, dpi=dpi)
864+
return outfile

matplotlibrc.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ numerix : %(numerix)s # numpy, Numeric or numarray
265265
#image.cmap : jet # gray | jet etc...
266266
#image.lut : 256 # the size of the colormap lookup table
267267
#image.origin : upper # lower | upper
268-
268+
#image.resample : False
269269

270270
### CONTOUR PLOTS
271271
#contour.negative_linestyle : dashed # dashed | solid

setupext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ def parse_tcl_config(tcl_lib_dir, tk_lib_dir):
942942
try:
943943
tcl_lib = tcl_vars.get("default", "TCL_LIB_SPEC")[1:-1].split()[0][2:]
944944
tcl_inc = tcl_vars.get("default", "TCL_INCLUDE_SPEC")[3:-1]
945+
945946
tk_lib = tk_vars.get("default", "TK_LIB_SPEC")[1:-1].split()[0][2:]
946947
if tk_vars.has_option("default", "TK_INCLUDE_SPEC"):
947948
# On Ubuntu 8.04

0 commit comments

Comments
 (0)