EPS format images are common used in latex, however, images we often create are png. In this tutorial, we will introduce how to convert png images to eps format using python pillow.
Preliminary
To convert png images to eps, we should be sure the mode of png image is L, LAB, RGB and CMYK. Otherwise, we will get an ValueError: image mode is not supported. To fix this error, you can read this turorial.
Fix Pillow Convert Images to EPS ValueError: image mode is not supported
Import pillow library
from PIL import Image
Check the image mode of png image
We should check the image mode of png and make it can be converted to eps.
image_png = 'logo.png' im = Image.open(image_png) print(im.mode)
The resout is:RGBA, which can not be read by pillow.
Convert png image mode
We should convert current png mode to L, LAB, RGB or CMYK.
For example, if we convert RGBA to RGB, we can do like this:
fig = im.convert('RGB')
Convert png to eps
After converting png image mode, we can convert current png image to eps by pillow.
fig.save('logo-RGB.eps', lossless = True)
We also can convert RGBA to other mode, here is all effect. Because RGBA can not be converted to LAB, so we do not show it.
From the effect,we can find mode L will change the color of original png files.
Compare file size of different mode eps file
Here is result:
From the result, we can find converting png to eps with CMYK mode will get the biggest file size and model L is smallest.