The PythonImagingLibrary (PIL) adds ImageProcessing capabilities to your Python interpreter. This library supports many file formats, and provides powerful ImageProcessing and graphics capabilities.
http://www.pythonware.com/products/pil/
예제코드
GradationEffect -> EvalueGradation.py
SONY DigitalCamera multiburst 연사사진 슬라이스 스크립트
1 #!/usr/local/bin/python2.3
2 import sys,os, unittest
3 import Image
4
5 def usage():
6 print "Usage: ./imgslice.py filename"
7
8 def getBoxes((x,y),num=4):
9 stepx = x/num; stepy = y/num
10 for eachy in range(0,y,stepy):
11 for eachx in range(0,x,stepx):
12 yield (eachx, eachy, eachx+stepx, eachy+stepy)
13
14 def sliceImage(inFileName):
15 prefix,ext = os.path.splitext(inFileName)
16 outDir = os.path.join(os.curdir, prefix)
17 if not os.path.isdir(outDir):
18 os.makedirs(outDir)
19 try:
20 im = Image.open(inFileName)
21 except IOError:
22 print "no exist filename"
23 usage()
24 os._exit(1)
25
26 for i,box in enumerate(getBoxes(im.size)):
27 outName = os.path.join(outDir, prefix+'_'+str(i+1).zfill(2)+'.'+ext)
28 print 'writing %s...'%outName
29 im.crop(box).save(outName)
30
31 class TestSlicerBox(unittest.TestCase):
32 def test1(self):
33 it = getBoxes((400,40))
34 self.assertEquals((0,0,100,10),it.next())
35 self.assertEquals((100,0,200,10),it.next())
36
37 def main():
38 try:
39 inFileName = sys.argv[1]
40 except IndexError:
41 usage()
42 os._exit(1)
43 sliceImage(inFileName)
RedHatEnterpriseLinux 에서 jpeg를 못읽어 한참을 해맸다. libjpeg 를 따로 설치하고, 경로를 PIL 의 setup.py 에 명시해야 한다. -- yong27 2008-10-29 15:20:29