Skip to content

Instantly share code, notes, and snippets.

@mertkahyaoglu
Created February 28, 2017 08:34
Show Gist options
  • Save mertkahyaoglu/436a925c6310ad0921b7cb26a93cf92c to your computer and use it in GitHub Desktop.
Save mertkahyaoglu/436a925c6310ad0921b7cb26a93cf92c to your computer and use it in GitHub Desktop.
import os
from PIL import Image
from psd_tools import PSDImage
tifpath = "tif"
psdpath = "psd"
if not os.path.exists(tifpath):
os.makedirs(tifpath)
if not os.path.exists(psdpath):
os.makedirs(psdpath)
images_path = "images"
def trim_and_save_png(im):
try:
im_trimmed = im.crop(im.getbbox())
im_trimmed.thumbnail((350, 350))
im_trimmed.save(outpath, "PNG")
except Exception, e:
print e
def save_from_tif(path):
im = Image.open(path)
trim_and_save_png(im)
def save_from_psd(path):
psd = PSDImage.load(path)
first_image = psd.layers[0].as_PIL()
trim_and_save_png(first_image)
for root, dirs, files in os.walk(images_path, topdown=False):
for name in files:
file_path = os.path.join(root, name)
file_ext = os.path.splitext(file_path)[1].lower()
if file_ext == ".tif":
print file_path
outpath = os.path.splitext(os.path.join(tifpath, name))[0] + ".png"
if os.path.isfile(outpath):
print "exists: %s" % name
else:
save_from_tif(file_path)
elif file_ext == ".psd":
print file_path
outpath = os.path.splitext(os.path.join(psdpath, name))[0] + ".png"
if os.path.isfile(outpath):
print "exists: %s" % name
else:
save_from_psd(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment