Bläddra i källkod

add imread and imwrite utils

quarrying 3 år sedan
förälder
incheckning
c8dc639ce5
3 ändrade filer med 29 tillägg och 2 borttagningar
  1. 27 0
      khandy/image/misc.py
  2. 1 1
      khandy/version.py
  3. 1 1
      setup.py

+ 27 - 0
khandy/image/misc.py

@@ -1,8 +1,35 @@
+import os
+
 import cv2
 import khandy
 import numpy as np
+from PIL import Image
 
 
+def imread_pil(filename, to_mode='RGB'):
+    with open(filename, 'rb') as f:
+        img = Image.open(f)
+        if to_mode is None:
+            return img
+        else:
+            return img.convert(to_mode)
+            
+            
+def imread_cv(filename, flags=-1):
+    """Improvement on cv2.imread, make it support filename including chinese character.
+    """
+    try:
+        return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), flags)
+    except Exception as e:
+        return None
+    
+    
+def imwrite_cv(filename, image):
+    """Improvement on cv2.imwrite, make it support filename including chinese character.
+    """
+    cv2.imencode(os.path.splitext(filename)[-1], image)[1].tofile(filename)
+    
+    
 def normalize_image_dtype(image, keep_num_channels=False):
     """Normalize image dtype to uint8 (usually for visualization).
     

+ 1 - 1
khandy/version.py

@@ -1,3 +1,3 @@
-__version__ = '0.1.5'
+__version__ = '0.1.6'
 
 __all__ = ['__version__']

+ 1 - 1
setup.py

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