Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Sigil > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 04-22-2019, 12:49 PM   #1
nirosan
Member
nirosan began at the beginning.
 
Posts: 17
Karma: 10
Join Date: Jul 2017
Device: epub
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
nirosan is offline   Reply With Quote
Old 04-22-2019, 01:20 PM   #2
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 20,309
Karma: 104227632
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
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()

Last edited by DiapDealer; 04-22-2019 at 01:38 PM.
DiapDealer is offline   Reply With Quote
Old 04-22-2019, 02:00 PM   #3
nirosan
Member
nirosan began at the beginning.
 
Posts: 17
Karma: 10
Join Date: Jul 2017
Device: epub
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
nirosan is offline   Reply With Quote
Old 04-22-2019, 02:50 PM   #4
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 20,309
Karma: 104227632
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
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.

Last edited by DiapDealer; 04-22-2019 at 02:53 PM.
DiapDealer is offline   Reply With Quote
Old 04-22-2019, 03:03 PM   #5
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 4,547
Karma: 14554601
Join Date: Dec 2010
Device: Kindle PW2
@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.
Doitsu is offline   Reply With Quote
Old 04-22-2019, 03:10 PM   #6
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 20,309
Karma: 104227632
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by Doitsu View Post
@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!
DiapDealer is offline   Reply With Quote
Old 04-22-2019, 07:13 PM   #7
nirosan
Member
nirosan began at the beginning.
 
Posts: 17
Karma: 10
Join Date: Jul 2017
Device: epub
Thank you very much

yeah it´s works
nirosan is offline   Reply With Quote
Old 05-25-2019, 12:38 AM   #8
AlanHK
Evangelist
AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'AlanHK knows the difference between 'who' and 'whom'
 
AlanHK's Avatar
 
Posts: 491
Karma: 10232
Join Date: Apr 2014
Device: PW-3, Android phone
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.

Last edited by AlanHK; 05-25-2019 at 12:41 AM.
AlanHK is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[File Type Plugin] Automatically Add Covers to .djvu Files on Import hazel.noelle Plugins 0 03-22-2019 05:34 PM
plugin to paste an image file from clipboard dhdurgee Plugins 23 02-02-2017 02:04 PM
table of content file link not add in toc.ncx file bhambhu Conversion 0 01-14-2016 01:40 AM
JPG image lost when add HTML file to Calibre mshane Conversion 8 01-03-2013 06:08 PM


All times are GMT -4. The time now is 12:40 AM.


MobileRead.com is a privately owned, operated and funded community.