8000 MNT pass black CI job · scikit-learn/scikit-learn@037c9ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 037c9ab

Browse files
MNT pass black CI job
1 parent fddd481 commit 037c9ab

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

sklearn/utils/_pilutil.py

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,20 @@
4343

4444
import numpy
4545

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+
)
4860

4961
# Modification of original scipy pilutil.py to make this module importable if
5062
# pillow is not installed. If pillow is not installed, functions will raise
@@ -55,12 +67,12 @@
5567
except ImportError:
5668
import Image
5769
pillow_installed = True
58-
if not hasattr(Image, 'frombytes'):
70+
if not hasattr(Image, "frombytes"):
5971
Image.frombytes = Image.fromstring
6072
except ImportError:
6173
pillow_installed = False
6274

63-
__all__ = ['bytescale', 'imread', 'imsave', 'fromimage', 'toimage', 'imresize']
75+
__all__ = ["bytescale", "imread", "imsave", "fromimage", "toimage", "imresize"]
6476

6577

6678
PILLOW_ERROR_MESSAGE = (
@@ -293,26 +305,26 @@ def fromimage(im, flatten=False, mode=None):
293305
if mode is not None:
294306
if mode != im.mode:
295307
im = im.convert(mode)
296-
elif im.mode == 'P':
308+
elif im.mode == "P":
297309
# Mode 'P' means there is an indexed "palette". If we leave the mode
298310
# as 'P', then when we do `a = array(im)` below, `a` will be a 2-D
299311
# containing the indices into the palette, and not a 3-D array
300312
# 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")
303315
else:
304-
im = im.convert('RGB')
316+
im = im.convert("RGB")
305317

306318
if flatten:
307-
im = im.convert('F')
308-
elif im.mode == '1':
319+
im = im.convert("F")
320+
elif im.mode == "1":
309321
# Workaround for crash in PIL. When im is 1-bit, the call array(im)
310322
# can cause a seg. fault, or generate garbage. See
311323
# https://github.com/scipy/scipy/issues/2138 and
312324
# https://github.com/python-pillow/Pillow/issues/350.
313325
#
314326
# This converts im from a 1-bit image to an 8-bit image.
315-
im = im.convert('L')
327+
im = im.convert("L")
316328

317329
a = array(im)
318330
return a
@@ -321,8 +333,9 @@ def fromimage(im, flatten=False, mode=None):
321333
_errstr = "Mode is unknown or incompatible with input array shape."
322334

323335

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, cmin=None, cmax=None, pal=None, mode=None, channel_axis=None
338+
):
326339
"""Takes a numpy array and returns a PIL image.
327340
328341
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,
359372
if iscomplexobj(data):
360373
raise ValueError("Cannot convert a complex-valued array.")
361374
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)))
364376
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.")
367378
if len(shape) == 2:
368379
shape = (shape[1], shape[0]) # columns show up first
369-
if mode == 'F':
380+
if mode == "F":
370381
data32 = data.astype(numpy.float32)
371382
image = Image.frombytes(mode, shape, data32.tobytes())
372383
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())
377387
if pal is not None:
378388
image.putpalette(asarray(pal, dtype=uint8).tobytes())
379389
# 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+
)
383395
image.putpalette(asarray(pal, dtype=uint8).tobytes())
384396
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())
388400
return image
389401
if cmin is None:
390402
cmin = amin(ravel(data))
391403
if cmax is None:
392404
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":
395407
data32 = data.astype(numpy.uint32)
396408
image = Image.frombytes(mode, shape, data32.tobytes())
397409
else:
@@ -401,7 +413,7 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
401413
# if here then 3-d array with a 3 or a 4 in the shape length.
402414
# Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
403415
if channel_axis is None:
404-
if (3 in shape):
416+
if 3 in shape:
405417
ca = numpy.flatnonzero(asarray(shape) == 3)[0]
406418
else:
407419
ca = numpy.flatnonzero(asarray(shape) == 4)
@@ -428,17 +440,17 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
428440
shape = (shape[2], shape[1])
429441
if mode is None:
430442
if numch == 3:
431-
mode = 'RGB'
443+
mode = "RGB"
432444
else:
433-
mode = 'RGBA'
445+
mode = "RGBA"
434446

435-
if mode not in ['RGB', 'RGBA', 'YCbCr', 'CMYK']:
447+
if mode not in ["RGB", "RGBA", "YCbCr", "CMYK"]:
436448
raise ValueError(_errstr)
437449

438-
if mode in ['RGB', 'YCbCr']:
450+
if mode in ["RGB", "YCbCr"]:
439451
if numch != 3:
440452
raise ValueError("Invalid array shape for mode.")
441-
if mode in ['RGBA', 'CMYK']:
453+
if mode in ["RGBA", "CMYK"]:
442454
if numch != 4:
443455
raise ValueError("Invalid array shape for mode.")
444456

@@ -447,7 +459,7 @@ def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
447459
return image
448460

449461

450-
def imresize(arr, size, interp='bilinear', mode=None):
462+
def imresize(arr, size, interp="bilinear", mode=None):
451463
"""
452464
Resize an image.
453465
@@ -493,11 +505,11 @@ def imresize(arr, size, interp='bilinear', mode=None):
493505
ts = type(size)
494506
if issubdtype(ts, numpy.signedinteger):
495507
percent = size / 100.0
496-
size = tuple((array(im.size)*percent).astype(int))
508+
size = tuple((array(im.size) * percent).astype(int))
497509
elif issubdtype(type(size), numpy.floating):
498-
size = tuple((array(im.size)*size).astype(int))
510+
size = tuple((array(im.size) * size).astype(int))
499511
else:
500512
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}
502514
imnew = im.resize(size, resample=func[interp])
503515
return fromimage(imnew)

0 commit comments

Comments
 (0)
0