cabrel09 commited on
Commit
7f86004
·
verified ·
1 Parent(s): 796d9a5

Update khandy/image/misc.py

Browse files
Files changed (1) hide show
  1. khandy/image/misc.py +9 -2
khandy/image/misc.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
- import imghdr
 
 
3
  import numbers
4
  import warnings
5
  from io import BytesIO
@@ -80,7 +82,12 @@ def imwrite_bytes(filename, image_bytes: bytes, update_extension: bool = True):
80
  whether update extension according to image_bytes or not.
81
  the cost of update extension is smaller than update image format.
82
  """
83
- extension = imghdr.what('', image_bytes)
 
 
 
 
 
84
  file_extension = khandy.get_path_extension(filename)
85
  # imghdr.what fails to determine image format sometimes!
86
  # so when its return value is None, never update extension.
 
1
  import os
2
+ # import imghdr
3
+ import io
4
+ from PIL import Image
5
  import numbers
6
  import warnings
7
  from io import BytesIO
 
82
  whether update extension according to image_bytes or not.
83
  the cost of update extension is smaller than update image format.
84
  """
85
+ # extension = imghdr.what('', image_bytes)
86
+ try:
87
+ with Image.open(io.BytesIO(image_bytes)) as img:
88
+ extension = img.format.lower() # .format renvoie 'JPEG', 'PNG', etc.
89
+ except Exception:
90
+ extension = None
91
  file_extension = khandy.get_path_extension(filename)
92
  # imghdr.what fails to determine image format sometimes!
93
  # so when its return value is None, never update extension.