@@ -425,6 +425,41 @@ def hflip(img):
425
425
return img .transpose (Image .FLIP_LEFT_RIGHT )
426
426
427
427
428
+ def _parse_fill (fill , img , min_pil_version ):
429
+ """Helper function to get the fill color for rotate and perspective transforms.
430
+
431
+ Args:
432
+ fill (n-tuple or int or float): Pixel fill value for area outside the transformed
433
+ image. If int or float, the value is used for all bands respectively.
434
+ Defaults to 0 for all bands.
435
+ img (PIL Image): Image to be filled.
436
+ min_pil_version (str): The minimum PILLOW version for when the ``fillcolor`` option
437
+ was first introduced in the calling function. (e.g. rotate->5.2.0, perspective->5.0.0)
438
+
439
+ Returns:
440
+ dict: kwarg for ``fillcolor``
441
+ """
442
+ if PILLOW_VERSION < min_pil_version :
443
+ if fill is None :
444
+ return {}
445
+ else :
446
+ msg = ("The option to fill background area of the transformed image, "
447
+ "requires pillow>={}" )
448
+ raise RuntimeError (msg .format (min_pil_version ))
449
+
450
+ num_bands = len (img .getbands ())
451
+ if fill is None :
452
+ fill = 0
453
+ if isinstance (fill , (int , float )) and num_bands > 1 :
454
+ fill = tuple ([fill ] * num_bands )
455
+ if not isinstance (fill , (int , float )) and len (fill ) != num_bands :
456
+ msg = ("The number of elements in 'fill' does not match the number of "
457
+ "bands of the image ({} != {})" )
458
+ raise ValueError (msg .format (len (fill ), num_bands ))
459
+
460
+ return {"fillcolor" : fill }
461
+
462
+
428
463
def _get_perspective_coeffs (startpoints , endpoints ):
429
464
"""Helper function to get the coefficients (a, b, c, d, e, f, g, h) for the perspective transforms.
430
465
@@ -450,22 +485,29 @@ def _get_perspective_coeffs(startpoints, endpoints):
450
485
return res .squeeze_ (1 ).tolist ()
451
486
452
487
453
- def perspective (img , startpoints , endpoints , interpolation = Image .BICUBIC ):
488
+ def perspective (img , startpoints , endpoints , interpolation = Image .BICUBIC , fill = None ):
454
489
"""Perform perspective transform of the given PIL Image.
455
490
456
491
Args:
457
492
img (PIL Image): Image to be transformed.
458
493
startpoints: List containing [top-left, top-right, bottom-right, bottom-left] of the orignal image
459
494
endpoints: List containing [top-left, top-right, bottom-right, bottom-left] of the transformed image
460
495
interpolation: Default- Image.BICUBIC
496
+ fill (n-tuple or int or float): Pixel fill value for area outside the rotated
497
+ image. If int or float, the value is used for all bands respectively.
498
+ This option is only available for ``pillow>=5.0.0``.
499
+
461
500
Returns:
462
501
PIL Image: Perspectively transformed Image.
463
502
"""
503
+
464
504
if not _is_pil_image (img ):
465
505
raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
466
506
507
+ opts = _parse_fill (fill , img , '5.0.0' )
508
+
467
509
coeffs = _get_perspective_coeffs (startpoints , endpoints )
468
- return img .transform (img .size , Image .PERSPECTIVE , coeffs , interpolation )
510
+ return img .transform (img .size , Image .PERSPECTIVE , coeffs , interpolation , ** opts )
469
511
470
512
471
513
def vflip (img ):
@@ -721,30 +763,10 @@ def rotate(img, angle, resample=False, expand=False, center=None, fill=None):
721
763
.. _filters: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#filters
722
764
723
765
"""
724
- def parse_fill (fill , num_bands ):
725
- if PILLOW_VERSION < "5.2.0" :
726
- if fill is None :
727
- return {}
728
- else :
729
- msg = ("The option to fill background area of the rotated image, "
730
- "requires pillow>=5.2.0" )
731
- raise RuntimeError (msg )
732
-
733
- if fill is None :
734
- fill = 0
735
- if isinstance (fill , (int , float )) and num_bands > 1 :
736
- fill = tuple ([fill ] * num_bands )
737
- if not isinstance (fill , (int , float )) and len (fill ) != num_bands :
738
- msg = ("The number of elements in 'fill' does not match the number of "
739
- "bands of the image ({} != {})" )
740
- raise ValueError (msg .format (len (fill ), num_bands ))
741
-
742
- return {"fillcolor" : fill }
743
-
744
766
if not _is_pil_image (img ):
745
767
raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
746
768
747
- opts = parse_fill (fill , len ( img . getbands ()) )
769
+ opts = _parse_fill (fill , img , '5.2.0' )
748
770
749
771
return img .rotate (angle , resample , expand , center , ** opts )
750
772
0 commit comments