##// END OF EJS Templates
Issue 2053:...
Jerry Fowler -
Show More
@@ -0,0 +1,38 b''
1 #-----------------------------------------------------------------------------
2 # Copyright (C) 2010-2011 The IPython Development Team.
3 #
4 # Distributed under the terms of the BSD License.
5 #
6 # The full license is in the file COPYING.txt, distributed with this software.
7 #-----------------------------------------------------------------------------
8 import os
9
10 import nose.tools as nt
11
12 from IPython.core import display
13 from IPython.utils import path as ipath
14
15 def test_image_size():
16 """Simple test for display.Image(args, width=x,height=y)"""
17 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
18 img = display.Image(url=thisurl, width=200, height=200)
19 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
20 img = display.Image(url=thisurl, width=200)
21 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
22 img = display.Image(url=thisurl)
23 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
24
25 def test_image_filename_defaults():
26 '''test format constraint, and validity of jpeg and png'''
27 tpath = ipath.get_ipython_package_dir()
28 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'),
29 embed=True)
30 nt.assert_raises(ValueError, display.Image)
31 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
32 imgfile = os.path.join(tpath, 'frontend/html/notebook/static/ipynblogo.png')
33 img = display.Image(filename=imgfile)
34 nt.assert_equal('png', img.format)
35 nt.assert_is_not_none(img._repr_png_())
36 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
37 nt.assert_equal('jpeg', img.format)
38 nt.assert_is_none(img._repr_jpeg_())
@@ -421,8 +421,11 b' class Javascript(DisplayObject):'
421 class Image(DisplayObject):
421 class Image(DisplayObject):
422
422
423 _read_flags = 'rb'
423 _read_flags = 'rb'
424 _FMT_JPEG = u'jpeg'
425 _FMT_PNG = u'png'
426 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
424
427
425 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None):
428 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None, width=None, height=None):
426 """Create a display an PNG/JPEG image given raw data.
429 """Create a display an PNG/JPEG image given raw data.
427
430
428 When this object is returned by an expression or passed to the
431 When this object is returned by an expression or passed to the
@@ -449,6 +452,10 b' class Image(DisplayObject):'
449 default value is `False`.
452 default value is `False`.
450
453
451 Note that QtConsole is not able to display images if `embed` is set to `False`
454 Note that QtConsole is not able to display images if `embed` is set to `False`
455 width : int
456 Width to which to constrain the image in html
457 height : int
458 Height to which to constrain the image in html
452
459
453 Examples
460 Examples
454 --------
461 --------
@@ -464,17 +471,27 b' class Image(DisplayObject):'
464 ext = self._find_ext(filename)
471 ext = self._find_ext(filename)
465 elif url is not None:
472 elif url is not None:
466 ext = self._find_ext(url)
473 ext = self._find_ext(url)
474 elif data is None:
475 raise ValueError("No image data found. Expecting filename, url, or data.")
467 elif data.startswith('http'):
476 elif data.startswith('http'):
468 ext = self._find_ext(data)
477 ext = self._find_ext(data)
469 else:
478 else:
470 ext = None
479 ext = None
480
471 if ext is not None:
481 if ext is not None:
482 format = ext.lower()
472 if ext == u'jpg' or ext == u'jpeg':
483 if ext == u'jpg' or ext == u'jpeg':
473 format = u'jpeg'
484 format = self._FMT_JPEG
474 if ext == u'png':
485 if ext == u'png':
475 format = u'png'
486 format = self._FMT_PNG
487
476 self.format = unicode(format).lower()
488 self.format = unicode(format).lower()
477 self.embed = embed if embed is not None else (url is None)
489 self.embed = embed if embed is not None else (url is None)
490
491 if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
492 raise ValueError("Cannot embed the '%s' image format" % (self.format))
493 self.width = width
494 self.height = height
478 super(Image, self).__init__(data=data, url=url, filename=filename)
495 super(Image, self).__init__(data=data, url=url, filename=filename)
479
496
480 def reload(self):
497 def reload(self):
@@ -484,7 +501,12 b' class Image(DisplayObject):'
484
501
485 def _repr_html_(self):
502 def _repr_html_(self):
486 if not self.embed:
503 if not self.embed:
487 return u'<img src="%s" />' % self.url
504 width = height = ''
505 if self.width:
506 width = ' width="%d"' % self.width
507 if self.height:
508 height = ' height="%d"' % self.height
509 return u'<img src="%s"%s%s/>' % (self.url, width, height)
488
510
489 def _repr_png_(self):
511 def _repr_png_(self):
490 if self.embed and self.format == u'png':
512 if self.embed and self.format == u'png':
General Comments 0
You need to be logged in to leave comments. Login now