Search code examples
pythonpython-3.xwindowsturtle-graphicspython-turtle

How to convert an .eps file into a .png in Python 3.6


With Python turtle I'm trying to save the canvas as a png. I've researched it and found a way to save it as an eps file without any modules, but I'm finding it hard to convert the eps file into a png.

Is there a way to convert eps to png without downloading another module? If not can someone tell me more about ImageMagick, because I have looked at it, but I'm confused how to use it? I've also seen it being linked to linux and is it outdated?

If not converting eps to png, is there a even simpler way to save the canvas as a png?

Btw I have seen this, but I don't understand it :/ How to convert a .eps file to a high quality 1024x1024 .jpg?


Solution

  • From the link you show, there is this Imagemagick command:

    convert -density 300 image.eps -resize 1024x1024 image.jpg
    

    Most EPS files are vector images. They have no physical size in pixels, since it a vector drawing with commands that describe how to draw each object. It is not a raster image containing pixels and does not have any particular pixel set of dimensions.

    So with vector files, you set the printing density to tell Imagemagick (which passes it off to Ghostscript to do the rasterizing work) to convert the vector data to raster data and then save it as a raster format output image. Nominal density is 72 dpi (sometimes 92 or 96). So if you use -density 288 with the following command:

    convert -density 288 image.eps image.png
    

    It would result in an image that is 4 times larger in each dimension than if you just did

    convert image.eps image.png
    

    which for default dpi of 72 would be the same as

    convert -density 72 image.eps image.png
    

    Note that 72*4=288.

    Now you have a large high quality raster png, especially if the eps file was line drawing with thin lines like blue-prints.

    However if that is too large and you want to reduce it back to its nominal size by 1/4, you could do (note 1/4 = 25%)

    convert -density 288 image.eps -resize 25% image.png
    

    This process is sometimes called supersampling and would produce a better looking result than just doing

    convert image.eps image.png
    

    In the original command, they decide to get a high quality raster image and just resize to 1024x1024.

    So you can resize to any size you want after producing a high definition raster image from the EPS vector image.

    The larger the density you use, the higher the quality will be in the PNG, but it will take longer to process. So you have to trade time vs quality and pick the smallest density that produces good enough quality in a reasonable amount of time.

    I do not know if Python Wand supports setting the density or if it supports reading PDF file, which requires Ghostscript. But you can use Python Subprocess module to make a call to an Imagemagick command line. See https://www.imagemagick.org/discourse-server/viewtopic.php?f=4&t=32920