ソースを参照

refactor imread_cv

quarrying 3 年 前
コミット
885cc75195
1 ファイル変更5 行追加2 行削除
  1. 5 2
      khandy/image/misc.py

+ 5 - 2
khandy/image/misc.py

@@ -15,11 +15,14 @@ def imread_pil(filename, to_mode='RGB'):
             return img.convert(to_mode)
             
             
-def imread_cv(filename, flags=-1):
+def imread_cv(filename_or_buffer, flags=-1):
     """Improvement on cv2.imread, make it support filename including chinese character.
     """
     try:
-        return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), flags)
+        if isinstance(filename_or_buffer, bytes):
+            return cv2.imdecode(np.frombuffer(filename_or_buffer, dtype=np.uint8), flags)
+        else:
+            return cv2.imdecode(np.fromfile(filename_or_buffer, dtype=np.uint8), flags)
     except Exception as e:
         return None