Exemple #1
0
def rescale_image(data, maxsizeb=IMAGE_MAX_SIZE, dimen=None):
    '''
    Convert image setting all transparent pixels to white and changing format
    to JPEG. Ensure the resultant image has a byte size less than
    maxsizeb.

    If dimen is not None, generate a thumbnail of
    width=dimen, height=dimen or width, height = dimen (depending on the type
    of dimen)

    Returns the image as a bytestring
    '''
    if dimen is not None:
        if hasattr(dimen, '__len__'):
            width, height = dimen
        else:
            width = height = dimen
        data = scale_image(data,
                           width=width,
                           height=height,
                           compression_quality=90)[-1]
    else:
        # Replace transparent pixels with white pixels and convert to JPEG
        data = save_cover_data_to(data)
    if len(data) <= maxsizeb:
        return data
    orig_data = data  # save it in case compression fails
    quality = 90
    while len(data) > maxsizeb and quality >= 5:
        data = image_to_data(image_from_data(orig_data),
                             compression_quality=quality)
        quality -= 5
    if len(data) <= maxsizeb:
        return data
    orig_data = data

    scale = 0.9
    while len(data) > maxsizeb and scale >= 0.05:
        img = image_from_data(data)
        w, h = img.width(), img.height()
        img = resize_image(img, int(scale * w), int(scale * h))
        data = image_to_data(img, compression_quality=quality)
        scale -= 0.05
    return data
Exemple #2
0
def rescale_image(data, maxsizeb=IMAGE_MAX_SIZE, dimen=None):
    '''
    Convert image setting all transparent pixels to white and changing format
    to JPEG. Ensure the resultant image has a byte size less than
    maxsizeb.

    If dimen is not None, generate a thumbnail of
    width=dimen, height=dimen or width, height = dimen (depending on the type
    of dimen)

    Returns the image as a bytestring
    '''
    if dimen is not None:
        if hasattr(dimen, '__len__'):
            width, height = dimen
        else:
            width = height = dimen
        data = scale_image(data, width=width, height=height, compression_quality=90)[-1]
    else:
        # Replace transparent pixels with white pixels and convert to JPEG
        data = save_cover_data_to(data)
    if len(data) <= maxsizeb:
        return data
    orig_data = data  # save it in case compression fails
    quality = 90
    while len(data) > maxsizeb and quality >= 5:
        data = image_to_data(image_from_data(orig_data), compression_quality=quality)
        quality -= 5
    if len(data) <= maxsizeb:
        return data
    orig_data = data

    scale = 0.9
    while len(data) > maxsizeb and scale >= 0.05:
        img = image_from_data(data)
        w, h = img.width(), img.height()
        img = resize_image(img, int(scale*w), int(scale*h))
        data = image_to_data(img, compression_quality=quality)
        scale -= 0.05
    return data
Exemple #3
0
 def HandleImage(self, imageData, imagePath):
     from calibre.utils.img import image_from_data, resize_image, image_to_data
     img = image_from_data(imageData)
     x, y = img.width(), img.height()
     if self.opts:
         if self.opts.snb_full_screen:
             SCREEN_X, SCREEN_Y = self.opts.output_profile.screen_size
         else:
             SCREEN_X, SCREEN_Y = self.opts.output_profile.comic_screen_size
     else:
         SCREEN_X = 540
         SCREEN_Y = 700
     # Handle big image only
     if x > SCREEN_X or y > SCREEN_Y:
         xScale = float(x) / SCREEN_X
         yScale = float(y) / SCREEN_Y
         scale = max(xScale, yScale)
         # TODO : intelligent image rotation
         #     img = img.rotate(90)
         #     x,y = y,x
         img = resize_image(img, x // scale, y // scale)
     with lopen(imagePath, 'wb') as f:
         f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))
Exemple #4
0
 def HandleImage(self, imageData, imagePath):
     from calibre.utils.img import image_from_data, resize_image, image_to_data
     img = image_from_data(imageData)
     x, y = img.width(), img.height()
     if self.opts:
         if self.opts.snb_full_screen:
             SCREEN_X, SCREEN_Y = self.opts.output_profile.screen_size
         else:
             SCREEN_X, SCREEN_Y = self.opts.output_profile.comic_screen_size
     else:
         SCREEN_X = 540
         SCREEN_Y = 700
     # Handle big image only
     if x > SCREEN_X or y > SCREEN_Y:
         xScale = float(x) / SCREEN_X
         yScale = float(y) / SCREEN_Y
         scale = max(xScale, yScale)
         # TODO : intelligent image rotation
         #     img = img.rotate(90)
         #     x,y = y,x
         img = resize_image(img, x / scale, y / scale)
     with lopen(imagePath, 'wb') as f:
         f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))
Exemple #5
0
    def process_pages(self):
        from calibre.utils.img import (
            image_to_data, rotate_image, remove_borders_from_image, normalize_image,
            add_borders_to_image, resize_image, gaussian_sharpen_image, grayscale_image,
            despeckle_image, quantize_image
        )
        for i, img in enumerate(self.pages):
            if self.rotate:
                img = rotate_image(img, -90)

            if not self.opts.disable_trim:
                img = remove_borders_from_image(img)

            # Do the Photoshop "Auto Levels" equivalent
            if not self.opts.dont_normalize:
                img = normalize_image(img)
            sizex, sizey = img.width(), img.height()

            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
                    img = resize_image(img, newsizex, newsizey)
                    img = add_borders_to_image(img, left=deltax, right=deltax, top=deltay, bottom=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
                    img = resize_image(img, newsizex, newsizey)
                    img = add_borders_to_image(img, left=deltax, right=deltax, top=deltay, bottom=deltay)
            else:
                if SCRWIDTH < MAX_SCREEN_SIZE and SCRHEIGHT < MAX_SCREEN_SIZE:
                    img = resize_image(img, SCRWIDTH, SCRHEIGHT)

            if not self.opts.dont_sharpen:
                img = gaussian_sharpen_image(img, 0.0, 1.0)

            if not self.opts.dont_grayscale:
                img = grayscale_image(img)

            if self.opts.despeckle:
                img = despeckle_image(img)

            if self.opts.output_format.lower() == 'png' and self.opts.colors:
                img = quantize_image(img, max_colors=min(256, self.opts.colors))
            dest = '%d_%d.%s'%(self.num, i, self.opts.output_format)
            dest = os.path.join(self.dest, dest)
            with lopen(dest, 'wb') as f:
                f.write(image_to_data(img, fmt=self.opts.output_format))
            self.append(dest)
Exemple #6
0
 def size(self, val):
     w, h = val[:2]
     self.img = resize_image(self.img, w, h)
Exemple #7
0
 def thumbnail(self, width, height):
     self.img = resize_image(self.img, width, height)
Exemple #8
0
 def size(self, val):
     w, h = val[:2]
     self.img = resize_image(self.img, w, h)
Exemple #9
0
 def thumbnail(self, width, height):
     self.img = resize_image(self.img, width, height)
Exemple #10
0
    def process_pages(self):
        from calibre.utils.img import (image_to_data, rotate_image,
                                       remove_borders_from_image,
                                       normalize_image, add_borders_to_image,
                                       resize_image, gaussian_sharpen_image,
                                       grayscale_image, despeckle_image,
                                       quantize_image)
        for i, img in enumerate(self.pages):
            if self.rotate:
                img = rotate_image(img, -90)

            if not self.opts.disable_trim:
                img = remove_borders_from_image(img)

            # Do the Photoshop "Auto Levels" equivalent
            if not self.opts.dont_normalize:
                img = normalize_image(img)
            sizex, sizey = img.width(), img.height()

            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
                    img = resize_image(img, newsizex, newsizey)
                    img = add_borders_to_image(img,
                                               left=deltax,
                                               right=deltax,
                                               top=deltay,
                                               bottom=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
                    img = resize_image(img, newsizex, newsizey)
                    img = add_borders_to_image(img,
                                               left=deltax,
                                               right=deltax,
                                               top=deltay,
                                               bottom=deltay)
            else:
                if SCRWIDTH < MAX_SCREEN_SIZE and SCRHEIGHT < MAX_SCREEN_SIZE:
                    img = resize_image(img, SCRWIDTH, SCRHEIGHT)

            if not self.opts.dont_sharpen:
                img = gaussian_sharpen_image(img, 0.0, 1.0)

            if not self.opts.dont_grayscale:
                img = grayscale_image(img)

            if self.opts.despeckle:
                img = despeckle_image(img)

            if self.opts.output_format.lower() == 'png' and self.opts.colors:
                img = quantize_image(img,
                                     max_colors=min(256, self.opts.colors))
            dest = '%d_%d.%s' % (self.num, i, self.opts.output_format)
            dest = os.path.join(self.dest, dest)
            with lopen(dest, 'wb') as f:
                f.write(image_to_data(img, fmt=self.opts.output_format))
            self.append(dest)