mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
When setting an image with transparent pixels as the book cover, overlay it on a white background first. Fixes transparent covers getting random backgrounds.
This commit is contained in:
parent
1fc5cd26c8
commit
a76b689d5d
@ -9,12 +9,6 @@ The database used to store ebook metadata
|
|||||||
import os, sys, shutil, cStringIO, glob,functools, traceback
|
import os, sys, shutil, cStringIO, glob,functools, traceback
|
||||||
from itertools import repeat
|
from itertools import repeat
|
||||||
from math import floor
|
from math import floor
|
||||||
try:
|
|
||||||
from PIL import Image as PILImage
|
|
||||||
PILImage
|
|
||||||
except ImportError:
|
|
||||||
import Image as PILImage
|
|
||||||
|
|
||||||
|
|
||||||
from PyQt4.QtGui import QImage
|
from PyQt4.QtGui import QImage
|
||||||
|
|
||||||
@ -37,7 +31,7 @@ from calibre.utils.date import utcnow, now as nowf, utcfromtimestamp
|
|||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs
|
||||||
from calibre.utils.search_query_parser import saved_searches
|
from calibre.utils.search_query_parser import saved_searches
|
||||||
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
|
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
|
||||||
|
from calibre.utils.magick_draw import save_cover_data_to
|
||||||
|
|
||||||
if iswindows:
|
if iswindows:
|
||||||
import calibre.utils.winshell as winshell
|
import calibre.utils.winshell as winshell
|
||||||
@ -475,11 +469,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
if callable(getattr(data, 'save', None)):
|
if callable(getattr(data, 'save', None)):
|
||||||
data.save(path)
|
data.save(path)
|
||||||
else:
|
else:
|
||||||
f = data
|
if callable(getattr(data, 'read', None)):
|
||||||
if not callable(getattr(data, 'read', None)):
|
data = data.read()
|
||||||
f = cStringIO.StringIO(data)
|
save_cover_data_to(data, path)
|
||||||
im = PILImage.open(f)
|
|
||||||
im.convert('RGB').save(path, 'JPEG')
|
|
||||||
|
|
||||||
def book_on_device(self, id):
|
def book_on_device(self, id):
|
||||||
if callable(self.book_on_device_func):
|
if callable(self.book_on_device_func):
|
||||||
|
@ -212,6 +212,23 @@ def create_cover_page(top_lines, logo_path, width=590, height=750,
|
|||||||
p.DestroyMagickWand(canvas)
|
p.DestroyMagickWand(canvas)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
def save_cover_data_to(data, path, bgcolor='white'):
|
||||||
|
'''
|
||||||
|
Saves image in data to path, in the format specified by the path
|
||||||
|
extension. Composes the image onto a blank cancas so as to
|
||||||
|
properly convert transparent images.
|
||||||
|
'''
|
||||||
|
with open(path, 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
with p.ImageMagick():
|
||||||
|
img = load_image(path)
|
||||||
|
canvas = create_canvas(p.MagickGetImageWidth(img),
|
||||||
|
p.MagickGetImageHeight(img), bgcolor)
|
||||||
|
compose_image(canvas, img, 0, 0)
|
||||||
|
p.MagickWriteImage(canvas, path)
|
||||||
|
p.DestroyMagickWand(img)
|
||||||
|
p.DestroyMagickWand(canvas)
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
import subprocess
|
import subprocess
|
||||||
with TemporaryFile('.png') as f:
|
with TemporaryFile('.png') as f:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user