python:imagesprocess
Table of Contents
Image Processing
Pillow
Image Module
ImageDraw Module
Psd tools
source: https://github.com/kmike/psd-tools refer API: http://searchcode.babies.vn:8080/source/xref/python2.7/site-packages/psd_tools/user_api/psd_image.py
Install
- windows:
SET VS90COMNTOOLS=%VS100COMNTOOLS% easy_install.exe psd-tools easy_install.exe packbits
- linux:
pip install psd-tools pip install packbits
Using Psd tools
Read psd file and get header, layers(contain layers and groups)
- PSDImage.load return psd_tools.user_api.psd_image.PSDImage object
- psd_tools.user_api.psd_image.PSDImage contain list of objects psd_tools.Group and psd_tools.Layer
Basic Examples:
- Read header and layers:
from psd_tools import PSDImage psd = PSDImage.load('1203.psd') print psd.header print psd.layers
output:
PsdHeader(number_of_channels=4, height=1186, width=1181, depth=8, color_mode=CMYK) [<psd_tools.Layer: u'COMPANY copy 2', size=528x35, x=286, y=666>, <psd_tools.Layer: u'COMPANY', size=738x88, x=108, y=742>, <psd_tools.Layer: u'Layer 2', size=535x388, u'Layer 3', size=1181x1186, x=0, y=0>]
- Save all layers and groups:
from psd_tools import PSDImage from psd_tools.user_api.psd_image import PSDImage as UserImage psd = PSDImage.load('1203.psd') for layer in psd.layers: pil_img = layer.as_PIL() pil_img.save(layer.name + '.png')
working with group(psd_tools.Group)
working with layer(psd_tools.Layer)
export psd file to png
psd.as_PIL() return object PIL.Image.Image
- Simple export
from psd_tools import PSDImage from psd_tools.user_api.psd_image import PSDImage as UserImage psd = PSDImage.load('1203.psd') merge_img = psd.as_PIL() merge_img.save('1203.png')
- script export psd to png file:
from psd_tools import PSDImage import sys, os import re from os import path def convertpsd2png(psdfilename, outpath = ''): psd = PSDImage.load(psdfilename) pil_img = psd.as_PIL() pngfilename = re.sub('.psd', '.png', path.basename(psdfilename)) pngfilename = path.join(outpath, pngfilename) if outpath != '' and not path.exists(outpath): os.makedirs(outpath) print pngfilename pil_img.save(pngfilename) nargs = len(sys.argv) outpath = '' if nargs <= 1: print 'please input psd file' exit() elif nargs >=2: outpath = sys.argv[2] psdfilename = sys.argv[1] if path.exists(psdfilename): ext = path.splitext(psdfilename)[1] if ext == '.psd': convertpsd2png(psdfilename, outpath) else: print 'the extension of file is incorrect' else: print 'file is not exists:', psdfilename
script to run for multiple files:
for /F "delims=\r\n" %x in ('dir /B indir') do psd2png.py indir\%x outdir
python/imagesprocess.txt · Last modified: 2022/10/29 16:15 by 127.0.0.1