##// END OF EJS Templates
Support gif files, urls, and format
Grant Nestor -
Show More
@@ -23,7 +23,7 b' from IPython.utils.py3compat import cast_unicode'
23 from IPython.testing.skipdoctest import skip_doctest
23 from IPython.testing.skipdoctest import skip_doctest
24
24
25 __all__ = ['display', 'display_pretty', 'display_html', 'display_markdown',
25 __all__ = ['display', 'display_pretty', 'display_html', 'display_markdown',
26 'display_svg', 'display_png', 'display_jpeg', 'display_latex', 'display_json',
26 'display_svg', 'display_png', 'display_jpeg', 'display_gif', 'display_latex', 'display_json',
27 'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject',
27 'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject',
28 'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'GeoJSON', 'Javascript',
28 'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'GeoJSON', 'Javascript',
29 'Image', 'clear_output', 'set_matplotlib_formats', 'set_matplotlib_close',
29 'Image', 'clear_output', 'set_matplotlib_formats', 'set_matplotlib_close',
@@ -96,6 +96,7 b' def publish_display_data(data, metadata=None, source=None, *, transient=None, **'
96 * application/javascript
96 * application/javascript
97 * image/png
97 * image/png
98 * image/jpeg
98 * image/jpeg
99 * image/gif
99 * image/svg+xml
100 * image/svg+xml
100
101
101 Parameters
102 Parameters
@@ -256,6 +257,7 b' def display(*objs, include=None, exclude=None, metadata=None, transient=None, di'
256 - `_repr_json_`: return a JSONable dict
257 - `_repr_json_`: return a JSONable dict
257 - `_repr_jpeg_`: return raw JPEG data
258 - `_repr_jpeg_`: return raw JPEG data
258 - `_repr_png_`: return raw PNG data
259 - `_repr_png_`: return raw PNG data
260 - `_repr_gif_`: return raw PNG data
259 - `_repr_svg_`: return raw SVG data as a string
261 - `_repr_svg_`: return raw SVG data as a string
260 - `_repr_latex_`: return LaTeX commands in a string surrounded by "$".
262 - `_repr_latex_`: return LaTeX commands in a string surrounded by "$".
261 - `_repr_mimebundle_`: return a full mimebundle containing the mapping
263 - `_repr_mimebundle_`: return a full mimebundle containing the mapping
@@ -494,6 +496,22 b' def display_jpeg(*objs, **kwargs):'
494 Metadata to be associated with the specific mimetype output.
496 Metadata to be associated with the specific mimetype output.
495 """
497 """
496 _display_mimetype('image/jpeg', objs, **kwargs)
498 _display_mimetype('image/jpeg', objs, **kwargs)
499
500 def display_gif(*objs, **kwargs):
501 """Display the GIF representation of an object.
502
503 Parameters
504 ----------
505 objs : tuple of objects
506 The Python objects to display, or if raw=True raw gif data to
507 display.
508 raw : bool
509 Are the data objects raw data or Python objects that need to be
510 formatted before display? [default: False]
511 metadata : dict (optional)
512 Metadata to be associated with the specific mimetype output.
513 """
514 _display_mimetype('image/gif', objs, **kwargs)
497
515
498
516
499 def display_latex(*objs, **kwargs):
517 def display_latex(*objs, **kwargs):
@@ -927,7 +945,7 b' class Javascript(TextDisplayObject):'
927 raise TypeError('expected sequence, got: %r' % css)
945 raise TypeError('expected sequence, got: %r' % css)
928 self.lib = lib
946 self.lib = lib
929 self.css = css
947 self.css = css
930 super(Javascript, self).__init__(data=data, url=url, filename=filename)
948 superg(Javascript, self).__init__(data=data, url=url, filename=filename)
931
949
932 def _repr_javascript_(self):
950 def _repr_javascript_(self):
933 r = ''
951 r = ''
@@ -974,12 +992,13 b' class Image(DisplayObject):'
974 _read_flags = 'rb'
992 _read_flags = 'rb'
975 _FMT_JPEG = u'jpeg'
993 _FMT_JPEG = u'jpeg'
976 _FMT_PNG = u'png'
994 _FMT_PNG = u'png'
977 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
995 _FMT_GIF = u'gif'
996 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG, _FMT_GIF]
978
997
979 def __init__(self, data=None, url=None, filename=None, format=None,
998 def __init__(self, data=None, url=None, filename=None, format=None,
980 embed=None, width=None, height=None, retina=False,
999 embed=None, width=None, height=None, retina=False,
981 unconfined=False, metadata=None):
1000 unconfined=False, metadata=None):
982 """Create a PNG/JPEG image object given raw data.
1001 """Create a PNG/JPEG/GIF image object given raw data.
983
1002
984 When this object is returned by an input cell or passed to the
1003 When this object is returned by an input cell or passed to the
985 display function, it will result in the image being displayed
1004 display function, it will result in the image being displayed
@@ -997,7 +1016,7 b' class Image(DisplayObject):'
997 Path to a local file to load the data from.
1016 Path to a local file to load the data from.
998 Images from a file are always embedded.
1017 Images from a file are always embedded.
999 format : unicode
1018 format : unicode
1000 The format of the image data (png/jpeg/jpg). If a filename or URL is given
1019 The format of the image data (png/jpeg/jpg/gif). If a filename or URL is given
1001 for format will be inferred from the filename extension.
1020 for format will be inferred from the filename extension.
1002 embed : bool
1021 embed : bool
1003 Should the image data be embedded using a data URI (True) or be
1022 Should the image data be embedded using a data URI (True) or be
@@ -1059,6 +1078,9 b' class Image(DisplayObject):'
1059 format = self._FMT_JPEG
1078 format = self._FMT_JPEG
1060 if ext == u'png':
1079 if ext == u'png':
1061 format = self._FMT_PNG
1080 format = self._FMT_PNG
1081 if ext == u'gif':
1082 # use PNG format until we understand why GIF is not working
1083 format = self._FMT_PNG
1062 else:
1084 else:
1063 format = ext.lower()
1085 format = ext.lower()
1064 elif isinstance(data, bytes):
1086 elif isinstance(data, bytes):
@@ -1069,11 +1091,15 b' class Image(DisplayObject):'
1069
1091
1070 # failed to detect format, default png
1092 # failed to detect format, default png
1071 if format is None:
1093 if format is None:
1072 format = 'png'
1094 format = self._FMT_PNG
1073
1095
1074 if format.lower() == 'jpg':
1096 if format.lower() == 'jpg':
1075 # jpg->jpeg
1097 # jpg->jpeg
1076 format = self._FMT_JPEG
1098 format = self._FMT_JPEG
1099
1100 if format == self._FMT_GIF:
1101 # use PNG format until we understand why GIF is not working
1102 format = self._FMT_PNG
1077
1103
1078 self.format = format.lower()
1104 self.format = format.lower()
1079 self.embed = embed if embed is not None else (url is None)
1105 self.embed = embed if embed is not None else (url is None)
@@ -1100,9 +1126,9 b' class Image(DisplayObject):'
1100 """load pixel-doubled width and height from image data"""
1126 """load pixel-doubled width and height from image data"""
1101 if not self.embed:
1127 if not self.embed:
1102 return
1128 return
1103 if self.format == 'png':
1129 if self.format == self._FMT_PNG:
1104 w, h = _pngxy(self.data)
1130 w, h = _pngxy(self.data)
1105 elif self.format == 'jpeg':
1131 elif self.format == self._FMT_JPEG:
1106 w, h = _jpegxy(self.data)
1132 w, h = _jpegxy(self.data)
1107 else:
1133 else:
1108 # retina only supports png
1134 # retina only supports png
@@ -1150,11 +1176,15 b' class Image(DisplayObject):'
1150 return self.data
1176 return self.data
1151
1177
1152 def _repr_png_(self):
1178 def _repr_png_(self):
1153 if self.embed and self.format == u'png':
1179 if self.embed and self.format == self._FMT_PNG:
1154 return self._data_and_metadata()
1180 return self._data_and_metadata()
1155
1181
1156 def _repr_jpeg_(self):
1182 def _repr_jpeg_(self):
1157 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
1183 if self.embed and (self.format == self._FMT_JPEG or self.format == u'jpg'):
1184 return self._data_and_metadata()
1185
1186 def _repr_gif_(self):
1187 if self.embed and self.format == self._FMT_PNG:
1158 return self._data_and_metadata()
1188 return self._data_and_metadata()
1159
1189
1160 def _find_ext(self, s):
1190 def _find_ext(self, s):
@@ -1264,6 +1294,9 b' class Video(DisplayObject):'
1264 def _repr_jpeg_(self):
1294 def _repr_jpeg_(self):
1265 # TODO
1295 # TODO
1266 pass
1296 pass
1297 def _repr_gif_(self):
1298 # TODO
1299 pass
1267
1300
1268 def clear_output(wait=False):
1301 def clear_output(wait=False):
1269 """Clear the output of the current cell receiving output.
1302 """Clear the output of the current cell receiving output.
General Comments 0
You need to be logged in to leave comments. Login now