ignore covers that are to tall or to wide

This commit is contained in:
j 2016-01-13 16:41:28 +05:30
parent e2b96b6ba2
commit e6e52d53d5
2 changed files with 25 additions and 5 deletions

View file

@ -311,8 +311,11 @@ class Item(db.Model):
def update_icons(self): def update_icons(self):
def get_ratio(data): def get_ratio(data):
img = Image.open(BytesIO(data)) try:
return img.size[0]/img.size[1] img = Image.open(BytesIO(data))
return img.size[0]/img.size[1]
except:
return 1
key = 'cover:%s'%self.id key = 'cover:%s'%self.id
cover = None cover = None
if 'cover' in self.meta and self.meta['cover']: if 'cover' in self.meta and self.meta['cover']:

View file

@ -8,6 +8,9 @@ import zipfile
import re import re
from urllib.parse import unquote from urllib.parse import unquote
import lxml.html import lxml.html
from io import BytesIO
from PIL import Image
from ox import strip_tags, decode_html from ox import strip_tags, decode_html
@ -16,6 +19,13 @@ from utils import find_isbns, get_language, to_isbn13
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_ratio(data):
try:
img = Image.open(BytesIO(data))
return img.size[0]/img.size[1]
except:
return -1
def cover(path): def cover(path):
logger.debug('cover %s', path) logger.debug('cover %s', path)
data = None data = None
@ -27,12 +37,19 @@ def cover(path):
def use(filename): def use(filename):
logger.debug('using %s', filename) logger.debug('using %s', filename)
return z.read(filename) data = z.read(filename)
r = get_ratio(data)
if r < 0.3 or r > 2:
return None
return data
files = []
for f in z.filelist: for f in z.filelist:
if f.filename == 'calibre-logo.png':
continue
if 'cover' in f.filename.lower() and f.filename.split('.')[-1] in ('jpg', 'jpeg', 'png'): if 'cover' in f.filename.lower() and f.filename.split('.')[-1] in ('jpg', 'jpeg', 'png'):
return use(f.filename) return use(f.filename)
files = [f.filename for f in z.filelist] files.append(f.filename)
opf = [f for f in files if f.endswith('opf')] opf = [f for f in files if f.endswith('opf')]
if opf: if opf:
#logger.debug('opf: %s', z.read(opf[0]).decode()) #logger.debug('opf: %s', z.read(opf[0]).decode())