Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to remove white borders on Graphics[]?

206 views
Skip to first unread message

leh...@gmail.com

unread,
Dec 24, 2008, 6:00:47 AM12/24/08
to
Hello,
I want to generate some raster texture. But Graphics[] returns it with
white borders and I have not found any way to remove them without
loosing quality:

imagescale={Automatic,500,128};

plot=LineIntegralConvolutionPlot[{{Sin[Cos[3 y]+Sin[3 x]],Cos[Sin[3 y]
+Cos[3 x]]},imagescale},{x,-3,3},{y,-3,3},ColorFunction-
>"DarkRainbow",Frame->False,LightingAngle->0]
raster=plot[[1,1,1]];
image=Graphics[raster,ImagePadding->None] (*white borders still
present*)
Graphics[raster,ImagePadding->0] (*white borders still present*)

ImageCrop[image] (*the quality is much less than quality of the
original image*)

Import[Export["C://1.png", image]] (*again, the quality is much less
than the original*)

Anyone knows a way to extract generated texture without loosing
quality?

And as I see there is the obvious bug in the internal work with
graphics in Mathematica, is not it?

sjoerd.c...@gmail.com

unread,
Dec 25, 2008, 3:55:41 AM12/25/08
to
Just the other day, John Fultz posted a tip how to do this in this
thread:
http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread=
/thread/8c5e15ac350c185c/0a18d6836b02807e#0a18d6836b02807e.

Basically, you add Method -> {"ShrinkWrap" -> True} as an option to
your plotcommand and no extra whitespace is added.

Cheers -- Sjoerd

On Dec 24, 1:00 pm, lehi...@gmail.com wrote:
> Hello,
> I want to generate some raster texture. But Graphics[] returns it with
> white borders and I have not found any way to remove them without
> loosing quality:
>
> imagescale={Automatic,500,128};
>
> plot=LineIntegralConvolutionPlot[{{Sin[Cos[3 y]+Sin[3 x]],Cos[Sin[3 y]

> +Cos[3 x]]},imagescale},{x,-3,3},{y,-3,3},ColorFunction->"DarkRainbow",Fr=

leh...@gmail.com

unread,
Dec 26, 2008, 5:40:50 AM12/26/08
to
On 25 =C4=C5=CB, 11:55, "sjoerd.c.devr...@gmail.com"

<sjoerd.c.devr...@gmail.com> wrote:
> Just the other day, John Fultz posted a tip how to do this in this
> thread:http://groups.google.com/group/comp.soft-sys.math.mathematica/brow=
se_....

>
> Basically, you add Method -> {"ShrinkWrap" -> True} as an option to
> your plotcommand and no extra whitespace is added.
>
> Cheers -- Sjoerd

Yes, but Export[] and any other manipulations with the plot still
degrades quality of the image. Try the following:

imagescale = {Automatic, 500, 128};

tex1 = LineIntegralConvolutionPlot[{{Cos[x^2 + y^3], Cos[y^2 + x^3]},
imagescale}, {x, -3, 3}, {y, -3, 3}, ColorFunction -> "Rainbow",
Frame -> False, LightingAngle -> 0,
Method -> {"ShrinkWrap" -> True}];

Style[Row[{tex1, Import[Export["C://1.png", tex1]]}],
ImageSizeMultipliers -> {1, 2}]

Comparison of the two images (original and exported in lossless
format .PNG) clearly shows the degradation of quality. The same result
you may see with any other manipulations with the image in
Mathematica, for example:

Style[Row[{tex1, ImageCrop[tex1]}], ImageSizeMultipliers -> {1, 2}]

Has anyone an idea how to turn of such behaviour or at to find some
workaround?

Sjoerd C. de Vries

unread,
Dec 26, 2008, 5:39:36 AM12/26/08
to
Google Groups keeps on mutilating all the links to other threads that
I post. Hope this one survives:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread=
/thread/8c5e15ac350c185c/0a18d6836b02807e#0a18d6836b02807e

On Dec 25, 10:55 am, "sjoerd.c.devr...@gmail.com"


<sjoerd.c.devr...@gmail.com> wrote:
> Just the other day, John Fultz posted a tip how to do this in this

> thread:http://groups.google.com/group/comp.soft-sys.math.mathematica/brow=
se_....


>
> Basically, you add Method -> {"ShrinkWrap" -> True} as an option to
> your plotcommand and no extra whitespace is added.
>
> Cheers -- Sjoerd
>
> On Dec 24, 1:00 pm, lehi...@gmail.com wrote:> Hello,
> > I want to generate some raster texture. But Graphics[] returns it with
> > white borders and I have not found any way to remove them without
> > loosing quality:
>
> > imagescale={Automatic,500,128};
>

> > plot=LineIntegralConvolutionPlot[{{Sin[Cos[3 y]+Sin[3 x]],Cos[Sin[3 y=
]
> > +Cos[3 x]]},imagescale},{x,-3,3},{y,-3,3},ColorFunction->"DarkRainbow",=

Stoney Ballard

unread,
Dec 27, 2008, 7:04:08 AM12/27/08
to
On Dec 24, 6:00 am, lehi...@gmail.com wrote:
> Hello,
> I want to generate some raster texture. But Graphics[] returns it with
> white borders and I have not found any way to remove them without
> loosing quality:
>
> imagescale={Automatic,500,128};
>
> plot=LineIntegralConvolutionPlot[{{Sin[Cos[3 y]+Sin[3 x]],Cos[Sin[3 y]
> +Cos[3 x]]},imagescale},{x,-3,3},{y,-3,3},ColorFunction->"DarkRainbow",Fr=

ame->False,LightingAngle->0]
>
> raster=plot[[1,1,1]];
> image=Graphics[raster,ImagePadding->None] (*white borders still
> present*)
> Graphics[raster,ImagePadding->0] (*white borders still present*)
>
> ImageCrop[image] (*the quality is much less than quality of the
> original image*)
>
> Import[Export["C://1.png", image]] (*again, the quality is much less
> than the original*)
>
> Anyone knows a way to extract generated texture without loosing
> quality?
>
> And as I see there is the obvious bug in the internal work with
> graphics in Mathematica, is not it?

I blogged about a similar problem here:
<http://tinyurl.com/a7rtl2>
and revised it for V7 here:
<http://tinyurl.com/7kdo4s>

In V7, using Image seems to preserve the pixels. In V6, there are
options to ArrayPlot that produce pixel-perfect images, in particular,
PixelConstrained and ImageSize.


leh...@gmail.com

unread,
Dec 29, 2008, 6:35:29 AM12/29/08
to
On 27 =C4=C5=CB, 15:04, Stoney Ballard <ston...@gmail.com> wrote:
> On Dec 24, 6:00 am, lehi...@gmail.com wrote:> Hello,
> I blogged about a similar problem here:
> <http://tinyurl.com/a7rtl2>
> and revised it for V7 here:
> <http://tinyurl.com/7kdo4s>
>
> In V7, using Image seems to preserve the pixels. In V6, there are
> options to ArrayPlot that produce pixel-perfect images, in particular,
> PixelConstrained and ImageSize.

Thank you very much! My problem is solved!

The following demonstrates this:

imagescale = {Automatic, 500, 128};
tex1 = LineIntegralConvolutionPlot[{{Cos[x^2 + y^3], Cos[y^2 + x^3]},
imagescale}, {x, -3, 3}, {y, -3, 3}, ColorFunction -> "Rainbow",
Frame -> False, LightingAngle -> 0,

Method -> {"ShrinkWrap" -> True}];


Style[Row[{tex1, ImageCrop[tex1]}], ImageSizeMultipliers -> {1, 2}]

(*loosing quality*)
tex1Normalized = Image[tex1[[1, 1, 1, 1]], ColorSpace -> "RGB"];
Style[Row[{tex1, tex1Normalized}], ImageSizeMultipliers -> {1, 2}]
(*quality is retained*)
Style[Row[{tex1, Import[Export["C://1.png", tex1Normalized]]}],
ImageSizeMultipliers -> {1, 2}] (*quality is retained with Export[]*)

Thank you once more!

leh...@gmail.com

unread,
Dec 29, 2008, 6:40:49 AM12/29/08
to
On 27 =C4=C5=CB, 15:04, Stoney Ballard <ston...@gmail.com> wrote:
> I blogged about a similar problem here:
> <http://tinyurl.com/a7rtl2>
> and revised it for V7 here:
> <http://tinyurl.com/7kdo4s>
>
> In V7, using Image seems to preserve the pixels. In V6, there are
> options to ArrayPlot that produce pixel-perfect images, in particular,
> PixelConstrained and ImageSize.

Now I understand the reason for loosing quality. It is behavour of
default option ImageSize -> Automatic in Graphics[]. The "Automatic"
value of this option makes all large graphic obgects to be 360 pixels
width. Under "More information" field on the help page for Graphics
one may read : "Graphics[] gives an empty graphic with the default
image size." We can find that "default image size" is 360 pixels
width:
In[1] := Graphics[] // ImageDimensions
Out[1] = {360, 359}
In my case the generated texture must be 500 pixxels width but is
automatically converted to 360 pixels width with loosing quality :

In[2] := imagescale = {Automatic, 500, 128};
texture = LineIntegralConvolutionPlot[{{Cos[x^2 + y^3], Cos[y^2 +


x^3]}, imagescale}, {x, -3, 3}, {y, -3, 3}, ColorFunction ->
"Rainbow", Frame -> False, LightingAngle -> 0, Method -> {"ShrinkWrap"
-> True}];

texture // ImageDimensions
Out[4] = {360, 361}

I say "loosing quality" because we can not get back all the points
those are computed by LineIntegralConvolutionPlot[] by resizing the
image, magnification, Export[] etc. Thus I think that in this case
Mathematica is doing somethink that may be called as "fraud". All of
the lost points are still kept in the InputForm of generated Graphics
[] but any converting operation on this results in loosing all
additional information needed for recovering points. And the way for
recovering them is difficult and not obvious. Exporting all generated
pionts in some lossless image format is one of every - day tasks for
anyone who generates raster images in Mathematica. But in
Documentation I have not found any guidance for this.
The way to correct this behavour is to specify the precise ImageSize :

In[5] := fix4Graphics = Append[#, ImageSize -> Reverse[Take[Dimensions
[Part[#, Sequence @@ Position[#, _Raster][[1]], 1]], 2]]] &;
fix4Graphics@texture // ImageDimensions
Out[6] = {500, 500}

I think that this behavour should be default for ImageSize. And if the
size is too big it should be automatically magnified to fit the screen
area as it already happens in the case of Image[] :

In[5] := Raster2Image = Image[Part[#, Sequence @@ Position[#, _Raster]
[[1]], 1], ColorSpace -> "RGB"] &;
Raster2Image@texture // ImageDimensions
Raster2Image@texture
Out[6] = {500, 500}
Out[7] =

Resizing of the notebook results in resizing of the generated image
without loosing quality.

Thanks Stoney Ballard for reviews.

AES

unread,
Dec 30, 2008, 5:50:54 AM12/30/08
to
In article <gjad01$p3t$1...@smc.vnet.net>, leh...@gmail.com wrote:

> I say "loosing quality" because we can not get back all the points
> those are computed by LineIntegralConvolutionPlot[] by resizing the
> image, magnification, Export[] etc. Thus I think that in this case
> Mathematica is doing somethink that may be called as "fraud". All of
> the lost points are still kept in the InputForm of generated Graphics
> [] but any converting operation on this results in loosing all
> additional information needed for recovering points. And the way for
> recovering them is difficult and not obvious. Exporting all generated
> pionts in some lossless image format is one of every - day tasks for
> anyone who generates raster images in Mathematica. But in
> Documentation I have not found any guidance for this.

> The way to correct this behavour is to specify the precise . . .

I believe this one single paragraph amounts to a telling illustration
of just how far Mathematica has gone down several wrong paths,
and of the hassles and dysfunctionality that this can create for
"ordinary users" of Mathematica.

0 new messages