diff --git a/resources/images/library.png b/resources/images/library.png index e093247162..721ef0546d 100644 Binary files a/resources/images/library.png and b/resources/images/library.png differ diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 6e9c72de26..5b2bb99022 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -19,15 +19,13 @@ from calibre.ebooks.BeautifulSoup import BeautifulSoup, NavigableString, CData, from calibre.ebooks.metadata.opf2 import OPFCreator from calibre import entity_to_unicode from calibre.web import Recipe -from calibre.ebooks import render_html from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.metadata import MetaInformation from calibre.web.feeds import feed_from_xml, templates, feeds_from_index, Feed from calibre.web.fetch.simple import option_parser as web2disk_option_parser from calibre.web.fetch.simple import RecursiveFetcher from calibre.utils.threadpool import WorkRequest, ThreadPool, NoResultsPending -from calibre.ptempfile import PersistentTemporaryFile, \ - PersistentTemporaryDirectory +from calibre.ptempfile import PersistentTemporaryFile from calibre.utils.date import now as nowf class BasicNewsRecipe(Recipe): @@ -928,63 +926,52 @@ class BasicNewsRecipe(Recipe): ''' Create a generic cover for recipes that dont have a cover ''' - from calibre.gui2 import is_ok_to_use_qt - if not is_ok_to_use_qt(): - return False - img_data = open(I('library.png'), 'rb').read() - tdir = PersistentTemporaryDirectory('_default_cover') - img = os.path.join(tdir, 'logo.png') - with open(img, 'wb') as g: - g.write(img_data) - img = os.path.basename(img) - html= u'''\ - - - - - - -

%(title)s

-

-
-
- calibre -
-
-

%(date)s

-




-

%(author)s

-








-

Produced by %(app)s

-
-
- - - '''%dict(title=self.title if isinstance(self.title, unicode) else self.title.decode(preferred_encoding, 'replace'), - author=self.__author__ if isinstance(self.__author__, unicode) else self.__author__.decode(preferred_encoding, 'replace'), - date=strftime(self.timefmt), - app=__appname__ +' '+__version__, - img=img) - hf = os.path.join(tdir, 'cover.htm') - with open(hf, 'wb') as f: - f.write(html.encode('utf-8')) - renderer = render_html(hf) - if renderer.tb is not None: - self.log.warning('Failed to render default cover') - self.log.debug(renderer.tb) - else: - cover_file.write(renderer.data) + try: + try: + from PIL import Image, ImageDraw, ImageFont + Image, ImageDraw, ImageFont + except ImportError: + import Image, ImageDraw, ImageFont + font_path = P('fonts/liberation/LiberationSerif-Bold.ttf') + font = ImageFont.truetype(font_path, 48) + title = self.title if isinstance(self.title, unicode) else \ + self.title.decode(preferred_encoding, 'replace') + date = strftime(self.timefmt) + app = '['+__appname__ +' '+__version__+']' + + COVER_WIDTH, COVER_HEIGHT = 590, 750 + img = Image.new('RGB', (COVER_WIDTH, COVER_HEIGHT), 'white') + draw = ImageDraw.Draw(img) + # Title + width, height = draw.textsize(title, font=font) + left = max(int((COVER_WIDTH - width)/2.), 0) + top = 15 + draw.text((left, top), title, fill=(0,0,0), font=font) + bottom = top + height + # Date + font = ImageFont.truetype(font_path, 32) + width, height = draw.textsize(date, font=font) + left = max(int((COVER_WIDTH - width)/2.), 0) + draw.text((left, bottom+15), date, fill=(0,0,0), font=font) + # Vanity + font = ImageFont.truetype(font_path, 28) + width, height = draw.textsize(app, font=font) + left = max(int((COVER_WIDTH - width)/2.), 0) + top = COVER_HEIGHT - height - 15 + draw.text((left, top), app, fill=(0,0,0), font=font) + # Logo + logo = Image.open(I('library.png'), 'r') + width, height = logo.size + left = max(int((COVER_WIDTH - width)/2.), 0) + top = max(int((COVER_HEIGHT - height)/2.), 0) + img.paste(logo, (left, top)) + img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE) + + img.convert('RGB').save(cover_file, 'JPEG') cover_file.flush() + except: + self.log.exception('Failed to generate default cover') + return False return True def get_masthead_title(self):