mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
Modernize code in t4b.py
Fixes #1265225 [Update Orizon driver](https://bugs.launchpad.net/calibre/+bug/1265225)
This commit is contained in:
parent
39dcf1e4db
commit
8e6ee68bd8
@ -1,20 +1,18 @@
|
|||||||
|
# vim:fileencoding=utf-8
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2013, Jellby <jellby at yahoo.com>'
|
__copyright__ = '2013, Jellby <jellby at yahoo.com>'
|
||||||
'''
|
'''
|
||||||
Write a t4b file to disk.
|
Write a t4b file to disk.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import StringIO
|
from io import BytesIO
|
||||||
|
|
||||||
DEFAULT_T4B_DATA = ''
|
DEFAULT_T4B_DATA = b''
|
||||||
|
|
||||||
def reduce_color(c):
|
def reduce_color(c):
|
||||||
if (c < 0):
|
return max(0, min(255, c))//16
|
||||||
return 0
|
|
||||||
elif (c > 255):
|
|
||||||
return 15
|
|
||||||
else:
|
|
||||||
return c//16
|
|
||||||
|
|
||||||
def write_t4b(t4bfile, coverdata=None):
|
def write_t4b(t4bfile, coverdata=None):
|
||||||
'''
|
'''
|
||||||
@ -22,21 +20,20 @@ def write_t4b(t4bfile, coverdata=None):
|
|||||||
coverdata is a string representation of a JPEG file.
|
coverdata is a string representation of a JPEG file.
|
||||||
'''
|
'''
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
if coverdata != None:
|
if coverdata is not None:
|
||||||
coverdata = StringIO.StringIO(coverdata)
|
coverdata = BytesIO(coverdata)
|
||||||
cover = Image.open(coverdata).convert("L")
|
cover = Image.open(coverdata).convert("L")
|
||||||
cover.thumbnail((96, 144), Image.ANTIALIAS)
|
cover.thumbnail((96, 144), Image.ANTIALIAS)
|
||||||
t4bcover = Image.new('L', (96, 144), 'white')
|
t4bcover = Image.new('L', (96, 144), 'white')
|
||||||
|
|
||||||
x, y = cover.size
|
x, y = cover.size
|
||||||
t4bcover.paste(cover, ((96-x)/2, (144-y)/2))
|
t4bcover.paste(cover, ((96-x)//2, (144-y)//2))
|
||||||
|
|
||||||
pxs = t4bcover.getdata()
|
pxs = t4bcover.getdata()
|
||||||
t4bfile.write('t4bp')
|
t4bfile.write(b't4bp')
|
||||||
for i in range(0,len(pxs),2):
|
data = (16 * reduce_color(pxs[i]) + reduce_color(pxs[i+1])
|
||||||
byte = reduce_color(pxs[i])
|
for i in xrange(0, len(pxs), 2))
|
||||||
byte = 16*byte + reduce_color(pxs[i+1])
|
t4bfile.write(bytes(bytearray(data)))
|
||||||
t4bfile.write(chr(byte))
|
|
||||||
else:
|
else:
|
||||||
t4bfile.write(DEFAULT_T4B_DATA)
|
t4bfile.write(DEFAULT_T4B_DATA)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user