ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 26 Mar 2020 22:46:55 +0100Scaling imageshttps://ask.sagemath.org/question/50364/scaling-images/ Hi,
I am trying to figure out how to create say an 800 x 600 image that will be able to fit different size sheets of paper. Eventually I would like to expand this algorithm to include all the other screen resolutions. However, when Sage generates an image there is the boarder of white. So I have to go in and crop it out with image editing software. This then changes the pixel dimension. I would like to get a ratio of 1:1 between the m x n image and the image generated. I have figured out this ratio when dpi value is at 100, and it turns out that a dpi value of about 172 will get me really close to creating an image of 800 x 600. However, the actual image is roughly 795 x 596, and if I use 173 it turns out to be 801 x 600.
I am wondering if someone has already created an algorithm to print a 1:1 scale for screen resolution to printed image?Wed, 25 Mar 2020 22:48:17 +0100https://ask.sagemath.org/question/50364/scaling-images/Answer by Sébastien for <p>Hi,</p>
<p>I am trying to figure out how to create say an 800 x 600 image that will be able to fit different size sheets of paper. Eventually I would like to expand this algorithm to include all the other screen resolutions. However, when Sage generates an image there is the boarder of white. So I have to go in and crop it out with image editing software. This then changes the pixel dimension. I would like to get a ratio of 1:1 between the m x n image and the image generated. I have figured out this ratio when dpi value is at 100, and it turns out that a dpi value of about 172 will get me really close to creating an image of 800 x 600. However, the actual image is roughly 795 x 596, and if I use 173 it turns out to be 801 x 600. </p>
<p>I am wondering if someone has already created an algorithm to print a 1:1 scale for screen resolution to printed image?</p>
https://ask.sagemath.org/question/50364/scaling-images/?answer=50367#post-id-50367You may do:
sage: p = plot(sin(x), (x, -2*pi, 2*pi))
sage: p.show(figsize=[8,1])
to change the scaling:
![image description](/upfiles/15852105334286679.png)
The method `save` accepts the same argument:
sage: p.save('image.png', figsize=[8,1])
To do further changes on the figure, you may get the matplotlib object behind the 2d `Graphics` object and use [matplotlib possibilities](https://matplotlib.org/3.2.0/index.html):
sage: figure = p.matplotlib(figsize=[8,6])
sage: figure
<Figure size 800x600 with 1 Axes>
sage: type(figure)
<class 'matplotlib.figure.Figure'>
sage: axes = figure.axes[0]Thu, 26 Mar 2020 09:20:47 +0100https://ask.sagemath.org/question/50364/scaling-images/?answer=50367#post-id-50367Comment by nooniensoong97 for <p>You may do:</p>
<pre><code>sage: p = plot(sin(x), (x, -2*pi, 2*pi))
sage: p.show(figsize=[8,1])
</code></pre>
<p>to change the scaling:</p>
<p><img alt="image description" src="/upfiles/15852105334286679.png"></p>
<p>The method <code>save</code> accepts the same argument:</p>
<pre><code>sage: p.save('image.png', figsize=[8,1])
</code></pre>
<p>To do further changes on the figure, you may get the matplotlib object behind the 2d <code>Graphics</code> object and use <a href="https://matplotlib.org/3.2.0/index.html">matplotlib possibilities</a>:</p>
<pre><code>sage: figure = p.matplotlib(figsize=[8,6])
sage: figure
<Figure size 800x600 with 1 Axes>
sage: type(figure)
<class 'matplotlib.figure.Figure'>
sage: axes = figure.axes[0]
</code></pre>
https://ask.sagemath.org/question/50364/scaling-images/?comment=50384#post-id-50384Thanks for the advise. However, when I tried using figsize that also includes the white border around the image. I am not wanting to include that border. Plus I had to use figsize = [9,7] to get close to 800 x 600, and with using a dpi value of 96. This gave me an image of 795 x 595 after cropping out the white border. I might have to live with this setup since both values are really close to the target values.Thu, 26 Mar 2020 22:46:55 +0100https://ask.sagemath.org/question/50364/scaling-images/?comment=50384#post-id-50384