[EDIT : The image should stay within Mathematica so I can not use Export["file.svg", expression] as Import does not support svg files]

Consider the expression $1+x^2$. One can convert this expression into an image using

ImportByteArray@ExportByteArray[1 + x^2, "PNG"]


ImportString@ExportString[1 + x^2, "PNG"]

However, the image quality is bad (pixeled). Changing "PNG" to "EPS" leads to a full black image (or white depending on the stylesheet) and changing "PNG" to "PDF" also leads to an image with bad quality (pixeled).

My solution was this rather complicated code that gives a nice image

Style[Text[1 + x^2], FontSize -> 100] // Graphics // Image // 
ExportByteArray[#, "PDF"] & // ImportByteArray // First // 
Image[#, ImageSize -> 50] &

(ImageCrop in the above code could also be useful)

But that code looks like it depends a lot on the size of the expression. For example changing 1+x^2 with Nest[Integrate[#, x] &, x*Log[x]/(x^2 + 4), 2] shows the wrong output image when compared with the actual output.

(Pane could be useful here but it required some fiddling around)

I suppose that a solution for this is to clip the image around the expression. How could one do that ?

Are there other ways to obtain nice images from an expression ?

The two test expressions:

  • 1 + x^2
  • Nest[Integrate[#, x] &, x*Log[x]/(x^2 + 4), 2]



  x^2 + 1 // ExportByteArray[#, "PDF"] &, {"PDF", 
   "PageGraphics"}] // First

I got a nice image but it is a Graphics object. I did not find a way to turn that into a crisp image with the same size. A solution that requires adjusting image resolution and image size would be too tedious for me.


There is a resource function SVGImport by a Wolfram research member that allows importing svg files using ExternalEvaluate and Python (so it might not work if Python is not configured to work with Mathematica). The resulting object is a Graphics object. The method seems to use PDF as an intermediate so I do not know if there is a benefit from exporting and importing as SVG rather than PDF.

Using Magnify with either the PDF or SVG method gave some nice results.

  • $\begingroup$ What do you want to do with the image? Do you need a bit map or can you target application handle vector graphics (e.g. SVG)? $\endgroup$
    – mikado
    Oct 8, 2022 at 20:16
  • $\begingroup$ @mikado Hi I want the image to stay inside Mathematica which is why I used ExportByteArray rather than Export. I wish I could use SVG with Import or ImportByteArray but it seems Mathematica does not support importing this format. $\endgroup$ Oct 8, 2022 at 20:19
  • $\begingroup$ @mikado there is a resource function SVGImport from a wolfram research member for importing SVG files. $\endgroup$ Oct 18, 2022 at 12:10
  • $\begingroup$ @mikado that said it uses ExternalEvaluate to evaluate to Python so it might not work if Pythonis not set up to work like that. $\endgroup$ Oct 18, 2022 at 13:33

1 Answer 1


You must rasterize the expression with a better resolution. The following creates a tiny picture of the expression

 ExportByteArray[Rasterize[1 + x^2, ImageResolution -> 1000], "PNG"]

enter image description here

From the tiny picture you can not judge the resolution. However, if you enlarge the picture by click and drag, you see that the picture has a good resolution:

enter image description here

  • $\begingroup$ Very nice thank you. I did not know how to change the resolution. For large expressions I had to decrease the resolution like ImportByteArray@ExportByteArray[Rasterize[Nest[Integrate[#, x] &, x*Log[x]/(x^2 + 4), 2], ImageResolution -> 100], "PNG"]. I guess I could try to fit a curve to find the maximal resolution given the LeafCount of an expression. I will accept your answer if I do not see any answer that does not involve choosing the resolution manually (maybe one that uses a vector format). $\endgroup$ Oct 8, 2022 at 20:02
  • $\begingroup$ On my computer (Linux) the image was not tiny so I did not have to click and drag. $\endgroup$ Oct 8, 2022 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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