View Single Post
Old 05-12-2016, 12:41 PM   #14
jackie_w
Grand Sorcerer
jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.jackie_w ought to be getting tired of karma fortunes by now.
 
Posts: 6,212
Karma: 16534894
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
@Kovid,

I'm doing well here! I've also found a problem when using my Copy Cover plugin to send covers to Pocketbook devices which must be .BMP 4BPP grayscale format.

This is the error message:
Code:
calibre, version 2.56.0
ERROR: Unhandled exception: <b>ValueError</b>:Failed to export image as BMP3 with error: Unsupported image format

calibre 2.56 [64bit]  embedded-python: True is64bit: True
Windows-7-6.1.7601-SP1 Windows ('64bit', 'WindowsPE')
('Windows', '7', '6.1.7601')
Python 2.7.9
Windows: ('7', '6.1.7601', 'SP1', 'Multiprocessor Free')
Successfully initialized third party plugins: DeDRM && Obok DeDRM && ScrambleEbook && Fantastic Fiction && Goodreads && JS Mod Epub && Diaps Editing Toolbag && DOC Input && KePub Metadata Writer && Barnes & Noble && Search The Internet && Count Pages && Wiki Reader && Quality Check && Open With && Copy Cover To Device && EpubSplit && JSTest Plugin && KindleUnpack - The Plugin && KePub Metadata Reader && Extract ISBN && KePub Input && Modify ePub && KoboTouchCSSEdit && Kobo Utilities && KePub Output && KoboBooks
Traceback (most recent call last):
  File "calibre_plugins.copy_cover_to_device.dialogs", line 91, in do_book_action
  File "calibre_plugins.copy_cover_to_device.dialogs", line 140, in copy_to_pocketbook_16
  File "site-packages\calibre\utils\magick\legacy.py", line 92, in save
  File "site-packages\calibre\utils\magick\legacy.py", line 139, in export
  File "site-packages\calibre\utils\img.py", line 124, in image_to_data
ValueError: Failed to export image as BMP3 with error: Unsupported image format
These are the relevant plugin methods. The error relates to the red line:
Spoiler:
Code:
def copy_to_pocketbook_16(self, screen, aspect, cdata, dest):
    img, rescale = self.cover_resize(screen, aspect, cdata)
    # Resize image then convert to greyscale
    if rescale:
        img.size = rescale
    img.quantize(number_colors=16, colorspace='GRAYColorspace')
    # save_cover_data_to(img, dest)
    img.type = "PaletteType"
    img.save(dest, 'BMP3')
    return rescale

def cover_resize(self, screen, aspect, cdata):
    img = Image()
    img.load(cdata)
    rescale = None
    if aspect:
        swidth, sheight = screen
        cwidth, cheight = img.size[:2]
        # calc new image size
        # Set rescale factor to match screen height
        rescale = (int(sheight * cwidth / cheight), sheight)
        if rescale[0] > swidth:
            # if too wide for screen, set to match screen width
            rescale = (swidth, int(swidth * cheight / cwidth))
    else:
        rescale = screen
    return img, rescale


The ImageMagick-related imports are these:
Code:
from calibre.utils.magick.draw import save_cover_data_to
from calibre.utils.magick import Image, create_canvas
I tested as well as I can given I don't own most of the supported devices (Kindle, Nook) and my Pocketbook battery is dead I discovered today. I have no reason to believe there is any problem with:
  • create_canvas - used for Kindles only
  • save_cover_data_to - used for Sony (JPG), Nook (JPG), Kindle (PNG)
    For Pocketbooks, save_cover_data_to had to be replaced by the workaround code (provided by you in 2012 if you can remember that far back ) you see above as it was not saving the in 4BPP format when using Linux.
jackie_w is offline   Reply With Quote