@@ -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
0 commit comments