public
Description: A little web app that remixes Dinosaur Comics.
Homepage: http://definingterms.com/dinoremix/
Clone URL: git://github.com/aag/dinoremix.git
dinoremix / cutimages.py
100644 121 lines (87 sloc) 3.2 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Python script to cut up Dinosaur Comics strips into their
constituent panels.
 
Copyright 2008, 2009 Adam Goforth
Started on: 2008.04.20
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
 
import Image
import os
import sys
 
 
def cutImage(image):
"""Accepts an image containing a whole comic and returns a dict of images, each a panel of the comic."""
retDict = {}
 
tlwidth = 244
tmwidth = 129
trwidth = 362
 
blwidth = 194
bmwidth = 298
brwidth = 243
 
theight = 243
bheight = 244
 
tlcoords = (0, 0, tlwidth, theight)
tlimage = image.crop(tlcoords)
tlimage.load()
retDict["topleft"] = tlimage
 
tmcoords = (tlwidth, 0, tlwidth + tmwidth, theight)
tmimage = image.crop(tmcoords)
tmimage.load()
retDict["topmiddle"] = tmimage
 
trcoords = (tlwidth + tmwidth, 0, tlwidth + tmwidth + trwidth, theight)
trimage = image.crop(trcoords)
trimage.load()
retDict["topright"] = trimage
 
blcoords = (0, theight, blwidth, theight + bheight)
blimage = image.crop(blcoords)
blimage.load()
retDict["bottomleft"] = blimage
 
bmcoords = (blwidth, theight, blwidth + bmwidth, theight + bheight)
bmimage = image.crop(bmcoords)
bmimage.load()
retDict["bottommiddle"] = bmimage
 
brcoords = (blwidth + bmwidth, theight, blwidth + bmwidth + brwidth, theight + bheight)
brimage = image.crop(brcoords)
brimage.load()
retDict["bottomright"] = brimage
 
return retDict
 
 
def dumpPanels(outdir, comicFileName, imageDict):
for panelname, image in imageDict.iteritems():
outputdir = os.path.join(outdir, panelname)
if not os.path.exists(outputdir):
os.mkdir(outputdir)
 
outfileName = os.path.join(outputdir, comicFileName) + "-" + panelname + ".png"
image.save(outfileName)
 
 
def cutAllImagesInDir(indir, outdir):
allFiles = os.listdir(indir)
numImages = 0
 
print "Looking for new files to cut..."
 
for filename in allFiles:
filePath = os.path.join(indir + filename)
filenamePrefix = filename[0:filename.rfind(".")]
tlPanelFileName = filenamePrefix + "-topleft.png"
 
if not os.path.exists(os.path.join(outdir, "topleft", tlPanelFileName)):
print 'processing', filePath
 
image = Image.open(filePath)
panels = cutImage(image)
 
dirs, filename = os.path.split(filePath)
filename, sep, ext = filename.rpartition(".")
 
dumpPanels(outdir, filename, panels)
numImages = numImages + 1
 
return numImages
 
 
def cutAllImages():
scriptDir = sys.path[0]
comicsDir = os.path.join(scriptDir, "comics/")
panelsDir = os.path.join(scriptDir, "panels/")
processedImages = cutAllImagesInDir(comicsDir, panelsDir)
 
print processedImages, 'images processed\n'
 
if __name__ == "__main__":
cutAllImages()