Show More
@@ -0,0 +1,64 | |||
|
1 | // | |
|
2 | // Test display of images | |
|
3 | // | |
|
4 | // The effect of shape metadata is validated, | |
|
5 | // using Image(retina=True) | |
|
6 | // | |
|
7 | ||
|
8 | ||
|
9 | // 2x2 black square in b64 jpeg and png | |
|
10 | b64_image_data = { | |
|
11 | "image/png" : "b'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAC0lEQVR4nGNgQAYAAA4AAamRc7EA\\nAAAASUVORK5CYII='", | |
|
12 | "image/jpeg" : "b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a\\nHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy\\nMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAACAAIDASIA\\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5/ooo\\noA//2Q=='" | |
|
13 | } | |
|
14 | ||
|
15 | ||
|
16 | casper.notebook_test(function () { | |
|
17 | // this.printLog(); | |
|
18 | this.test_img_shape = function(fmt, retina) { | |
|
19 | this.thenEvaluate(function (b64data, retina) { | |
|
20 | IPython.notebook.get_cell(0).clear_output(); | |
|
21 | var cell = IPython.notebook.get_cell(0); | |
|
22 | cell.set_text([ | |
|
23 | "import base64", | |
|
24 | "from IPython.display import display, Image", | |
|
25 | "data = base64.decodestring(" + b64data + ")", | |
|
26 | "retina = bool(" + retina + ")", | |
|
27 | "display(Image(data, retina=retina))" | |
|
28 | ].join("\n")); | |
|
29 | cell.execute(); | |
|
30 | }, {b64data : b64_image_data[fmt], retina : retina ? 1:0 }); | |
|
31 | ||
|
32 | this.wait_for_output(0); | |
|
33 | ||
|
34 | this.then(function() { | |
|
35 | var img = this.evaluate(function() { | |
|
36 | // get a summary of the image that was just displayed | |
|
37 | var cell = IPython.notebook.get_cell(0); | |
|
38 | var img = $(cell.output_area.element.find("img")[0]); | |
|
39 | return { | |
|
40 | src : img.attr("src"), | |
|
41 | width : img.width(), | |
|
42 | height : img.height(), | |
|
43 | width_attr : img.attr("width"), | |
|
44 | height_attr : img.attr("height") | |
|
45 | }; | |
|
46 | }); | |
|
47 | var prefix = "Image('" + fmt + "', retina=" + retina + ") "; | |
|
48 | this.test.assertType(img, "object", prefix + "img was displayed"); | |
|
49 | this.test.assertEquals(img.src.split(',')[0], "data:" + fmt + ";base64", | |
|
50 | prefix + "data-uri prefix" | |
|
51 | ); | |
|
52 | var sz = retina ? 1 : 2; | |
|
53 | var sz_attr = retina ? "1" : undefined; | |
|
54 | this.test.assertEquals(img.height, sz, prefix + "measured height"); | |
|
55 | this.test.assertEquals(img.width, sz, prefix + "measured width"); | |
|
56 | this.test.assertEquals(img.height_attr, sz_attr, prefix + "height attr"); | |
|
57 | this.test.assertEquals(img.width_attr, sz_attr, prefix + "width attr"); | |
|
58 | }); | |
|
59 | }; | |
|
60 | this.test_img_shape("image/png", false); | |
|
61 | this.test_img_shape("image/png", true); | |
|
62 | this.test_img_shape("image/jpeg", false); | |
|
63 | this.test_img_shape("image/jpeg", true); | |
|
64 | }); |
@@ -613,19 +613,20 var IPython = (function (IPython) { | |||
|
613 | 613 | }); |
|
614 | 614 | }, 250); |
|
615 | 615 | }; |
|
616 | ||
|
617 | ||
|
616 | ||
|
617 | var set_width_height = function (img, md, mime) { | |
|
618 | // set width and height of an img element from metadata | |
|
619 | var height = _get_metadata_key(md, 'height', mime); | |
|
620 | if (height !== undefined) img.attr('height', height); | |
|
621 | var width = _get_metadata_key(md, 'width', mime); | |
|
622 | if (width !== undefined) img.attr('width', width); | |
|
623 | }; | |
|
624 | ||
|
618 | 625 | OutputArea.prototype.append_png = function (png, md, element) { |
|
619 | 626 | var type = 'image/png'; |
|
620 | 627 | var toinsert = this.create_output_subarea(md, "output_png", type); |
|
621 | var img = $("<img/>"); | |
|
622 | img[0].setAttribute('src','data:image/png;base64,'+png); | |
|
623 | if (md['height']) { | |
|
624 | img[0].setAttribute('height', md['height']); | |
|
625 | } | |
|
626 | if (md['width']) { | |
|
627 | img[0].setAttribute('width', md['width']); | |
|
628 | } | |
|
628 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); | |
|
629 | set_width_height(img, md, 'image/png'); | |
|
629 | 630 | this._dblclick_to_reset_size(img); |
|
630 | 631 | toinsert.append(img); |
|
631 | 632 | element.append(toinsert); |
@@ -636,12 +637,7 var IPython = (function (IPython) { | |||
|
636 | 637 | var type = 'image/jpeg'; |
|
637 | 638 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); |
|
638 | 639 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
639 | if (md['height']) { | |
|
640 | img.attr('height', md['height']); | |
|
641 | } | |
|
642 | if (md['width']) { | |
|
643 | img.attr('width', md['width']); | |
|
644 | } | |
|
640 | set_width_height(img, md, 'image/jpeg'); | |
|
645 | 641 | this._dblclick_to_reset_size(img); |
|
646 | 642 | toinsert.append(img); |
|
647 | 643 | element.append(toinsert); |
General Comments 0
You need to be logged in to leave comments.
Login now