This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
image.py
285 lines (225 loc) · 7.78 KB
/
image.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Copyright (C) 2010 Alex Yatskov
#
# 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 3 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 os
from PIL import Image, ImageDraw, ImageStat, ImageChops, ImageOps
class ImageFlags:
Orient = 1 << 0
Resize = 1 << 1
Frame = 1 << 2
Quantize = 1 << 3
ScaleCrop = 1 << 4
SplitRightLeft = 1 << 5 # split right then left
SplitRight = 1 << 6 # split only the right page
SplitLeft = 1 << 7 # split only the left page
SplitLeftRight = 1 << 8 # split left then right page
AutoCrop = 1 << 9
class KindleData:
Palette4 = [
0x00, 0x00, 0x00,
0x55, 0x55, 0x55,
0xaa, 0xaa, 0xaa,
0xff, 0xff, 0xff
]
Palette15a = [
0x00, 0x00, 0x00,
0x11, 0x11, 0x11,
0x22, 0x22, 0x22,
0x33, 0x33, 0x33,
0x44, 0x44, 0x44,
0x55, 0x55, 0x55,
0x66, 0x66, 0x66,
0x77, 0x77, 0x77,
0x88, 0x88, 0x88,
0x99, 0x99, 0x99,
0xaa, 0xaa, 0xaa,
0xbb, 0xbb, 0xbb,
0xcc, 0xcc, 0xcc,
0xdd, 0xdd, 0xdd,
0xff, 0xff, 0xff,
]
Palette15b = [
0x00, 0x00, 0x00,
0x11, 0x11, 0x11,
0x22, 0x22, 0x22,
0x33, 0x33, 0x33,
0x44, 0x44, 0x44,
0x55, 0x55, 0x55,
0x77, 0x77, 0x77,
0x88, 0x88, 0x88,
0x99, 0x99, 0x99,
0xaa, 0xaa, 0xaa,
0xbb, 0xbb, 0xbb,
0xcc, 0xcc, 0xcc,
0xdd, 0xdd, 0xdd,
0xee, 0xee, 0xee,
0xff, 0xff, 0xff,
]
Profiles = {
'Kindle 1': ((600, 800), Palette4),
'Kindle 2/3/Touch': ((600, 800), Palette15a),
'Kindle 4 & 5': ((600, 800), Palette15b),
'Kindle DX/DXG': ((824, 1200), Palette15a),
'Kindle Paperwhite 1 & 2': ((758, 1024), Palette15b),
'Kindle Paperwhite 3/Voyage/Oasis': ((1072, 1448), Palette15b),
'Kobo Mini/Touch': ((600, 800), Palette15b),
'Kobo Glo': ((768, 1024), Palette15b),
'Kobo Glo HD': ((1072, 1448), Palette15b),
'Kobo Aura': ((758, 1024), Palette15b),
'Kobo Aura HD': ((1080, 1440), Palette15b),
'Kobo Aura H2O': ((1080, 1430), Palette15a),
}
# decorate a function that use image, *** and if there
# is an exception raise by PIL (IOError) then return
# the original image because PIL cannot manage it
def protect_bad_image(func):
def func_wrapper(*args, **kwargs):
# If cannot convert (like a bogus image) return the original one
# args will be "image" and other params are after
try:
return func(*args, **kwargs)
except IOError: # Exception from PIL about bad image
return args[0]
return func_wrapper
@protect_bad_image
def splitLeft(image):
widthImg, heightImg = image.size
return image.crop((0, 0, widthImg / 2, heightImg))
@protect_bad_image
def splitRight(image):
widthImg, heightImg = image.size
return image.crop((widthImg / 2, 0, widthImg, heightImg))
@protect_bad_image
def quantizeImage(image, palette):
colors = len(palette) / 3
if colors < 256:
palette = palette + palette[:3] * (256 - colors)
palImg = Image.new('P', (1, 1))
palImg.putpalette(palette)
return image.quantize(palette=palImg)
@protect_bad_image
def scaleCropImage(image, size):
return ImageOps.fit(image, size, Image.ANTIALIAS)
@protect_bad_image
def resizeImage(image, size):
widthDev, heightDev = size
widthImg, heightImg = image.size
if widthImg <= widthDev and heightImg <= heightDev:
return image
ratioImg = float(widthImg) / float(heightImg)
ratioWidth = float(widthImg) / float(widthDev)
ratioHeight = float(heightImg) / float(heightDev)
if ratioWidth > ratioHeight:
widthImg = widthDev
heightImg = int(widthDev / ratioImg)
elif ratioWidth < ratioHeight:
heightImg = heightDev
widthImg = int(heightDev * ratioImg)
else:
widthImg, heightImg = size
return image.resize((widthImg, heightImg), Image.ANTIALIAS)
@protect_bad_image
def formatImage(image):
if image.mode == 'RGB':
return image
return image.convert('RGB')
@protect_bad_image
def orientImage(image, size):
widthDev, heightDev = size
widthImg, heightImg = image.size
if (widthImg > heightImg) != (widthDev > heightDev):
return image.rotate(90, Image.BICUBIC, True)
return image
# We will auto crop the image, by removing just white part around the image
# by inverting colors, and asking a bounder box ^^
@protect_bad_image
def autoCropImage(image):
try:
x0, y0, xend, yend = ImageChops.invert(image).getbbox()
except TypeError: # bad image, specific to chops
return image
image = image.crop((x0, y0, xend, yend))
return image
def frameImage(image, foreground, background, size):
widthDev, heightDev = size
widthImg, heightImg = image.size
pastePt = (
max(0, (widthDev - widthImg) / 2),
max(0, (heightDev - heightImg) / 2)
)
corner1 = (
pastePt[0] - 1,
pastePt[1] - 1
)
corner2 = (
pastePt[0] + widthImg + 1,
pastePt[1] + heightImg + 1
)
imageBg = Image.new(image.mode, size, background)
imageBg.paste(image, pastePt)
draw = ImageDraw.Draw(imageBg)
draw.rectangle([corner1, corner2], outline=foreground)
return imageBg
def loadImage(source):
try:
return Image.open(source)
except IOError:
raise RuntimeError('Cannot read image file %s' % source)
def saveImage(image, target):
try:
image.save(target)
except IOError:
raise RuntimeError('Cannot write image file %s' % target)
# Look if the image is more width than hight, if not, means
# it's should not be split (like the front page of a manga,
# when all the inner pages are double)
def isSplitable(source):
image = loadImage(source)
try:
widthImg, heightImg = image.size
return widthImg > heightImg
except IOError:
raise RuntimeError('Cannot read image file %s' % source)
def convertImage(source, target, device, flags):
try:
size, palette = KindleData.Profiles[device]
except KeyError:
raise RuntimeError('Unexpected output device %s' % device)
# Load image from source path
image = loadImage(source)
# Format according to palette
image = formatImage(image)
# Apply flag transforms
if flags & ImageFlags.SplitRight:
image = splitRight(image)
if flags & ImageFlags.SplitRightLeft:
image = splitLeft(image)
if flags & ImageFlags.SplitLeft:
image = splitLeft(image)
if flags & ImageFlags.SplitLeftRight:
image = splitRight(image)
# Auto crop the image, but before manage size and co, clean the source so
if flags & ImageFlags.AutoCrop:
image = autoCropImage(image)
if flags & ImageFlags.Orient:
image = orientImage(image, size)
if flags & ImageFlags.Resize:
image = resizeImage(image, size)
if flags & ImageFlags.ScaleCrop:
image = scaleCropImage(image, size)
if flags & ImageFlags.Frame:
image = frameImage(image, tuple(palette[:3]), tuple(palette[-3:]), size)
if flags & ImageFlags.Quantize:
image = quantizeImage(image, palette)
saveImage(image, target)