##// 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 23 from IPython.testing.skipdoctest import skip_doctest
24 24
25 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 27 'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject',
28 28 'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'GeoJSON', 'Javascript',
29 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 96 * application/javascript
97 97 * image/png
98 98 * image/jpeg
99 * image/gif
99 100 * image/svg+xml
100 101
101 102 Parameters
@@ -256,6 +257,7 b' def display(*objs, include=None, exclude=None, metadata=None, transient=None, di'
256 257 - `_repr_json_`: return a JSONable dict
257 258 - `_repr_jpeg_`: return raw JPEG data
258 259 - `_repr_png_`: return raw PNG data
260 - `_repr_gif_`: return raw PNG data
259 261 - `_repr_svg_`: return raw SVG data as a string
260 262 - `_repr_latex_`: return LaTeX commands in a string surrounded by "$".
261 263 - `_repr_mimebundle_`: return a full mimebundle containing the mapping
@@ -495,6 +497,22 b' def display_jpeg(*objs, **kwargs):'
495 497 """
496 498 _display_mimetype('image/jpeg', objs, **kwargs)
497 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)
515
498 516
499 517 def display_latex(*objs, **kwargs):
500 518 """Display the LaTeX representation of an object.
@@ -927,7 +945,7 b' class Javascript(TextDisplayObject):'
927 945 raise TypeError('expected sequence, got: %r' % css)
928 946 self.lib = lib
929 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 950 def _repr_javascript_(self):
933 951 r = ''
@@ -974,12 +992,13 b' class Image(DisplayObject):'
974 992 _read_flags = 'rb'
975 993 _FMT_JPEG = u'jpeg'
976 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 998 def __init__(self, data=None, url=None, filename=None, format=None,
980 999 embed=None, width=None, height=None, retina=False,
981 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 1003 When this object is returned by an input cell or passed to the
985 1004 display function, it will result in the image being displayed
@@ -997,7 +1016,7 b' class Image(DisplayObject):'
997 1016 Path to a local file to load the data from.
998 1017 Images from a file are always embedded.
999 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 1020 for format will be inferred from the filename extension.
1002 1021 embed : bool
1003 1022 Should the image data be embedded using a data URI (True) or be
@@ -1059,6 +1078,9 b' class Image(DisplayObject):'
1059 1078 format = self._FMT_JPEG
1060 1079 if ext == u'png':
1061 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 1084 else:
1063 1085 format = ext.lower()
1064 1086 elif isinstance(data, bytes):
@@ -1069,12 +1091,16 b' class Image(DisplayObject):'
1069 1091
1070 1092 # failed to detect format, default png
1071 1093 if format is None:
1072 format = 'png'
1094 format = self._FMT_PNG
1073 1095
1074 1096 if format.lower() == 'jpg':
1075 1097 # jpg->jpeg
1076 1098 format = self._FMT_JPEG
1077 1099
1100 if format == self._FMT_GIF:
1101 # use PNG format until we understand why GIF is not working
1102 format = self._FMT_PNG
1103
1078 1104 self.format = format.lower()
1079 1105 self.embed = embed if embed is not None else (url is None)
1080 1106
@@ -1100,9 +1126,9 b' class Image(DisplayObject):'
1100 1126 """load pixel-doubled width and height from image data"""
1101 1127 if not self.embed:
1102 1128 return
1103 if self.format == 'png':
1129 if self.format == self._FMT_PNG:
1104 1130 w, h = _pngxy(self.data)
1105 elif self.format == 'jpeg':
1131 elif self.format == self._FMT_JPEG:
1106 1132 w, h = _jpegxy(self.data)
1107 1133 else:
1108 1134 # retina only supports png
@@ -1150,11 +1176,15 b' class Image(DisplayObject):'
1150 1176 return self.data
1151 1177
1152 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 1180 return self._data_and_metadata()
1155 1181
1156 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 1188 return self._data_and_metadata()
1159 1189
1160 1190 def _find_ext(self, s):
@@ -1264,6 +1294,9 b' class Video(DisplayObject):'
1264 1294 def _repr_jpeg_(self):
1265 1295 # TODO
1266 1296 pass
1297 def _repr_gif_(self):
1298 # TODO
1299 pass
1267 1300
1268 1301 def clear_output(wait=False):
1269 1302 """Clear the output of the current cell receiving output.
General Comments 0
You need to be logged in to leave comments. Login now