ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 10 Jul 2013 23:25:06 -0500png image within a Graphics objecthttp://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/Hi,
I have an image, let say a png file, which represents the values of a function. The function is continuous but highly non derivable (a good example is [a Julia set](http://en.wikipedia.org/wiki/Julia_set) and there is no way to obtain a clean picture directly within Sage or matplotlib.
Now that I have this picture, I would like to be able to add axes, lines or circles. Is it possible to do it in Sage ? with matplotlib ?
VincentWed, 03 Apr 2013 19:02:44 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/Answer by vdelecroix for <p>Hi,</p>
<p>I have an image, let say a png file, which represents the values of a function. The function is continuous but highly non derivable (a good example is <a href="http://en.wikipedia.org/wiki/Julia_set">a Julia set</a> and there is no way to obtain a clean picture directly within Sage or matplotlib.</p>
<p>Now that I have this picture, I would like to be able to add axes, lines or circles. Is it possible to do it in Sage ? with matplotlib ?</p>
<p>Vincent</p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?answer=14730#post-id-14730I am very sorry to edit an answer for my own question but I finally ended with a solution. I confess that it is not completely satisfactory. It makes use of the precious post [suppress border](http://ask.sagemath.org/question/1994/mystery-white-border-around-graphics-object) and the library [PIL](http://www.pythonware.com/products/pil/) which is included in Sage.
First of all, I do have a graphic which is here of size 600x600 which is the Mandelbrot set in the range xmin=-2, xmax=1, ymin=-1.5, ymax=1.5.
![image description](/upfiles/13650704135610573.png)
Let us do some import
sage: from PIL import Image
sage: from matplotlib.backends.backend_agg import FigureCanvasAgg
sage: import matplotlib.pyplot as plt
The graphics that I want to add are the axes the precise plot of the main cardioid and the circle that corresponds to the period 2 attracting cycle.
sage: P1 = line2d([(1-(exp(CC(0,t*2.*RR(pi)))+1)**2)/4 for t in xsrange(-.5,.5,.002)])
sage: P1 += point2d((0,0), color='green')
sage: P2 = circle((-1,0),1/4)
sage: P2 += point2d((-1,0), color='green')
sage: G = P1 + P2
sage: G.set_axes_range(-2,1,-1.5,1.5)
Now I use the trick in the post mentioned above and save the picture
sage: pm = G.matplotlib(axes=True, axes_pad=0)
sage: pm.subplots_adjust(left=0,right=1,top=1,bottom=0)
sage: pm.set_canvas(FigureCanvasAgg(pm))
sage: pm.set_size_inches(6,6) # with 80 dpi, this gives a 600x600 picture
sage: pm.savefig('/tmp/a.png', bbox_inches=0)
The result is seen below and one can check that it has the expected dimension 600x600.
![image description](/upfiles/1365071110663862.png)
Then, using PIL, I can merge the two images
sage: mandel = Image.open('/tmp/mandel.png')
sage: axes = Image.open('/tmp/a.png')
sage: mask = Image.new('L', (600,600), 125)
sage: mandel.paste(axes, mask)
sage: mandel.save('/tmp/mandel_with_axes.png')
![image description](/upfiles/1365071092253301.png)Thu, 04 Apr 2013 00:28:45 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?answer=14730#post-id-14730Comment by kcrisman for <p>I am very sorry to edit an answer for my own question but I finally ended with a solution. I confess that it is not completely satisfactory. It makes use of the precious post <a href="http://ask.sagemath.org/question/1994/mystery-white-border-around-graphics-object">suppress border</a> and the library <a href="http://www.pythonware.com/products/pil/">PIL</a> which is included in Sage.</p>
<p>First of all, I do have a graphic which is here of size 600x600 which is the Mandelbrot set in the range xmin=-2, xmax=1, ymin=-1.5, ymax=1.5.</p>
<p><img alt="image description" src="/upfiles/13650704135610573.png"/></p>
<p>Let us do some import</p>
<pre><code>sage: from PIL import Image
sage: from matplotlib.backends.backend_agg import FigureCanvasAgg
sage: import matplotlib.pyplot as plt
</code></pre>
<p>The graphics that I want to add are the axes the precise plot of the main cardioid and the circle that corresponds to the period 2 attracting cycle. </p>
<pre><code>sage: P1 = line2d([(1-(exp(CC(0,t*2.*RR(pi)))+1)**2)/4 for t in xsrange(-.5,.5,.002)])
sage: P1 += point2d((0,0), color='green')
sage: P2 = circle((-1,0),1/4)
sage: P2 += point2d((-1,0), color='green')
sage: G = P1 + P2
sage: G.set_axes_range(-2,1,-1.5,1.5)
</code></pre>
<p>Now I use the trick in the post mentioned above and save the picture</p>
<pre><code>sage: pm = G.matplotlib(axes=True, axes_pad=0)
sage: pm.subplots_adjust(left=0,right=1,top=1,bottom=0)
sage: pm.set_canvas(FigureCanvasAgg(pm))
sage: pm.set_size_inches(6,6) # with 80 dpi, this gives a 600x600 picture
sage: pm.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
<p>The result is seen below and one can check that it has the expected dimension 600x600.</p>
<p><img alt="image description" src="/upfiles/1365071110663862.png"/></p>
<p>Then, using PIL, I can merge the two images</p>
<pre><code>sage: mandel = Image.open('/tmp/mandel.png')
sage: axes = Image.open('/tmp/a.png')
sage: mask = Image.new('L', (600,600), 125)
sage: mandel.paste(axes, mask)
sage: mandel.save('/tmp/mandel_with_axes.png')
</code></pre>
<p><img alt="image description" src="/upfiles/1365071092253301.png"/></p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17943#post-id-17943In this case, these are well-known properties of the Mandelbrot set. Note that he saves it with exactly 600x600 - the size of his original png.Thu, 04 Apr 2013 03:23:37 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17943#post-id-17943Comment by ppurka for <p>I am very sorry to edit an answer for my own question but I finally ended with a solution. I confess that it is not completely satisfactory. It makes use of the precious post <a href="http://ask.sagemath.org/question/1994/mystery-white-border-around-graphics-object">suppress border</a> and the library <a href="http://www.pythonware.com/products/pil/">PIL</a> which is included in Sage.</p>
<p>First of all, I do have a graphic which is here of size 600x600 which is the Mandelbrot set in the range xmin=-2, xmax=1, ymin=-1.5, ymax=1.5.</p>
<p><img alt="image description" src="/upfiles/13650704135610573.png"/></p>
<p>Let us do some import</p>
<pre><code>sage: from PIL import Image
sage: from matplotlib.backends.backend_agg import FigureCanvasAgg
sage: import matplotlib.pyplot as plt
</code></pre>
<p>The graphics that I want to add are the axes the precise plot of the main cardioid and the circle that corresponds to the period 2 attracting cycle. </p>
<pre><code>sage: P1 = line2d([(1-(exp(CC(0,t*2.*RR(pi)))+1)**2)/4 for t in xsrange(-.5,.5,.002)])
sage: P1 += point2d((0,0), color='green')
sage: P2 = circle((-1,0),1/4)
sage: P2 += point2d((-1,0), color='green')
sage: G = P1 + P2
sage: G.set_axes_range(-2,1,-1.5,1.5)
</code></pre>
<p>Now I use the trick in the post mentioned above and save the picture</p>
<pre><code>sage: pm = G.matplotlib(axes=True, axes_pad=0)
sage: pm.subplots_adjust(left=0,right=1,top=1,bottom=0)
sage: pm.set_canvas(FigureCanvasAgg(pm))
sage: pm.set_size_inches(6,6) # with 80 dpi, this gives a 600x600 picture
sage: pm.savefig('/tmp/a.png', bbox_inches=0)
</code></pre>
<p>The result is seen below and one can check that it has the expected dimension 600x600.</p>
<p><img alt="image description" src="/upfiles/1365071110663862.png"/></p>
<p>Then, using PIL, I can merge the two images</p>
<pre><code>sage: mandel = Image.open('/tmp/mandel.png')
sage: axes = Image.open('/tmp/a.png')
sage: mask = Image.new('L', (600,600), 125)
sage: mandel.paste(axes, mask)
sage: mandel.save('/tmp/mandel_with_axes.png')
</code></pre>
<p><img alt="image description" src="/upfiles/1365071092253301.png"/></p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17946#post-id-17946Very nice. How do you determine the origin `(0,0)` of the image?
I see that you have xmin, xmax, etc. But what if you didn't have this information? And how do you know xmin, xmax, etc?Thu, 04 Apr 2013 02:01:48 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17946#post-id-17946Answer by Jason Grout for <p>Hi,</p>
<p>I have an image, let say a png file, which represents the values of a function. The function is continuous but highly non derivable (a good example is <a href="http://en.wikipedia.org/wiki/Julia_set">a Julia set</a> and there is no way to obtain a clean picture directly within Sage or matplotlib.</p>
<p>Now that I have this picture, I would like to be able to add axes, lines or circles. Is it possible to do it in Sage ? with matplotlib ?</p>
<p>Vincent</p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?answer=14733#post-id-14733I wonder if you could use matplotlib's imshow to do this in an easier way: http://matplotlib.org/users/image_tutorial.htmlThu, 04 Apr 2013 09:23:38 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?answer=14733#post-id-14733Comment by vdelecroix for <p>I wonder if you could use matplotlib's imshow to do this in an easier way: <a href="http://matplotlib.org/users/image_tutorial.html">http://matplotlib.org/users/image_tut...</a></p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17939#post-id-17939I don't see how... I tried but did not suceed.Thu, 04 Apr 2013 10:31:35 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17939#post-id-17939Answer by Wheatley for <p>Hi,</p>
<p>I have an image, let say a png file, which represents the values of a function. The function is continuous but highly non derivable (a good example is <a href="http://en.wikipedia.org/wiki/Julia_set">a Julia set</a> and there is no way to obtain a clean picture directly within Sage or matplotlib.</p>
<p>Now that I have this picture, I would like to be able to add axes, lines or circles. Is it possible to do it in Sage ? with matplotlib ?</p>
<p>Vincent</p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?answer=15222#post-id-15222You can try a [image processing toolkit](http://www.rasteredge.com/how-to/csharp-imaging/imaging-processing/) for adding axes, lines or circleswhich of cause should support the PNG manipulation.Wed, 10 Jul 2013 18:27:28 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?answer=15222#post-id-15222Comment by vdelecroix for <p>You can try a <a href="http://www.rasteredge.com/how-to/csharp-imaging/imaging-processing/">image processing toolkit</a> for adding axes, lines or circleswhich of cause should support the PNG manipulation.</p>
http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17305#post-id-17305I did not see how to add axes with the library you mentioned. It is more suited to image manipulation (crop, resize, rotate, ...). Could you provide a more precise pointer?Wed, 10 Jul 2013 23:25:06 -0500http://ask.sagemath.org/question/9980/png-image-within-a-graphics-object/?comment=17305#post-id-17305