コード例 #1
0
	def get_details(self, search_result, timeout):
		if search_result.cover_bak:
				if search_result.cover_bak[0] == "/":
					search_result.cover_bak = self.base_url + search_result.cover_bak
				br1 = self.create_browser()
				with closing(br1.open(search_result.cover_bak, timeout=timeout)) as f:
					search_result.cover_data = f.read()
				search_result.cover_data = thumbnail(search_result.cover_data, 64, 64)[2]
コード例 #2
0
ファイル: content.py プロジェクト: suman95/calibre
 def copy_func(dest):
     buf = BytesIO()
     db.copy_cover_to(book_id, buf)
     quality = min(
         99,
         max(50,
             tweaks['content_server_thumbnail_compression_quality']))
     w, h, data = thumbnail(buf.getvalue(),
                            width=width,
                            height=height,
                            compression_quality=quality)
     dest.write(data)
コード例 #3
0
 def run(self):
     while self._run and not self.tasks.empty():
         try:
             result, callback, timeout = self.tasks.get()
             if result and result.cover_url:
                 with closing(self.br.open(result.cover_url, timeout=timeout)) as f:
                     result.cover_data = f.read()
                 result.cover_data = thumbnail(result.cover_data, 64, 64)[2]
                 callback()
             self.tasks.task_done()
         except:
             if DEBUG:
                 traceback.print_exc()
コード例 #4
0
 def run(self):
     while self._run and not self.tasks.empty():
         try:
             result, callback, timeout = self.tasks.get()
             if result and result.cover_url:
                 with closing(self.br.open(result.cover_url, timeout=timeout)) as f:
                     result.cover_data = f.read()
                 result.cover_data = thumbnail(result.cover_data, 64, 64)[2]
                 callback()
             self.tasks.task_done()
         except:
             if DEBUG:
                 traceback.print_exc()
コード例 #5
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 = thumbnail(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, 'img.jpg', return_data=True)
    if len(data) <= maxsizeb:
        return data
    orig_data = data
    img = Image()
    quality = 95

    img.load(data)
    while len(data) >= maxsizeb and quality >= 10:
        quality -= 5
        img.set_compression_quality(quality)
        data = img.export('jpg')
    if len(data) <= maxsizeb:
        return data
    orig_data = data

    scale = 0.9
    while len(data) >= maxsizeb and scale >= 0.05:
        img = Image()
        img.load(orig_data)
        w, h = img.size
        img.size = (int(scale * w), int(scale * h))
        img.set_compression_quality(quality)
        data = img.export('jpg')
        scale -= 0.05
    return data
コード例 #6
0
ファイル: utils.py プロジェクト: AtulKumar2/calibre
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 = thumbnail(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, 'img.jpg', return_data=True)
    if len(data) <= maxsizeb:
        return data
    orig_data = data
    img = Image()
    quality = 95

    img.load(data)
    while len(data) >= maxsizeb and quality >= 10:
        quality -= 5
        img.set_compression_quality(quality)
        data = img.export('jpg')
    if len(data) <= maxsizeb:
        return data
    orig_data = data

    scale = 0.9
    while len(data) >= maxsizeb and scale >= 0.05:
        img = Image()
        img.load(orig_data)
        w, h = img.size
        img.size = (int(scale*w), int(scale*h))
        img.set_compression_quality(quality)
        data = img.export('jpg')
        scale -= 0.05
    return data
コード例 #7
0
ファイル: driver.py プロジェクト: BobPyron/calibre
    def upload_cover(self, path, filename, metadata, filepath):
        from calibre.ebooks import calibre_cover
        from calibre.utils.magick.draw import thumbnail
        coverdata = getattr(metadata, 'thumbnail', None)
        if coverdata and coverdata[2]:
            cover = coverdata[2]
        else:
            cover = calibre_cover(metadata.get('title', _('Unknown')),
                    metadata.get('authors', _('Unknown')))

        cover = thumbnail(cover, width=self.THUMBNAIL_HEIGHT,
                height=self.THUMBNAIL_HEIGHT, fmt='png')[-1]

        cpath = self.alex_cpath(os.path.join(path, filename))
        cdir = os.path.dirname(cpath)
        if not os.path.exists(cdir):
            os.makedirs(cdir)
        with open(cpath, 'wb') as coverfile:
            coverfile.write(cover)
コード例 #8
0
ファイル: simple.py プロジェクト: sss/calibre
    def rescale_image(self, data):
        orig_w, orig_h, ifmt = identify_data(data)
        orig_data = data  # save it in case compression fails
        if self.scale_news_images is not None:
            wmax, hmax = self.scale_news_images
            scale, new_w, new_h = fit_image(orig_w, orig_h, wmax, hmax)
            if scale:
                data = thumbnail(data, new_w, new_h,
                                 compression_quality=95)[-1]
                orig_w = new_w
                orig_h = new_h
        if self.compress_news_images_max_size is None:
            if self.compress_news_images_auto_size is None:  # not compressing
                return data
            else:
                maxsizeb = (orig_w *
                            orig_h) / self.compress_news_images_auto_size
        else:
            maxsizeb = self.compress_news_images_max_size * 1024
        scaled_data = data  # save it in case compression fails
        if len(scaled_data) <= maxsizeb:  # no compression required
            return scaled_data

        img = Image()
        quality = 95
        img.load(data)
        while len(data) >= maxsizeb and quality >= 5:
            quality -= 5
            img.set_compression_quality(quality)
            data = img.export('jpg')

        if len(data) >= len(scaled_data):  # compression failed
            return orig_data if len(orig_data) <= len(
                scaled_data) else scaled_data

        if len(data) >= len(orig_data):  # no improvement
            return orig_data

        return data
コード例 #9
0
    def upload_cover(self, path, filename, metadata, filepath):
        from calibre.ebooks import calibre_cover
        from calibre.utils.magick.draw import thumbnail
        coverdata = getattr(metadata, 'thumbnail', None)
        if coverdata and coverdata[2]:
            cover = coverdata[2]
        else:
            cover = calibre_cover(metadata.get('title', _('Unknown')),
                                  metadata.get('authors', _('Unknown')))

        cover = thumbnail(cover,
                          width=self.THUMBNAIL_HEIGHT,
                          height=self.THUMBNAIL_HEIGHT,
                          fmt='png')[-1]

        cpath = self.alex_cpath(os.path.join(path, filename))
        cdir = os.path.dirname(cpath)
        if not os.path.exists(cdir):
            os.makedirs(cdir)
        with open(cpath, 'wb') as coverfile:
            coverfile.write(cover)
            fsync(coverfile)
コード例 #10
0
ファイル: simple.py プロジェクト: anzizhao/calibre
    def rescale_image(self, data):
        orig_w, orig_h, ifmt = identify_data(data)
        orig_data = data  # save it in case compression fails
        if self.scale_news_images is not None:
            wmax, hmax = self.scale_news_images
            scale, new_w, new_h = fit_image(orig_w, orig_h, wmax, hmax)
            if scale:
                data = thumbnail(data, new_w, new_h, compression_quality=95)[-1]
                orig_w = new_w
                orig_h = new_h
        if self.compress_news_images_max_size is None:
            if self.compress_news_images_auto_size is None:  # not compressing
                return data
            else:
                maxsizeb = (orig_w * orig_h)/self.compress_news_images_auto_size
        else:
            maxsizeb = self.compress_news_images_max_size * 1024
        scaled_data = data  # save it in case compression fails
        if len(scaled_data) <= maxsizeb:  # no compression required
            return scaled_data

        img = Image()
        quality = 95
        img.load(data)
        while len(data) >= maxsizeb and quality >= 5:
            quality -= 5
            img.set_compression_quality(quality)
            data = img.export('jpg')

        if len(data) >= len(scaled_data):  # compression failed
            return orig_data if len(orig_data) <= len(scaled_data) else scaled_data

        if len(data) >= len(orig_data):  # no improvement
            return orig_data

        return data
コード例 #11
0
ファイル: content.py プロジェクト: ZRhinoceros/calibre
 def copy_func(dest):
     buf = BytesIO()
     db.copy_cover_to(book_id, buf)
     quality = min(99, max(50, tweaks["content_server_thumbnail_compression_quality"]))
     w, h, data = thumbnail(buf.getvalue(), width=width, height=height, compression_quality=quality)
     dest.write(data)