43
43
44
44
import numpy
45
45
46
- from numpy import (amin , amax , ravel , asarray , arange , ones , newaxis ,
47
- transpose , iscomplexobj , uint8 , issubdtype , array )
46
+ from numpy import (
47
+ amin ,
48
+ amax ,
49
+ ravel ,
50
+ asarray ,
51
+ arange ,
52
+ ones ,
53
+ newaxis ,
54
+ transpose ,
55
+ iscomplexobj ,
56
+ uint8 ,
57
+ issubdtype ,
58
+ array ,
59
+ )
48
60
49
61
# Modification of original scipy pilutil.py to make this module importable if
50
62
# pillow is not installed. If pillow is not installed, functions will raise
55
67
except ImportError :
56
68
import Image
57
69
pillow_installed = True
58
- if not hasattr (Image , ' frombytes' ):
70
+ if not hasattr (Image , " frombytes" ):
59
71
Image .frombytes = Image .fromstring
60
72
except ImportError :
61
73
pillow_installed = False
62
74
63
- __all__ = [' bytescale' , ' imread' , ' imsave' , ' fromimage' , ' toimage' , ' imresize' ]
75
+ __all__ = [" bytescale" , " imread" , " imsave" , " fromimage" , " toimage" , " imresize" ]
64
76
65
77
66
78
PILLOW_ERROR_MESSAGE = (
@@ -293,26 +305,26 @@ def fromimage(im, flatten=False, mode=None):
293
305
if mode is not None :
294
306
if mode != im .mode :
295
307
im = im .convert (mode )
296
- elif im .mode == 'P' :
308
+ elif im .mode == "P" :
297
309
# Mode 'P' means there is an indexed "palette". If we leave the mode
298
310
# as 'P', then when we do `a = array(im)` below, `a` will be a 2-D
299
311
# containing the indices into the palette, and not a 3-D array
300
312
# containing the RGB or RGBA values.
301
- if ' transparency' in im .info :
302
- im = im .convert (' RGBA' )
313
+ if " transparency" in im .info :
314
+ im = im .convert (" RGBA" )
303
315
else :
304
- im = im .convert (' RGB' )
316
+ im = im .convert (" RGB" )
305
317
306
318
if flatten :
307
- im = im .convert ('F' )
308
- elif im .mode == '1' :
319
+ im = im .convert ("F" )
320
+ elif im .mode == "1" :
309
321
# Workaround for crash in PIL. When im is 1-bit, the call array(im)
310
322
# can cause a seg. fault, or generate garbage. See
311
323
# https://github.com/scipy/scipy/issues/2138 and
312
324
# https://github.com/python-pillow/Pillow/issues/350.
313
325
#
314
326
# This converts im from a 1-bit image to an 8-bit image.
315
- im = im .convert ('L' )
327
+ im = im .convert ("L" )
316
328
317
329
a = array (im )
318
330
return a
@@ -321,8 +333,9 @@ def fromimage(im, flatten=False, mode=None):
321
333
_errstr = "Mode is unknown or incompatible with input array shape."
322
334
323
335
324
- def toimage (arr , high = 255 , low = 0 , cmin = None , cmax = None , pal = None ,
325
- mode = None , channel_axis = None ):
336
+ def toimage (
337
+ arr , high = 255 , low = 0
9E81
span>, cmin = None , cmax = None , pal = None , mode = None , channel_axis = None
338
+ ):
326
339
"""Takes a numpy array and returns a PIL image.
327
340
328
341
This function is only available if Python Imaging Library (PIL) is installed.
@@ -359,39 +372,38 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
359
372
if iscomplexobj (data ):
360
373
raise ValueError ("Cannot convert a complex-valued array." )
361
374
shape = list (data .shape )
362
- valid = len (shape ) == 2 or ((len (shape ) == 3 ) and
363
- ((3 in shape ) or (4 in shape )))
375
+ valid = len (shape ) == 2 or ((len (shape ) == 3 ) and ((3 in shape ) or (4 in shape )))
364
376
if not valid :
365
- raise ValueError ("'arr' does not have a suitable array shape for "
366
- "any mode." )
377
+ raise ValueError ("'arr' does not have a suitable array shape for any mode." )
367
378
if len (shape ) == 2 :
368
379
shape = (shape [1 ], shape [0 ]) # columns show up first
369
- if mode == 'F' :
380
+ if mode == "F" :
370
381
data32 = data .astype (numpy .float32 )
371
382
image = Image .frombytes (mode , shape , data32 .tobytes ())
372
383
F438
return image
373
- if mode in [None , 'L' , 'P' ]:
374
- bytedata = bytescale (data , high = high , low = low ,
375
- cmin = cmin , cmax = cmax )
376
- image = Image .frombytes ('L' , shape , bytedata .tobytes ())
384
+ if mode in [None , "L" , "P" ]:
385
+ bytedata = bytescale (data , high = high , low = low , cmin = cmin , cmax = cmax )
386
+ image = Image .frombytes ("L" , shape , bytedata .tobytes ())
377
387
if pal is not None :
378
388
image .putpalette (asarray (pal , dtype = uint8 ).tobytes ())
379
389
# Becomes a mode='P' automagically.
380
- elif mode == 'P' : # default gray-scale
381
- pal = (arange (0 , 256 , 1 , dtype = uint8 )[:, newaxis ] *
382
- ones ((3 ,), dtype = uint8 )[newaxis , :])
390
+ elif mode == "P" : # default gray-scale
391
+ pal = (
392
+ arange (0 , 256 , 1 , dtype = uint8 )[:, newaxis ]
393
+ * ones ((3 ,), dtype = uint8 )[newaxis , :]
394
+ )
383
395
image .putpalette (asarray (pal , dtype = uint8 ).tobytes ())
384
396
return image
385
- if mode == '1' : # high input gives threshold for 1
386
- bytedata = ( data > high )
387
- image = Image .frombytes ('1' , shape , bytedata .tobytes ())
397
+ if mode == "1" : # high input gives threshold for 1
398
+ bytedata = data > high
399
+ image = Image .frombytes ("1" , shape , bytedata .tobytes ())
388
400
return image
389
401
if cmin is None :
390
402
cmin = amin (ravel (data ))
391
403
if cmax is None :
392
404
cmax = amax (ravel (data ))
393
- data = (data * 1.0 - cmin )* (high - low )/ (cmax - cmin ) + low
394
- if mode == 'I' :
405
+ data = (data * 1.0 - cmin ) * (high - low ) / (cmax - cmin ) + low
406
+ if mode == "I" :
395
407
data32 = data .astype (numpy .uint32 )
396
408
image = Image .frombytes (mode , shape , data32 .tobytes ())
397
409
else :
@@ -401,7 +413,7 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
401
413
# if here then 3-d array with a 3 or a 4 in the shape length.
402
414
# Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
403
415
if channel_axis is None :
404
- if ( 3 in shape ) :
416
+ if 3 in shape :
405
417
ca = numpy .flatnonzero (asarray (shape ) == 3 )[0 ]
406
418
else :
407
419
ca = numpy .flatnonzero (asarray (shape ) == 4 )
@@ -428,17 +440,17 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
428
440
shape = (shape [2 ], shape [1 ])
429
441
if mode is None :
430
442
if numch == 3 :
431
- mode = ' RGB'
443
+ mode = " RGB"
432
444
else :
433
- mode = ' RGBA'
445
+ mode = " RGBA"
434
446
435
- if mode not in [' RGB' , ' RGBA' , ' YCbCr' , ' CMYK' ]:
447
+ if mode not in [" RGB" , " RGBA" , " YCbCr" , " CMYK" ]:
436
448
raise ValueError (_errstr )
437
449
438
- if mode in [' RGB' , ' YCbCr' ]:
450
+ if mode in [" RGB" , " YCbCr" ]:
439
451
if numch != 3 :
440
452
raise ValueError ("Invalid array shape for mode." )
441
- if mode in [' RGBA' , ' CMYK' ]:
453
+ if mode in [" RGBA" , " CMYK" ]:
442
454
if numch != 4 :
443
455
raise ValueError ("Invalid array shape for mode." )
444
456
@@ -447,7 +459,7 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
447
459
return image
448
460
449
461
450
- def imresize (arr , size , interp = ' bilinear' , mode = None ):
462
+ def imresize (arr , size , interp = " bilinear" , mode = None ):
451
463
"""
452
464
Resize an image.
453
465
@@ -493,11 +505,11 @@ def imresize(arr, size, interp='bilinear', mode=None):
493
505
ts = type (size )
494
506
if issubdtype (ts , numpy .signedinteger ):
495
507
percent = size / 100.0
496
- size = tuple ((array (im .size )* percent ).astype (int ))
508
+ size = tuple ((array (im .size ) * percent ).astype (int ))
497
509
elif issubdtype (type (size ), numpy .floating ):
498
- size = tuple ((array (im .size )* size ).astype (int ))
510
+ size = tuple ((array (im .size ) * size ).astype (int ))
499
511
else :
500
512
size = (size [1 ], size [0 ])
501
- func = {' nearest' : 0 , ' lanczos' : 1 , ' bilinear' : 2 , ' bicubic' : 3 , ' cubic' : 3 }
513
+ func = {" nearest" : 0 , " lanczos" : 1 , " bilinear" : 2 , " bicubic" : 3 , " cubic" : 3 }
502
514
imnew = im .resize (size , resample = func [interp ])
503
515
return fromimage (imnew )
0 commit comments