1

I'm building a Paint-like app Since I want the freedom to reposition and modify the shape properties later, I am using Tkinter to draw shapes on Canvas instead of PIL Draw or anything else. From other answers, I found how to save a canvas as PNG by 1st creating a postscript file and then converting it to PNG using PIL.

Now the problem is the EPS file has transparent spaces but the PNG file fills those voids with a White background color. I'm not sure where I am going wrong.

Below is the function I used.

def saveImg(event):
    global canvas
    canvas.postscript(file="my_drawing.eps", colormode='color')
    imgNew = Image.open("my_drawing.eps")
    imgNew.convert("RGBA")
    imgNew.thumbnail((2000,2000), Image.ANTIALIAS)
    imgNew.save('testImg.png', quality=90)
halfer
  • 19,824
  • 17
  • 99
  • 186
Ranga RS
  • 142
  • 9
  • Do you have ghostscript installed in your system? BTW, which platform are you running? – Paulo Scardine Oct 18 '17 at 12:57
  • @PauloScardine Yes! I have ghostscript installed. I'm running python 3.5.2 on Mac OS – Ranga RS Oct 18 '17 at 12:59
  • You could do the conversion on the command line with GhostScript, like this: `gs -dSAFER -dNOPAUSE -dBATCH -r150 -sDEVICE=pngalpha -sOutputFile=testImg.png my_drawing.eps`. The `-r150` sets the resolution to 150 DPI, higher resolutions take longer to render. – PM 2Ring Oct 18 '17 at 13:32
  • @PM2Ring wow! It works perfectly. Is it possible to set the width and height of the output file? Since eps is size independent, the output resolution seems to be random. I checked the documents but I can find only paper sizes. Is it possible to provide custom size? – Ranga RS Oct 18 '17 at 14:14
  • Updated the answer with a suggestion about how to get a specific width/height. – Paulo Scardine Oct 18 '17 at 16:30
  • Ranga, the eps files have a `%%BoundingBox:` statement right at the beginning. Those are interpreted as picture size in pt; 72pt = 1 inch. There is also an `-sDevice=bbox` that can get you the bounding box if the statement is missing. – Anderas Sep 19 '18 at 08:53

1 Answers1

2

Looks like transparency is not supported. From the docs:

The EPS driver can read EPS images in L, LAB, RGB and CMYK mode, but Ghostscript may convert the images to RGB mode rather than leaving them in the original color space.

When you load in RGB (instead of RGBA) the alpha channel information is discarded and converting it to RGBA later will not recover it.

Your best shot is porting it to more recent toolkits like cairo or QT or converting the file using GhostScript directly as suggested by PM2Ring.

For the GS approach in order to set the width and height of the output file you must use the -rN switch where N is the resolution in PPI (pixels per inch). You must do the math in order to get target resolution from the EPS bounding box and the desired output size.

Or you can render to a fixed resolution first, lets say, 100 PPI, see the width you got and do the math in order to get the correct resolution. For example, if rendering with -r100 gives you a file 500 pixels wide but you want it to be 1024:

desired_resolution = initial_resolution * desired_width // initial_width

In order to get a file 1024 pixels wide:

>>> 100 * 1024 // 500
204

So you must render the EPS again using -r204.

Edit 1:

I got the solution from this Question We can set custom width and height using -gNNNNxMMMM but the dpi value crops only a small area. I tried with the usual 72dpi and I got a decent output(I'm not sure if it's perfect or not). Now I need to find how to execute this command every time when I run the program and provide the custom image size value. :\

Ranga RS
  • 142
  • 9
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • I tried this but it resulted with same size problem. I compared the W/H ratio of the PIL converted png and GS converted png and what I found was GS reads the width of the image as Height and Height as Width :\ Im not sure why that happens, because even PIL uses GS to render png but there is no issue with the width and height. (PIL.png)W/H = H/W(GS.png) – Ranga RS Oct 18 '17 at 18:37