Kaynağa Gözat

add load_as_base64

quarrying 3 yıl önce
ebeveyn
işleme
1fd7bd131d
2 değiştirilmiş dosya ile 10 ekleme ve 1 silme
  1. 1 1
      khandy/image/crop_or_pad.py
  2. 9 0
      khandy/utils_file_io.py

+ 1 - 1
khandy/image/crop_or_pad.py

@@ -30,7 +30,7 @@ def crop_or_pad(image, x_min, y_min, x_max, y_max, border_value=0):
         dst_image = np.full(dst_image_shape, border_value, dtype=image.dtype)
     elif isinstance(border_value, tuple):
         assert len(border_value) == channels, \
-            'Expected the num of elements in tuple equals the channels' \
+            'Expected the num of elements in tuple equals the channels ' \
             'of input image. Found {} vs {}'.format(
                 len(border_value), channels)
         if channels == 1:

+ 9 - 0
khandy/utils_file_io.py

@@ -1,4 +1,5 @@
 import json
+import base64
 import numbers
 from collections import OrderedDict
 
@@ -41,3 +42,11 @@ def save_json(filename, data, encoding='utf-8', cls=None, sort_keys=False):
         json.dump(data, f, indent=4, separators=(',',': '),
                   ensure_ascii=False, cls=cls, sort_keys=sort_keys)
 
+
+def load_as_base64(filename: str) -> str:
+    with open(filename, 'rb') as f:
+        raw_bytes = f.read()
+    b64_bytes = base64.b64encode(raw_bytes)
+    b64_str = b64_bytes.decode()
+    return b64_str
+