@ -17,7 +17,7 @@ import av
import numpy as np
from natsort import os_sorted
from pyunpack import Archive
from PIL import Image , ImageFile
from PIL import Image , ImageFile , ImageOps
from random import shuffle
from cvat . apps . engine . utils import rotate_image
from cvat . apps . engine . models import DimensionType , SortingMethod
@ -127,9 +127,25 @@ class IMediaReader(ABC):
else :
preview = obj
preview = rotate_within_exif ( preview )
# TODO - Check if the other formats work. I'm only interested in I;16 for now. Sorry @:-|
# Summary:
# Images in the Format I;16 definitely don't work. Most likely I;16B/L/N won't work as well.
# Simple Conversion from I;16 to I/RGB/L doesn't work as well.
# Including any Intermediate Conversions doesn't work either. (eg. I;16 to I to L)
# Seems like an internal Bug of PIL
# See Issue for further details: https://github.com/python-pillow/Pillow/issues/3011
# Issue was opened 2018, so don't expect any changes soon and work with manual conversions.
mode : str = preview . mode
if mode == " I;16 " :
preview = np . array ( preview , dtype = np . uint16 ) # 'I;16' := Unsigned Integer 16, Grayscale
image = image - image . min ( ) # In case the used range lies in [a, 2^16] with a > 0
preview = preview / preview . max ( ) * 255 # Downscale into real numbers of range [0, 255]
preview = preview . astype ( np . uint8 ) # Floor to integers of range [0, 255]
preview = Image . fromarray ( preview , mode = " L " ) # 'L' := Unsigned Integer 8, Grayscale
preview = ImageOps . equalize ( preview ) # The Images need equalization. High resolution with 16-bit but only small range that actually contains information
preview . thumbnail ( PREVIEW_SIZE )
return preview . convert ( ' RGB ' )
return preview
@abstractmethod
def get_image_size ( self , i ) :
@ -589,6 +605,23 @@ class IChunkWriter(ABC):
im_data = np . array ( image )
im_data = im_data * ( 2 * * 8 / im_data . max ( ) )
image = Image . fromarray ( im_data . astype ( np . int32 ) )
# TODO - Check if the other formats work. I'm only interested in I;16 for now. Sorry @:-|
# Summary:
# Images in the Format I;16 definitely don't work. Most likely I;16B/L/N won't work as well.
# Simple Conversion from I;16 to I/RGB/L doesn't work as well.
# Including any Intermediate Conversions doesn't work either. (eg. I;16 to I to L)
# Seems like an internal Bug of PIL
# See Issue for further details: https://github.com/python-pillow/Pillow/issues/3011
# Issue was opened 2018, so don't expect any changes soon and work with manual conversions.
if image . mode == " I;16 " :
image = np . array ( image , dtype = np . uint16 ) # 'I;16' := Unsigned Integer 16, Grayscale
image = image - image . min ( ) # In case the used range lies in [a, 2^16] with a > 0
image = image / image . max ( ) * 255 # Downscale into real numbers of range [0, 255]
image = image . astype ( np . uint8 ) # Floor to integers of range [0, 255]
image = Image . fromarray ( image , mode = " L " ) # 'L' := Unsigned Integer 8, Grayscale
image = ImageOps . equalize ( image ) # The Images need equalization. High resolution with 16-bit but only small range that actually contains information
converted_image = image . convert ( ' RGB ' )
image . close ( )
buf = io . BytesIO ( )