16
16
17
17
18
18
import collections
19
- import imghdr
20
19
import json
21
20
22
21
from werkzeug import wrappers
30
29
from tensorboard .plugins .image import metadata as image_metadata
31
30
from tensorboard .plugins .metrics import metadata
32
31
from tensorboard .plugins .scalar import metadata as scalar_metadata
32
+ from tensorboard .util import img_mime_type_detector
33
33
34
34
35
- _IMGHDR_TO_MIMETYPE = {
36
- "bmp" : "image/bmp" ,
37
- "gif" : "image/gif" ,
38
- "jpeg" : "image/jpeg" ,
39
- "png" : "image/png" ,
40
- "svg" : "image/svg+xml" ,
41
- }
42
-
43
- _DEFAULT_IMAGE_MIMETYPE = "application/octet-stream"
44
-
45
35
_SINGLE_RUN_PLUGINS = frozenset (
46
36
[histogram_metadata .PLUGIN_NAME , image_metadata .PLUGIN_NAME ]
47
37
)
@@ -615,8 +605,8 @@ def _serve_image_data(self, request):
615
605
if not blob_key :
616
606
raise errors .InvalidArgumentError ("Missing 'imageId' field" )
617
607
618
- (data , content_type ) = self ._image_data_impl (ctx , blob_key )
619
- return http_util .Respond (request , data , content_type )
608
+ (data , mime_type ) = self ._image_data_impl (ctx , blob_key )
609
+ return http_util .Respond (request , data , mime_type )
620
610
621
611
def _image_data_impl (self , ctx , blob_key ):
622
612
"""Gets the image data for a blob key.
@@ -631,8 +621,5 @@ def _image_data_impl(self, ctx, blob_key):
631
621
content_type: a string HTTP content type.
632
622
"""
633
623
data = self ._data_provider .read_blob (ctx , blob_key = blob_key )
634
- image_type = imghdr .what (None , data )
635
- content_type = _IMGHDR_TO_MIMETYPE .get (
636
- image_type , _DEFAULT_IMAGE_MIMETYPE
637
- )
638
- return (data , content_type )
624
+ mime_type = img_mime_type_detector .from_bytes (data )
625
+ return (data , mime_type )
0 commit comments