Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from gmk57/png-transparency
Browse files Browse the repository at this point in the history
Adds an option to make PNG images non-transparent
  • Loading branch information
rupor-github authored Sep 25, 2016
2 parents 265aa59 + b710c6b commit 25d12c1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fb2epub.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes>
<vignette level="default">
<beforeTitle>profiles/vignettes/title_before.png</beforeTitle>
Expand Down Expand Up @@ -79,6 +80,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes/>
</profile>
<profile description="No hyphenation" name="nohyph">
Expand All @@ -100,6 +102,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes/>
</profile>
<profile description="Vignettes" name="vignettes">
Expand All @@ -121,6 +124,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes>
<vignette level="default">
<beforeTitle>profiles/vignettes/title_before.png</beforeTitle>
Expand Down Expand Up @@ -158,6 +162,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes/>
</profile>
</profiles>
Expand Down
5 changes: 5 additions & 0 deletions fb2mobi.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes>
<vignette level="default">
<beforeTitle>profiles/vignettes/title_before.png</beforeTitle>
Expand Down Expand Up @@ -79,6 +80,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes/>
</profile>
<profile description="No hyphenation" name="nohyph">
Expand All @@ -100,6 +102,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes/>
</profile>
<profile description="Vignettes" name="vignettes">
Expand All @@ -121,6 +124,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes>
<vignette level="default">
<beforeTitle>profiles/vignettes/title_before.png</beforeTitle>
Expand Down Expand Up @@ -158,6 +162,7 @@
<generateAnnotationPage>True</generateAnnotationPage>
<generateOPFGuide>True</generateOPFGuide>
<kindleRemovePersonalLabel>True</kindleRemovePersonalLabel>
<removePngTransparency>False</removePngTransparency>
<vignettes/>
</profile>
</profiles>
Expand Down
6 changes: 6 additions & 0 deletions fb2mobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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)
),
Expand Down
37 changes: 37 additions & 0 deletions modules/fb2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] # Заголовок для раздела содержания

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 25d12c1

Please sign in to comment.