mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
[SNBOutput] Reduce the size of the images
This commit is contained in:
parent
0c387834f4
commit
b4c69ba634
@ -21,7 +21,7 @@ def ProcessFileName(fileName):
|
|||||||
fileName = fileName.replace("#", "_")
|
fileName = fileName.replace("#", "_")
|
||||||
# Make it lower case
|
# Make it lower case
|
||||||
fileName = fileName.lower()
|
fileName = fileName.lower()
|
||||||
# Change extension from jpeg to jpg
|
# Change extension for image files to png
|
||||||
root, ext = os.path.splitext(fileName)
|
root, ext = os.path.splitext(fileName)
|
||||||
if ext in [ '.jpeg', '.jpg', '.gif', '.svg' ]:
|
if ext in [ '.jpeg', '.jpg', '.gif', '.svg' ]:
|
||||||
fileName = root + '.png'
|
fileName = root + '.png'
|
||||||
@ -187,11 +187,8 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
log.debug('Converting image: %s ...' % item.href)
|
log.debug('Converting image: %s ...' % item.href)
|
||||||
content = m.hrefs[item.href].data
|
content = m.hrefs[item.href].data
|
||||||
if m.hrefs[item.href].media_type != PNG_MIME:
|
if m.hrefs[item.href].media_type != PNG_MIME:
|
||||||
# Convert
|
# Convert & Resize image
|
||||||
from calibre.utils.magick import Image
|
self.HandleImage(content, os.path.join(snbiDir, ProcessFileName(item.href)))
|
||||||
img = Image()
|
|
||||||
img.load(content)
|
|
||||||
img.save(os.path.join(snbiDir, ProcessFileName(item.href)))
|
|
||||||
else:
|
else:
|
||||||
outputFile = open(os.path.join(snbiDir, ProcessFileName(item.href)), 'wb')
|
outputFile = open(os.path.join(snbiDir, ProcessFileName(item.href)), 'wb')
|
||||||
outputFile.write(content)
|
outputFile.write(content)
|
||||||
@ -202,6 +199,29 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
snbFile.FromDir(tdir)
|
snbFile.FromDir(tdir)
|
||||||
snbFile.Output(output_path)
|
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__':
|
if __name__ == '__main__':
|
||||||
from calibre.ebooks.oeb.reader import OEBReader
|
from calibre.ebooks.oeb.reader import OEBReader
|
||||||
from calibre.ebooks.oeb.base import OEBBook
|
from calibre.ebooks.oeb.base import OEBBook
|
||||||
|
Loading…
x
Reference in New Issue
Block a user