Google Groups Home
Help | Sign in
Precise bitmaps
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Jan G. Korvink  
View profile
 More options Feb 27 2005, 1:48 am
Newsgroups: comp.soft-sys.math.mathematica
From: "Jan G. Korvink" <korv...@t-online.de>
Date: Sun, 27 Feb 2005 06:48:36 +0000 (UTC)
Local: Sun, Feb 27 2005 1:48 am
Subject: Precise bitmaps
Dear All,

I am battling to obtain a "precise" Raster[] of some Graphics[] objects.
There seems to be no straightforward way to do the conversion, so I tried:

Export[ /tmp/myfile, Graphics[myGraphics], ImageSize -> {xmax,ymax}
myBitsPerUnit ]
g = Import[ /tmp/myfile ]

from which I can unpack the Raster[]. Unfortunately the Raster[]
contains extra white space around the object, and no matter what I
select for "myfile" (I have tried all bitmap formats) I get a
consistently bad result. Furthermore, the Export/Import route is pretty
bad, since it is slow for large pictures.

What works is to use Adobe Illustrator as the rendering engine. But this
is not nice!

I do not want to implement a renderer, unless absolutely necessary, and
it seems silly since Mathematica has such an animal.

Can anyone suggest an alternative route?

Thanks in advance, Jan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Luttrell  
View profile
 More options Feb 28 2005, 3:39 am
Newsgroups: comp.soft-sys.math.mathematica
From: "Steve Luttrell" <steve_usenet@_removemefirst_luttrell.org.uk>
Date: Mon, 28 Feb 2005 08:39:06 +0000 (UTC)
Local: Mon, Feb 28 2005 3:39 am
Subject: Re: Precise bitmaps
I have used precisely the same export/import trick as you in order to use
the Mathematica rendering engine to generate bitmaps for use in image
processing applications. My cure to the extra white space problem was to
simply discard those pixels after Importing the bitmap, and to anticipate
this loss of pixels by Exporting a slightly larger bitmap in the first
place.

I have just tried to reproduce what I did using Mathematica 5.1 (for
Windows) and find that the extra whitespace problem seems to have been cured
in that version. However, here is (roughly) what I did to cure the problem
in an earlier version of Mathematica:

Load a package for generating a pretty image.

<< "Graphics`Shapes`"

Generate an image (with a black background so you can see the extra white
space when it is added), export, then import it. Notice that I export 1
pixel more in each dimension than I eventually need.

g = Show[Graphics3D[Torus[], Background -> RGBColor[0, 0, 0]]]
Export["temp.bmp", g, "BMP", ImageSize -> {101, 101}]
g2 = Import["temp.bmp", "BMP"]
Show[g2]

Now have a look at the contents of g2. The first part is the image data, so
I have displayed only its dimensions. This shows which bits have to be
changed if you crop the image. There may be an "official" way of doing this,
but I haven't found it.

Dimensions[g2[[1,1]]]
g2[[1,2]]
g2[[1,3]]
g2[[1,4]]
g2[[2]]
g2[[3]]
g2[[4]]

{101, 101, 3}
{{0, 0}, {101, 101}}
{0, 255}
ColorFunction -> RGBColor
ImageSize -> {101, 101}
PlotRange -> {{0, 100}, {1, 101}}
AspectRatio -> Automatic

Make a copy of g2, and then hack it to crop the image. I am dropping the
last pixel in each row, and also the last row of the image. I am doing this
from memory, so if this prescription doesn't get rid of the white space then
modify it appropriately.

g3 = g2;
g3[[1,1]] = Drop[Map[Drop[#,-1]&,g2[[1,1]]],-1];
g3[[1,2,2]] = g3[[1,2,2]] - {1, 1};
g3[[2]] = g3[[2]] /. {x_, y_} -> {x - 1, y - 1};
g3[[3]] = g3[[3]] /. {{x1_, y1_}, {x2_, y2_}} -> {{x1, y1 - 1}, {x2, y2 -
1}};
Show[g3]

Have a look at the contents of g3.

Dimensions[g3[[1,1]]]
g3[[1,2]]
g3[[1,3]]
g3[[1,4]]
g3[[2]]
g3[[3]]
g3[[4]]

{100, 100, 3}
{{0, 0}, {100, 100}}
{0, 255}
ColorFunction -> RGBColor
ImageSize -> {100, 100}
PlotRange -> {{0, 99}, {1, 100}}
AspectRatio -> Automatic

That should do what you want.

Steve Luttrell

"Jan G. Korvink" <korv...@t-online.de> wrote in message
news:cvrqg4$p55$1@smc.vnet.net...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Precise bitmaps - addendum" by Steve Luttrell
Steve Luttrell  
View profile
 More options Feb 28 2005, 3:45 am
Newsgroups: comp.soft-sys.math.mathematica
From: "Steve Luttrell" <steve_usenet@_removemefirst_luttrell.org.uk>
Date: Mon, 28 Feb 2005 08:45:56 +0000 (UTC)
Local: Mon, Feb 28 2005 3:45 am
Subject: Re: Precise bitmaps - addendum
Further to my previous reply.

1. Mathematica 5.1 has NOT cured the problem.
2. The cropping operation to remove the extra white space is
Drop[Map[Drop[#,-1]&,g2[[1,1]]],1]

Steve Luttrell


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
DrBob  
View profile
 More options Mar 1 2005, 2:13 am
Newsgroups: comp.soft-sys.math.mathematica
From: DrBob <dr...@bigfoot.com>
Date: Tue, 1 Mar 2005 07:13:19 +0000 (UTC)
Local: Tues, Mar 1 2005 2:13 am
Subject: Re: Re: Precise bitmaps - addendum
In version 5 and above, that's the same as:

Rest[Most /@ g2[[1, 1]]]

Bobby

On Mon, 28 Feb 2005 03:27:22 -0500 (EST), Steve Luttrell <steve_usenet@_removemefirst_luttrell.org.uk> wrote:
> Further to my previous reply.

> 1. Mathematica 5.1 has NOT cured the problem.
> 2. The cropping operation to remove the extra white space is
> Drop[Map[Drop[#,-1]&,g2[[1,1]]],1]

> Steve Luttrell

--
Dr...@bigfoot.com
www.eclecticdreams.net

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Precise bitmaps" by Stefano Taschini
Stefano Taschini  
View profile
 More options Mar 4 2005, 5:14 am
Newsgroups: comp.soft-sys.math.mathematica
From: Stefano Taschini <tasch...@gmail.com>
Date: Fri, 4 Mar 2005 10:14:34 +0000 (UTC)
Local: Fri, Mar 4 2005 5:14 am
Subject: Re: Precise bitmaps
Jan,

The quality of the raster engine in Mathematica is anyway rather poor,
and I'd rather use an external tool.

The whole procedure can be automatized. If you have ImageMagick and
GhostScript installed on your Mac, you can use the following function

Options[Rasterize] = {ImageSize -> 288};
Rasterize[g_, opts___?OptionQ] := With[{
         f = OpenTemporary[],
         fullopts = Join[{opts}, Options@Rasterize]},
     Close[f];
     Export[First@f, g, "EPS"];
     Run@StringForm["sh -l -c 'convert -resize `2` eps:`1` bmp:`1`'",
         First@f,
         Replace[Replace[ImageSize, fullopts], {
           n_Integer :> StringForm["`1`x`1`", n],
           {x_, y_} :> StringForm["``x``!", x, y]}]];
     With[{bmp = Import[First@f, "BMP"]},
       DeleteFile[First@f];
       bmp]]

For instance, you can rasterize the thorus of a previous reply,

<< Graphics`Shapes`
g=Graphics3D[Torus[],Background->RGBColor[0,0,0]];
Show[Rasterize[g]]

With the option ImageSize you can set max(width,height)

Show[Rasterize[g, ImageSize -> 100]]

You can also set exactly both width and height

Show[Rasterize[g, ImageSize->{100,200}]]

I tested the Rasterize function with Mathematica 5.1 on Mac Os X 10.3.8,
but should work on any unix system, provided you have ImageMagick and
GhostScript installed.

Ciao,
Stefano


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google