Jelajahi Sumber

add center_pad

quarrying 3 tahun lalu
induk
melakukan
eca7a1c7fe
2 mengubah file dengan 29 tambahan dan 1 penghapusan
  1. 28 0
      khandy/image/crop_or_pad.py
  2. 1 1
      setup.py

+ 28 - 0
khandy/image/crop_or_pad.py

@@ -106,3 +106,31 @@ def center_crop(image, dst_width, dst_height, strict=True):
     cropped = image[crop_top: dst_height + crop_top, 
                     crop_left: dst_width + crop_left, ...]
     return cropped
+
+
+def center_pad(image, dst_width, dst_height, strict=True):
+    """
+    strict: 
+        when True, raise error if src size is greater than dst size. 
+        when False, remain unchanged if src size is greater than dst size, otherwise center pad.
+    """
+    assert image.ndim in [2, 3]
+    assert isinstance(dst_width, numbers.Integral) and isinstance(dst_height, numbers.Integral)
+    
+    src_height, src_width = image.shape[:2]
+    if strict:
+        assert (src_height <= dst_height) and (src_width <= dst_width)
+    
+    padding_x = max(dst_width - src_width, 0)
+    padding_y = max(dst_height - src_height, 0)
+    padding_top = padding_y // 2
+    padding_left = padding_x // 2
+    if image.ndim == 2:
+        padding = ((padding_top, padding_y - padding_top), 
+                   (padding_left, padding_x - padding_left))
+    else:
+        padding = ((padding_top, padding_y - padding_top), 
+                   (padding_left, padding_x - padding_left), (0, 0))
+    return np.pad(image, padding, 'constant')
+
+    

+ 1 - 1
setup.py

@@ -5,7 +5,7 @@ install_requires = ['numpy>=1.11.1', 'opencv-python']
 
 setup(
     name='KHandy',
-    version='0.1',
+    version='0.1.1',
     description='Handy Utilities for Computer Vision',
     long_description='Handy Utilities for Computer Vision',
     keywords='computer vision',