Hi, kovid, hi guys,
here is a new recipe. I only create it to show how i insert PNG coverfiles into my RSS feeds.
I found out that only jpg/jpeg files are shown as calibre covers. But mostly i got PNG files with alpha channel for this. (as i search on google-pictures for e.g. indian times ;-)
So i wrote a python function to convert this for me. May it is possible to add this functionality into the code? If not , this works like charm. Try it.
Code:
#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import unicode_literals, division, absolute_import, print_function
from calibre.web.feeds.news import BasicNewsRecipe
# some modification to get png as coverurl
from PIL import Image
from urllib2 import urlopen
from tempfile import mkdtemp
# (my) calibre only allow jpeg files as cover.
# mostly i got fine png files so i convert them
def png_to_jpg(url):
# e.g. url is 'http:\wiki.com\myfinecover.png'
# first we take the extension of the url if we got one
ext = url.rsplit('.', 1)[-1]
# now ext = png
# now we check if png match. May later we want other extension to? like svg ...?
if ext.upper() in ('PNG'):
# load the png url file to memory
im = Image.open(urlopen(url))
# if our png file has a alpha channel - remember jpg could not have one
if im.mode in ('RGBA', 'LA'):
# if we got alpha pictures, we create a backgound as a RGB Colored image with size of our original
bg = Image.new(im.mode[:-1], im.size, (255,255,255))
# and past the loaded image in front of the background
bg.paste(im,im.split()[-1])
# rewrite our loaded image with the new one
im = bg
# next to create a temporary directory and add the jpg extension . Finally we get a temp jpg filename
tmp = mkdtemp()+ '.jpg'
# save our picture as a jpg file
# variable tmp holds the filename
im.convert('RGB').save(tmp,"JPEG") #this converts png image as jpeg
else: # no png extension, so we pass the url to the output
tmp = url
return tmp
class AdvancedUserRecipe1583667870(BasicNewsRecipe):
title = 'The Times of India'
description = 'just a little demo to show the PNG cover function'
__author__ = 'vohe'
publisher = 'i guess: The Times Group'
use_embedded_content = False
language = 'en'
rescale_images = True
remove_empty_feeds = True
timeout = 5
no_stylesheets = True
oldest_article = 2
max_articles_per_feed = 200
auto_cleanup = True
cover_url = png_to_jpg('https://upload.wikimedia.org/wikipedia/en/thumb/8/82/The_Times_Group.svg/1200px-The_Times_Group.svg.png')
feeds = [
('Times of india', 'https://timesofindia.indiatimes.com/rssfeeds/-2128936835.cms'),
]