5
$\begingroup$

When I Export a result of Rasterize, I seem to get a worse-quality image, compared to exporting image directly. Image dimensions are the same in both cases. The differences are that 1) 3D element antialiasing is less aggressive 2) plot axes are thicker after Rasterize. 3) text is more blurry (letters in "min" are merged)

notebook

It looks like Rasterize triggers a lower quality method, any tips how I can recover the higher quality pixels used by default Export?

I need Rasterize to control the number of pixels in the output image. Mathematica 12.3.1.0 on MacOS

Self-contained way to visualize the issue. The second image looks more jaggy, axes are darker and letters are merged together

circle = 
  SliceContourPlot3D[x^2 + y^2, 
   z == 0, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}, Contours -> {1}, 
   ContourStyle -> Dashed, ContourShading -> None, 
   BoundaryStyle -> None, PlotRange -> All];

genPlot[theta_] := (plot1 = 
    Plot3D[3 x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
     MeshFunctions -> {#3 &, Mod[ArcTan[#1, #2], 2 Pi, theta] &}, 
     MeshStyle -> {Automatic, Thick}, 
     Mesh -> {5, {theta, theta + Pi}}, Boxed -> False, Axes -> False, 
     RegionFunction -> Function[{x, y, z}, x^2 + y^2 < 1], 
     PlotPoints -> 25];
   point = {Cos[theta], Sin[theta], 0};
   plot2 = 
    Graphics3D[{Opacity[.5], EdgeForm[None], 
      Polygon[{-point, point, 
        point + {0, 0, 10}, -point + {0, 0, 10}}]}];
   plot3d = 
    Show[plot1, plot2, circle, SphericalRegion -> True, 
     ImageSize -> 600];
   plot2d = 
    Plot[{x^2 + 3 y^2 /. {x -> Sin[theta]*d, y -> Cos[theta]*d}, 
      Labeled[3, Subscript[\[Lambda], max], {Above, Left}], 
      Labeled[1, Subscript[\[Lambda], min], {Above, Left}]}, {d, -1, 
      1}, PlotRange -> {0, 3.5}, 
     Ticks -> {{-1, 1}, {{1, ""}, {3, ""}}}, 
     PlotStyle -> {Automatic, Dashed, Dashed}, AspectRatio -> 1, 
     ImageSize -> 300, 
     Epilog -> {Gray, Thin, Line[{{-1, 0}, {-1, 3.5}}], 
       Line[{{1, 0}, {1, 3.5}}], 
       Circle[{1, 3 Cos[theta]^2 + Sin[theta]^2}, .05]}];
   GraphicsRow[{plot3d, plot2d}]);

img = genPlot[Pi/2];
is = 200;
fn = "rotation-debug.gif";
Export[fn, img, ImageSize -> is];
Image[Import[fn], Magnification -> 5]

Export[fn, 
  Rasterize[img, ImageSize -> is, RasterSize -> is, 
   ImageResolution -> 1000]];
Image[Import[fn], Magnification -> 5]

enter image description here

enter image description here

$\endgroup$
15
  • $\begingroup$ "12.3.1 for Microsoft Windows (64-bit) (June 24, 2021)" produces exactly the same files, so the problem might be (as stated in the reference for Rasterize) Mac-specific: Images generated by Rasterize can vary slightly from one computer system to another, mainly as a result of different fonts and anti-aliasing procedures. Have you tried using RenderingOptions in Show? There is a parameter called "HardwareAntialiasingQuality". Also, changing "3DRenderingEngine" or "3DRenderingMethod" might help. $\endgroup$
    – Domen
    Sep 14 at 13:30
  • $\begingroup$ Are they both like the higher-quality screenshot, or the lower-quality screenshot? $\endgroup$ Sep 14 at 13:38
  • $\begingroup$ Screenshot $\endgroup$
    – Domen
    Sep 14 at 15:13
  • $\begingroup$ I see....it looks like the high-quality antialiasing turns off for you as well -- compare the top-most pixels of the yellow solid $\endgroup$ Sep 14 at 16:41
  • 1
    $\begingroup$ I think the problem is not really in Rasterize, and it looks very similar to this. Check it yourself: remove Opacity[0.5] from plot2 and remove ContourShading -> None from circle. You should see a difference. When using Show with (at least) one transparent graphics, the rendering somehow changes for all the graphics ... $\endgroup$
    – Domen
    Sep 14 at 17:31
2
$\begingroup$

As mentioned by John Fultz on wolfram community, equivalent to default export is to Rasterize at 2x resolution and then downsample using ImageResize or Export:

Export["rotation.webp", Rasterize[#, ImageSize -> is, RasterSize -> 2 is], ImageSize -> is];

Notes (on MacOS):

  • Export[..., ImageSize->is] generates image with is pixels
  • Export[Show[...,ImageSize->is]] generates image with 2*is pibels
  • Rasterize[ImageSize->is, RasterSize->2*is] generates bitmap with 2*is pixels

Therefore, rasterize with 2x number of pixels using RasterSize->2*is and then downsample to 1x pixels using Export[...,ImageSize->is]

$\endgroup$
1
+50
$\begingroup$

Try:

Export["rotation-debug2.gif", Rasterize[img, RasterSize -> 700, ImageResolution -> 1000]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Adding Resolution still gives me subpar-quality anti-aliasing....updated my post with an example where the difference is clear $\endgroup$ Sep 14 at 10:24
  • $\begingroup$ @Yaroslav Bulatov Oh, I see, you're right. $\endgroup$ Sep 14 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.