Created
January 28, 2021 07:43
-
-
Save kitoriaaa/3bd5b8d86f5b77ed354cbd5342dcea23 to your computer and use it in GitHub Desktop.
Convert png file to eps file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script converts a png(jpg) file in the specified directory to an eps file. | |
How to use | |
python3 png2eps.py <png_dir> | |
required Pillow | |
""" | |
from PIL import Image | |
from argparse import ArgumentParser | |
import glob | |
import os | |
def parse_args(): | |
parser = ArgumentParser( | |
description="This script is convert png file to eps file") | |
parser.add_argument('png_dir', help="Target image directory") | |
args = parser.parse_args() | |
return args.png_dir | |
def remove_transparency(im, bg_color=(255, 255, 255)): | |
if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info): | |
alpha = im.convert('RGBA').split()[-1] | |
bg = Image.new("RGBA", im.size, bg_color + (255,)) | |
bg.paste(im, mask=alpha) | |
return bg | |
else: | |
return im | |
def convert(png_dir, eps_dir): | |
""" | |
Converts the .png file to .eps file | |
""" | |
img_list = glob.glob(png_dir+"/*.png") | |
img_list += glob.glob(png_dir+"/*.jpg") | |
os.makedirs(eps_dir, exist_ok=True) | |
for img in img_list: | |
im = Image.open(img) | |
if im.mode in ('RGBA', 'LA'): | |
im = remove_transparency(im) | |
im = im.convert('RGB') | |
name = os.path.splitext(img)[0].split('\\')[-1] | |
im.save(eps_dir + '/' + name + ".eps", lossless=True) | |
def main(): | |
png_dir = parse_args() | |
eps_dir = png_dir + "/output_eps" | |
print(png_dir) | |
print("Converting png to eps...") | |
convert(png_dir, eps_dir) | |
print("Conversion is finishing. \nOutput path is {}".format(eps_dir)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done! Would you call this method a form of steganography? ha!