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
|
||||
from itertools import repeat
|
||||
from math import floor
|
||||
try:
|
||||
from PIL import Image as PILImage
|
||||
PILImage
|
||||
except ImportError:
|
||||
import Image as PILImage
|
||||
|
||||
|
||||
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.search_query_parser import saved_searches
|
||||
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
|
||||
|
||||
from calibre.utils.magick_draw import save_cover_data_to
|
||||
|
||||
if iswindows:
|
||||
import calibre.utils.winshell as winshell
|
||||
@ -475,11 +469,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
||||
if callable(getattr(data, 'save', None)):
|
||||
data.save(path)
|
||||
else:
|
||||
f = data
|
||||
if not callable(getattr(data, 'read', None)):
|
||||
f = cStringIO.StringIO(data)
|
||||
im = PILImage.open(f)
|
||||
im.convert('RGB').save(path, 'JPEG')
|
||||
if callable(getattr(data, 'read', None)):
|
||||
data = data.read()
|
||||
save_cover_data_to(data, path)
|
||||
|
||||
def book_on_device(self, id):
|
||||
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)
|
||||
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():
|
||||
import subprocess
|
||||
with TemporaryFile('.png') as f:
|
||||
|
Loading…
x
Reference in New Issue
Block a user