04-22-2019, 01:49 PM | #1 | ||
Member
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:
Quote:
I have already checked the image file exists. Thank you |
||
04-22-2019, 02:20 PM | #2 |
Grand Sorcerer
Posts: 28,039
Karma: 199464182
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") 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): Last edited by DiapDealer; 04-22-2019 at 02:38 PM. |
04-22-2019, 03:00 PM | #3 | ||
Member
Posts: 17
Karma: 10
Join Date: Jul 2017
Device: epub
|
Thank you for quick reply.
I changed it to: Quote:
Quote:
Thank you |
||
04-22-2019, 03:50 PM | #4 |
Grand Sorcerer
Posts: 28,039
Karma: 199464182
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) Last edited by DiapDealer; 04-22-2019 at 03:53 PM. |
04-22-2019, 04:03 PM | #5 |
Grand Sorcerer
Posts: 5,640
Karma: 23191067
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 |
04-22-2019, 04:10 PM | #6 |
Grand Sorcerer
Posts: 28,039
Karma: 199464182
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
|
04-22-2019, 08:13 PM | #7 |
Member
Posts: 17
Karma: 10
Join Date: Jul 2017
Device: epub
|
Thank you very much
yeah it´s works |
05-25-2019, 01:38 AM | #8 |
Guru
Posts: 677
Karma: 929286
Join Date: Apr 2014
Device: PW-3, iPad, 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 01:41 AM. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[File Type Plugin] Automatically Add Covers to .djvu Files on Import | hazel.noelle | Plugins | 4 | 01-07-2023 08:33 AM |
plugin to paste an image file from clipboard | dhdurgee | Plugins | 23 | 02-02-2017 03:04 PM |
table of content file link not add in toc.ncx file | bhambhu | Conversion | 0 | 01-14-2016 02:40 AM |
JPG image lost when add HTML file to Calibre | mshane | Conversion | 8 | 01-03-2013 07:08 PM |