diff --git a/fb2epub.config b/fb2epub.config index 2ea8265..b4e155c 100644 --- a/fb2epub.config +++ b/fb2epub.config @@ -42,6 +42,7 @@ True True True + False profiles/vignettes/title_before.png @@ -79,6 +80,7 @@ True True True + False @@ -100,6 +102,7 @@ True True True + False @@ -121,6 +124,7 @@ True True True + False profiles/vignettes/title_before.png @@ -158,6 +162,7 @@ True True True + False diff --git a/fb2mobi.config b/fb2mobi.config index 7c2674a..3f8bad3 100644 --- a/fb2mobi.config +++ b/fb2mobi.config @@ -42,6 +42,7 @@ True True True + False profiles/vignettes/title_before.png @@ -79,6 +80,7 @@ True True True + False @@ -100,6 +102,7 @@ True True True + False @@ -121,6 +124,7 @@ True True True + False profiles/vignettes/title_before.png @@ -158,6 +162,7 @@ True True True + False diff --git a/fb2mobi.py b/fb2mobi.py index 6e43c17..22e8c6d 100644 --- a/fb2mobi.py +++ b/fb2mobi.py @@ -483,6 +483,8 @@ def process(args): config.current_profile['tocTitle'] = args.toctitle if args.chapteronnewpage is not None: config.current_profile['chapterOnNewPage'] = args.chapteronnewpage + if args.removepngtransparency is not None: + config.current_profile['removePngTransparency'] = args.removepngtransparency if args.noMOBIoptimization: config.noMOBIoptimization = args.noMOBIoptimization if args.sendtokindle is not None: @@ -593,6 +595,10 @@ def process(args): tocplace_group.add_argument('--toc-before-body', dest='tocbeforebody', action='store_true', default=None, help='Put TOC at the book beginning') tocplace_group.add_argument('--toc-after-body', dest='tocbeforebody', action='store_false', default=None, help='Put TOC at the book end') + pngtransparency_group = argparser.add_mutually_exclusive_group() + pngtransparency_group.add_argument('--remove-png-transparency', dest='removepngtransparency', action='store_true', default=None, help='Remove transparency in PNG images') + pngtransparency_group.add_argument('--no-remove-png-transparency', dest='removepngtransparency', action='store_false', default=None, help='Do not remove transparency in PNG images') + # Для совместимости с MyHomeLib добавляем аргументы, которые передает MHL в fb2mobi.exe argparser.add_argument('-nc', action='store_true', default=False, help='For MyHomeLib compatibility') argparser.add_argument('-cl', action='store_true', help='For MyHomeLib compatibility') diff --git a/modules/config.py b/modules/config.py index 0cb15f9..826776d 100644 --- a/modules/config.py +++ b/modules/config.py @@ -64,6 +64,7 @@ def __init__(self, config_file): self.profiles['default']['generateAnnotationPage'] = True self.profiles['default']['generateOPFGuide'] = True self.profiles['default']['kindleRemovePersonalLabel'] = True + self.profiles['default']['removePngTransparency'] = False self.current_profile = {} self.mhl = False @@ -171,6 +172,7 @@ def _load(self): self.profiles[prof_name]['generateOPFGuide'] = True self.profiles[prof_name]['flatTOC'] = True self.profiles[prof_name]['kindleRemovePersonalLabel'] = True + self.profiles[prof_name]['removePngTransparency'] = False for p in prof: if p.tag == 'hyphens': @@ -209,6 +211,9 @@ def _load(self): elif p.tag == 'kindleRemovePersonalLabel': self.profiles[prof_name]['kindleRemovePersonalLabel'] = p.text.lower() == 'true' + elif p.tag == 'removePngTransparency': + self.profiles[prof_name]['removePngTransparency'] = p.text.lower() == 'true' + elif p.tag == 'generateAnnotationPage': self.profiles[prof_name]['generateAnnotationPage'] = p.text.lower() == 'true' @@ -309,6 +314,7 @@ def _getProfiles(self): E('generateAnnotationPage', str(self.profiles[p]['generateAnnotationPage'])), E('generateOPFGuide', str(self.profiles[p]['generateOPFGuide'])), E('kindleRemovePersonalLabel', str(self.profiles[p]['kindleRemovePersonalLabel'])), + E('removePngTransparency', str(self.profiles[p]['removePngTransparency'])), E('vignettes', *self._getVignettes(p) ), diff --git a/modules/fb2html.py b/modules/fb2html.py index c806f7a..1cb3e17 100644 --- a/modules/fb2html.py +++ b/modules/fb2html.py @@ -119,6 +119,8 @@ def __init__(self, fb2file, mobifile, tempdir, config): self.vignettes = config.current_profile['vignettes'] self.vignette_files = [] + self.removepngtransparency = config.current_profile['removePngTransparency'] # Remove transparency in PNG images + self.annotation_title = config.current_profile['annotationTitle'] # Заголовок для раздела аннотации self.toc_title = config.current_profile['tocTitle'] # Заголовок для раздела содержания @@ -224,6 +226,8 @@ def generate(self): elif ns_tag(child.tag) == 'binary': self.parse_binary(child) + if self.removepngtransparency: + self.remove_png_transparency() self.correct_links() if self.generate_toc_page: self.generate_toc() @@ -281,6 +285,39 @@ def replace_url(url): else: copy_file(self.css_file, os.path.join(self.temp_content_dir, 'stylesheet.css')) + def remove_png_transparency(self): + self.log.info('Removing PNG transparency...') + for img_rel_path in self.image_file_list: + if os.path.splitext(img_rel_path)[1] == '.png': + self.log.debug('Processing file "{}"'.format(img_rel_path)) + + try: + filename = os.path.split(img_rel_path)[1] + img_full_path = os.path.join(self.temp_content_dir, 'images', filename) + img = Image.open(img_full_path) + + if img.format == 'PNG' and (img.mode in ('RGBA', 'LA') + or (img.mode in ('RGB', 'L', 'P') and 'transparency' in img.info)): + + if img.mode == "P" and type(img.info.get("transparency")) is bytes: + img = img.convert("RGBA") + + if img.mode in ("L", "LA"): + bg = Image.new("L", img.size, 255) + else: + bg = Image.new("RGB", img.size, (255, 255, 255)) + + alpha = img.convert("RGBA").split()[-1] + bg.paste(img, mask=alpha) + + img_temp_path = os.path.splitext(img_full_path)[0] + "-o.png" + bg.save(img_temp_path, dpi=img.info.get("dpi")) + os.replace(img_temp_path, img_full_path) + + except: + self.log.warning('Error while removing transparency in file "{}"'.format(img_rel_path)) + self.log.debug('Getting details:', exc_info=True) + def correct_links(self): for fl in self.html_file_list: parser = etree.XMLParser(encoding='utf-8')