MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Is it possible to add image file via plugin? (https://www.mobileread.com/forums/showthread.php?t=316834)

nirosan 04-22-2019 01:49 PM

Is it possible to add image file via plugin?
 
Hi,

I am trying to write a plugin which generate math formulas as image.
Is it possible to add the generated image file to ebook?
If yes how can I do this?

What I have tried:
Quote:

image_path = get_abspath_of_rendered_file()
with open(image_path, "rb") as imageFile:
str = base64.b64encode(imageFile.read())
self.bk.addfile("MathmMlid", "MathMLImage", str, "image/png", properties=None, fallback=None,overlay=None)
but nothing happens.

Quote:

image_path = get_abspath_of_rendered_file()
self.bk.addfile("MathmMlid", "MathMLImage", image_path, "image/png", properties=None, fallback=None,overlay=None)
file.close()
but luncher crashed.

I have already checked the image file exists.


Thank you

DiapDealer 04-22-2019 02:20 PM

At it's simplest:

Code:

f=open(image_path, 'rb')
img=f.read()
f.close()
bk.addfile("MathmMlid", "MathMLImage.png", img, "image/png")

I've no idea if you need to use self.bk.addfile. That all depends on whether you've passed the bk object as an argument to another object/class or not.

But your second parameter to addfile() needs to be a file name (with a valid extension).

Your first attempt is closer. Forget the encoding--just read in the data as binary. Figure out if you really need self.bk (or just bk) and make sure your basename parameter has a png extension.

Your second attempt tried to use a filepath parameter instead of binary image data.
Code:

addfile(uniqueid, basename, data, mime=None, properties=None, fallback=None, overlay=None):
The Plugin to insert and wrap an image with SVG has a working example of adding an image to an epub via bk.addfile()

nirosan 04-22-2019 03:00 PM

Thank you for quick reply.

I changed it to:
Quote:

print("BookContainer: ", self.bk)
image_path = get_abspath_of_rendered_file()
print("Image path: ", image_path)
f = open(image_path, 'rb')
img = f.read()
f.close()
print("Img: ", img)
self.bk.addfile("MathmMlid", "MathMLImage.png", img, "image/png")
But the plugin crashed:
Quote:

Launcher process crashed
BookContainer: <bookcontainer.BookContainer object at 0x000001927B1F42E8>
Image path: C:\Users\NiroBala\AppData\Local\sigil-ebook\sigil\plugins\AddMath\util\rendered.png
Img: b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\ x0e\x00\x00\x00\x0b\x04\x03\x00\x00\x00\xbd\xab\xa 1Q\x00\x00\x000PLTE\xff\xff\xff\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00/\xdd\xa0{\x00\x00\x00\x0ftRNS\x00\x10\x89\x99f2\xd dDT\xcd"v\xab\xef\xbb\xd4\xa5x\xe1\x00\x00\x00\tpH Ys\x00\x00\x0e\xc4\x00\x00\x0e\xc4\x01\x95+\x0e\x1 b\x00\x00\x00WIDAT\x08\x1dc``TVr`\x00\x82\xb0\x02\ x86$\x06\x16\x06\xd6\x1f\x0c\x0c\xfd\x02\x15\x0cl\ x13\x18\x18\xd67(0\xec\x07J\xcakm`\x98/\x00\xa4u\x18\x18\xfe\x02\x95\xca\xa720\xdc\x03\xd 1\x06\x0c\x0c/\x81t?\x90^\xbf\x80A\xa8\xe7@#\x03\xeb\xa1\xdd\x0e \\\x93\x1c\x00\xc6\x13\x12\xc0w?R\x9a\x00\x00\x00\ x00IEND\xaeB`\x82'
I will debug more later.

Thank you

DiapDealer 04-22-2019 03:50 PM

Yeah, trying to debug-print binary image data to the Plugin Runner window probably isn't a very good idea.

Get rid of:
Code:

print("Img: ", img)
and try again.

Doitsu 04-22-2019 04:03 PM

@nirosan

Copy a .png file to your plugin folder, rename it to cover.png and test the following code:

Code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os, string, random

def run(bk):
    image_name = 'cover.png'
    mime_type = 'image/png'
    image_path = os.path.join(bk._w.plugin_dir, bk._w.plugin_name, image_name)
   
    if os.path.isfile(image_path):
        with open(image_path, 'rb') as f:
            image_data = f.read()
   
        try:
            bk.addfile(image_name, image_name, image_data, mime_type)
        except:
            random_prefix = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
            image_name = random_prefix + image_name
            bk.addfile(image_name, image_name, image_data, mime_type)
           
    return 0

It should add a cover.png file to your epub.

DiapDealer 04-22-2019 04:10 PM

Quote:

Originally Posted by Doitsu (Post 3834571)
@nirosan

Copy a .png file to your plugin folder, rename it to cover.png and test the following code:


It should add a cover.png file to your epub.

Works like a champ for me!

nirosan 04-22-2019 08:13 PM

Thank you very much

yeah itīs works

AlanHK 05-25-2019 01:38 AM

You've used PNG images.

If the epub is going to be used to generate a Kindle file, Kindlegen will convert it to either GIF or JPEG as its fancy takes.
So better to choose one of those formats yourself which will pass through, rather than PNG and risk it becoming a larger and blurrier JPEG. I'd use GIF for this.

Also, whether you use GIF or PNG, for an image of text, I restrict to monochrome and I reduce the palette to 4 colours, very compact, but still avoids jaggies. 8 or 16 colours is slightly smoother but more does not improve quality. For extra credit, inspect the palette and make the darkest grey 100% black; I can do this in Irfanview manually, but maybe your images come like that to begin with.


All times are GMT -4. The time now is 08:29 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.