Skip to content

Commit 273d99b

Browse files
authored
Merge pull request #148 from jans-code/master
replace deprecated stdlib imghdr with filetype
2 parents e6f0b06 + fbabe31 commit 273d99b

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ __pycache__
33
build/
44
dist/
55
MANIFEST
6+
.ipynb_checkpoints

bash_kernel/display.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@
5252
is your friend to debug the format of the content message.
5353
"""
5454
import base64
55-
import imghdr
5655
import json
5756
import os
5857
import re
5958

59+
import filetype
60+
6061

6162
_TEXT_SAVED_IMAGE = "bash_kernel: saved image data to: "
6263
_TEXT_SAVED_HTML = "bash_kernel: saved html data to: "
@@ -101,15 +102,13 @@ def display_data_for_image(filename):
101102
with open(filename, 'rb') as f:
102103
image = f.read()
103104
_unlink_if_temporary(filename)
104-
105-
image_type = imghdr.what(None, image)
106-
if image_type is None:
105+
image_type = filetype.image_match(image).mime
106+
if image_type not in ("image/png", "image/jpg", "image/gif", "image/webp"):
107107
raise ValueError("Not a valid image: %s" % image)
108108

109-
image_data = base64.b64encode(image).decode('ascii')
110109
content = {
111110
'data': {
112-
'image/' + image_type: image_data
111+
image_type: image
113112
},
114113
'metadata': {}
115114
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ authors = [
88
{name = "Thomas Kluyver", email = "thomas@kluyver.me.uk"},
99
]
1010
readme = "README.rst"
11-
dependencies = ["pexpect (>=4.0)", "ipykernel"]
11+
dependencies = ["pexpect (>=4.0)", "ipykernel", "filetype"]
1212
classifiers = [
1313
"Framework :: Jupyter",
1414
"License :: OSI Approved :: BSD License",

0 commit comments

Comments
 (0)