Add: manage bogus image (like too short, miss bytes)
PIL lib is raising a IOError with such pictures, catch it, and if there is an error, give the original picture instead.
This commit is contained in:
parent
3fd0b94134
commit
7dfad73aff
@ -90,18 +90,34 @@ class KindleData:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# 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):
|
def splitLeft(image):
|
||||||
widthImg, heightImg = image.size
|
widthImg, heightImg = image.size
|
||||||
|
|
||||||
return image.crop((0, 0, widthImg / 2, heightImg))
|
return image.crop((0, 0, widthImg / 2, heightImg))
|
||||||
|
|
||||||
|
|
||||||
|
@protect_bad_image
|
||||||
def splitRight(image):
|
def splitRight(image):
|
||||||
widthImg, heightImg = image.size
|
widthImg, heightImg = image.size
|
||||||
|
|
||||||
return image.crop((widthImg / 2, 0, widthImg, heightImg))
|
return image.crop((widthImg / 2, 0, widthImg, heightImg))
|
||||||
|
|
||||||
|
|
||||||
|
@protect_bad_image
|
||||||
def quantizeImage(image, palette):
|
def quantizeImage(image, palette):
|
||||||
colors = len(palette) / 3
|
colors = len(palette) / 3
|
||||||
if colors < 256:
|
if colors < 256:
|
||||||
@ -113,10 +129,14 @@ def quantizeImage(image, palette):
|
|||||||
return image.quantize(palette=palImg)
|
return image.quantize(palette=palImg)
|
||||||
|
|
||||||
|
|
||||||
|
@protect_bad_image
|
||||||
def stretchImage(image, size):
|
def stretchImage(image, size):
|
||||||
widthDev, heightDev = size
|
widthDev, heightDev = size
|
||||||
|
|
||||||
return image.resize((widthDev, heightDev), Image.ANTIALIAS)
|
return image.resize((widthDev, heightDev), Image.ANTIALIAS)
|
||||||
|
|
||||||
|
|
||||||
|
@protect_bad_image
|
||||||
def resizeImage(image, size):
|
def resizeImage(image, size):
|
||||||
widthDev, heightDev = size
|
widthDev, heightDev = size
|
||||||
widthImg, heightImg = image.size
|
widthImg, heightImg = image.size
|
||||||
@ -140,19 +160,21 @@ def resizeImage(image, size):
|
|||||||
return image.resize((widthImg, heightImg), Image.ANTIALIAS)
|
return image.resize((widthImg, heightImg), Image.ANTIALIAS)
|
||||||
|
|
||||||
|
|
||||||
|
@protect_bad_image
|
||||||
def formatImage(image):
|
def formatImage(image):
|
||||||
if image.mode == 'RGB':
|
if image.mode == 'RGB':
|
||||||
return image
|
return image
|
||||||
|
|
||||||
return image.convert('RGB')
|
return image.convert('RGB')
|
||||||
|
|
||||||
|
|
||||||
|
@protect_bad_image
|
||||||
def orientImage(image, size):
|
def orientImage(image, size):
|
||||||
widthDev, heightDev = size
|
widthDev, heightDev = size
|
||||||
widthImg, heightImg = image.size
|
widthImg, heightImg = image.size
|
||||||
|
|
||||||
if (widthImg > heightImg) != (widthDev > heightDev):
|
if (widthImg > heightImg) != (widthDev > heightDev):
|
||||||
return image.rotate(90, Image.BICUBIC, True)
|
return image.rotate(90, Image.BICUBIC, True)
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user