예제 #1
0
 def resize_image(self, raw, base, max_width, max_height):
     resized, img = resize_to_fit(raw, max_width, max_height)
     if resized:
         base, ext = os.path.splitext(base)
         base = base + '-%dx%d%s' % (max_width, max_height, ext)
         raw = image_to_data(img, fmt=ext[1:])
     return raw, base, resized
예제 #2
0
def flip_image(img, flip):
    from ebook_converter.utils.img import image_to_data
    from ebook_converter.utils.img import image_and_format_from_data
    from ebook_converter.utils.img import flip_image
    with open(img, 'r+b') as f:
        img, fmt = image_and_format_from_data(f.read())
        img = flip_image(img, horizontal='x' in flip, vertical='y' in flip)
        f.seek(0), f.truncate()
        f.write(image_to_data(img, fmt=fmt))
예제 #3
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
예제 #4
0
def calibre_cover(title,
                  author_string,
                  series_string=None,
                  output_format='jpg',
                  title_size=46,
                  author_size=36,
                  logo_path=None):
    # TODO(gryf): generate cover using pillow
    return None
    title = normalize(title)
    author_string = normalize(author_string)
    series_string = normalize(series_string)
    from ebook_converter.ebooks.covers import calibre_cover2
    from ebook_converter.utils.img import image_to_data
    ans = calibre_cover2(title,
                         author_string or '',
                         series_string or '',
                         logo_path=logo_path,
                         as_qimage=True)
    return image_to_data(ans, fmt=output_format)
예제 #5
0
 def HandleImage(self, imageData, imagePath):
     from ebook_converter.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 open(imagePath, 'wb') as f:
         f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))