def extract_raster_image(wmf_data): try: wmf, wmf_err = plugins['wmf'] except KeyError: raise Unavailable('libwmf not available on this platform') if wmf_err: raise Unavailable(wmf_err) if iswindows: import sys, os appdir = sys.app_dir if isinstance(appdir, unicode): appdir = appdir.encode(filesystem_encoding) fdir = os.path.join(appdir, 'wmffonts') wmf.set_font_dir(fdir) data = '' with TemporaryDirectory('wmf2png') as tdir: with CurrentDir(tdir): wmf.render(wmf_data) images = list(sorted(glob.glob('*.png'))) if not images: raise NoRaster('No raster images in WMF') data = open(images[0], 'rb').read() im = Image() im.load(data) pw = PixelWand() pw.color = '#ffffff' im.rotate(pw, 180) return im.export('png')
def process_pages(self): from calibre.utils.magick import PixelWand for i, wand in enumerate(self.pages): pw = PixelWand() pw.color = '#ffffff' wand.set_border_color(pw) if self.rotate: wand.rotate(pw, -90) if not self.opts.disable_trim: wand.trim(25 * 65535 / 100) wand.set_page(0, 0, 0, 0) # Clear page after trim, like a "+repage" # Do the Photoshop "Auto Levels" equivalent if not self.opts.dont_normalize: wand.normalize() sizex, sizey = wand.size SCRWIDTH, SCRHEIGHT = self.opts.output_profile.comic_screen_size try: if self.opts.comic_image_size: SCRWIDTH, SCRHEIGHT = map(int, [ x.strip() for x in self.opts.comic_image_size.split('x') ]) except: pass # Ignore if self.opts.keep_aspect_ratio: # Preserve the aspect ratio by adding border aspect = float(sizex) / float(sizey) if aspect <= (float(SCRWIDTH) / float(SCRHEIGHT)): newsizey = SCRHEIGHT newsizex = int(newsizey * aspect) deltax = (SCRWIDTH - newsizex) / 2 deltay = 0 else: newsizex = SCRWIDTH newsizey = int(newsizex / aspect) deltax = 0 deltay = (SCRHEIGHT - newsizey) / 2 if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE: # Too large and resizing fails, so better # to leave it as original size wand.size = (newsizex, newsizey) wand.set_border_color(pw) wand.add_border(pw, deltax, deltay) elif self.opts.wide: # Keep aspect and Use device height as scaled image width so landscape mode is clean aspect = float(sizex) / float(sizey) screen_aspect = float(SCRWIDTH) / float(SCRHEIGHT) # Get dimensions of the landscape mode screen # Add 25px back to height for the battery bar. wscreenx = SCRHEIGHT + 25 wscreeny = int(wscreenx / screen_aspect) if aspect <= screen_aspect: newsizey = wscreeny newsizex = int(newsizey * aspect) deltax = (wscreenx - newsizex) / 2 deltay = 0 else: newsizex = wscreenx newsizey = int(newsizex / aspect) deltax = 0 deltay = (wscreeny - newsizey) / 2 if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE: # Too large and resizing fails, so better # to leave it as original size wand.size = (newsizex, newsizey) wand.set_border_color(pw) wand.add_border(pw, deltax, deltay) else: if SCRWIDTH < MAX_SCREEN_SIZE and SCRHEIGHT < MAX_SCREEN_SIZE: wand.size = (SCRWIDTH, SCRHEIGHT) if not self.opts.dont_sharpen: wand.sharpen(0.0, 1.0) if not self.opts.dont_grayscale: wand.type = 'GrayscaleType' if self.opts.despeckle: wand.despeckle() wand.quantize(self.opts.colors) dest = '%d_%d.%s' % (self.num, i, self.opts.output_format) dest = os.path.join(self.dest, dest) if dest.lower().endswith('.png'): dest += '8' wand.save(dest) if dest.endswith('8'): dest = dest[:-1] os.rename(dest + '8', dest) self.append(dest)
def process_pages(self): from calibre.utils.magick import PixelWand for i, wand in enumerate(self.pages): pw = PixelWand() pw.color = '#ffffff' wand.set_border_color(pw) if self.rotate: wand.rotate(pw, -90) if not self.opts.disable_trim: wand.trim(25*65535/100) wand.set_page(0, 0, 0, 0) # Clear page after trim, like a "+repage" # Do the Photoshop "Auto Levels" equivalent if not self.opts.dont_normalize: wand.normalize() sizex, sizey = wand.size SCRWIDTH, SCRHEIGHT = self.opts.output_profile.comic_screen_size try: if self.opts.comic_image_size: SCRWIDTH, SCRHEIGHT = map(int, [x.strip() for x in self.opts.comic_image_size.split('x')]) except: pass # Ignore if self.opts.keep_aspect_ratio: # Preserve the aspect ratio by adding border aspect = float(sizex) / float(sizey) if aspect <= (float(SCRWIDTH) / float(SCRHEIGHT)): newsizey = SCRHEIGHT newsizex = int(newsizey * aspect) deltax = (SCRWIDTH - newsizex) / 2 deltay = 0 else: newsizex = SCRWIDTH newsizey = int(newsizex / aspect) deltax = 0 deltay = (SCRHEIGHT - newsizey) / 2 if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE: # Too large and resizing fails, so better # to leave it as original size wand.size = (newsizex, newsizey) wand.set_border_color(pw) wand.add_border(pw, deltax, deltay) elif self.opts.wide: # Keep aspect and Use device height as scaled image width so landscape mode is clean aspect = float(sizex) / float(sizey) screen_aspect = float(SCRWIDTH) / float(SCRHEIGHT) # Get dimensions of the landscape mode screen # Add 25px back to height for the battery bar. wscreenx = SCRHEIGHT + 25 wscreeny = int(wscreenx / screen_aspect) if aspect <= screen_aspect: newsizey = wscreeny newsizex = int(newsizey * aspect) deltax = (wscreenx - newsizex) / 2 deltay = 0 else: newsizex = wscreenx newsizey = int(newsizex / aspect) deltax = 0 deltay = (wscreeny - newsizey) / 2 if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE: # Too large and resizing fails, so better # to leave it as original size wand.size = (newsizex, newsizey) wand.set_border_color(pw) wand.add_border(pw, deltax, deltay) else: if SCRWIDTH < MAX_SCREEN_SIZE and SCRHEIGHT < MAX_SCREEN_SIZE: wand.size = (SCRWIDTH, SCRHEIGHT) if not self.opts.dont_sharpen: wand.sharpen(0.0, 1.0) if not self.opts.dont_grayscale: wand.type = 'GrayscaleType' if self.opts.despeckle: wand.despeckle() wand.quantize(self.opts.colors) dest = '%d_%d.%s'%(self.num, i, self.opts.output_format) dest = os.path.join(self.dest, dest) if dest.lower().endswith('.png'): dest += '8' wand.save(dest) if dest.endswith('8'): dest = dest[:-1] os.rename(dest+'8', dest) self.append(dest)