From b4c69ba6343cae44110ea28e364d82ba57c313ae Mon Sep 17 00:00:00 2001 From: Li Fanxi Date: Sun, 10 Oct 2010 17:47:23 +0800 Subject: [PATCH] [SNBOutput] Reduce the size of the images --- src/calibre/ebooks/snb/output.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/snb/output.py b/src/calibre/ebooks/snb/output.py index 65f06c7994..08ede2ca37 100644 --- a/src/calibre/ebooks/snb/output.py +++ b/src/calibre/ebooks/snb/output.py @@ -21,7 +21,7 @@ def ProcessFileName(fileName): fileName = fileName.replace("#", "_") # Make it lower case fileName = fileName.lower() - # Change extension from jpeg to jpg + # Change extension for image files to png root, ext = os.path.splitext(fileName) if ext in [ '.jpeg', '.jpg', '.gif', '.svg' ]: fileName = root + '.png' @@ -187,11 +187,8 @@ class SNBOutput(OutputFormatPlugin): log.debug('Converting image: %s ...' % item.href) content = m.hrefs[item.href].data if m.hrefs[item.href].media_type != PNG_MIME: - # Convert - from calibre.utils.magick import Image - img = Image() - img.load(content) - img.save(os.path.join(snbiDir, ProcessFileName(item.href))) + # Convert & Resize image + self.HandleImage(content, os.path.join(snbiDir, ProcessFileName(item.href))) else: outputFile = open(os.path.join(snbiDir, ProcessFileName(item.href)), 'wb') outputFile.write(content) @@ -202,6 +199,29 @@ class SNBOutput(OutputFormatPlugin): snbFile.FromDir(tdir) snbFile.Output(output_path) + def HandleImage(self, imageData, imagePath): + from calibre.utils.magick import Image + img = Image() + img.load(imageData) + print img.size + (x,y) = img.size + # TODO use the data from device profile + SCREEN_X = 540 + SCREEN_Y = 700 + # Handle big image only + if x > SCREEN_X or y > SCREEN_Y: + SCREEN_RATIO = float(SCREEN_Y) / SCREEN_X + imgRatio = float(y) / x + xScale = float(x) / SCREEN_X + yScale = float(y) / SCREEN_Y + scale = max(xScale, yScale) + # TODO : intelligent image rotation + # img = img.rotate(90) + # x,y = y,x + img.size = (x / scale, y / scale) + print img.size + img.save(imagePath) + if __name__ == '__main__': from calibre.ebooks.oeb.reader import OEBReader from calibre.ebooks.oeb.base import OEBBook