MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   plugin to paste an image file from clipboard (https://www.mobileread.com/forums/showthread.php?t=267269)

dhdurgee 11-07-2015 08:55 PM

plugin to paste an image file from clipboard
 
I believe an edit plugin could be created to retrieve an image from the clipboard and save it in the epub Images. I lack knowledge in Python, the interfaces to the clipboard and the tools to work with the epub from the plugin, so I am hoping that someone else might be interested in creating such a plugin.

The incentive for this plugin is to make it simpler to incorporate images that are not already stored on the computer, typically from the internet, into the epub being edited by Sigil. The current method of incorporating such images involves saving them to a file, incorporating that file into the epub and deleting the no longer needed file. Copying the image to the clipboard and then using the plugin to incorporate it into the epub simplifies this greatly.

Dave

Doitsu 11-13-2015 08:47 AM

1 Attachment(s)
[Plugin] PasteImage - Adds images from the clipboard

Updated: December 22, 2015
Current Version: "0.2.1"

This is a very simple image pasting plugin that'll add clipboard images to the currently loaded epub as .jpg images (regardless of the original format).

Installation:

Select Plugins > Manage Plugins > Add Plugin > PasteImage.zip

If you're using the Windows version of Sigil 0.9.0 (or higher) select Use Bundled Python. If you're using an older Sigil version, you'll need to install a Python interpreter and the Pillow package. To install Pillow open an admin command prompt and enter the following command:

Code:

pip install Pillow
The Linux version of the plugin requires the gtk Python package and the gtk libraries. (Both gtk2 and gtk3 are supported.)

Unfortunately, the plugin is not fully compatible with OSX. You can't copy images via the system clipboard, however, you can copy images via browser image URLs. (Right-click the image in the browser and select Copy Image Location/URL.)

Usage:

Copy an image or an image URL to the clipboard and select Plugins > Edit > PasteImage.

Known issues:

The plugin won't work with all apps. For example, you can't paste images copied from Google Chrome (Firefox works). You also might get a "Pillow import error" message even though Pillow is installed, if Pillow can't handle the image.
The jpeg image quality is hard-coded, but it can easily be changed by editing the quality value in plugin.py.

License: GNU General Public License v3 (GPL-3)

dhdurgee 11-13-2015 10:28 AM

I just installed and tested this on my system, Linux Mint 17.1 rebecca x64 Cinnamon, and tested it with an image from SeaMonkey 2.39 and it worked as designed. Thank you for a quick response to my request for assistance.

Dave

eschwartz 11-13-2015 02:03 PM

Doitsu -- for python2+3 you can use:

Code:

from gi.repository import Gtk, Gdk
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
image = cb.wait_for_text()

PyGtk is deprecated (that's why there is no python3 version) and they want you to use PyGObject instead.

dhdurgee 11-13-2015 02:07 PM

Now that I have tried it on a few real examples I have run into one minor thing I needed to fix in the plugin.py:

On line 41 there is a "png" that should be "jpeg"

Once I fixed that I am no longer getting validation errors.

Dave

Doitsu 11-13-2015 02:17 PM

Quote:

Originally Posted by dhdurgee (Post 3205261)
Now that I have tried it on a few real examples I have run into one minor thing I needed to fix in the plugin.py:

On line 41 there is a "png" that should be "jpeg"

Thanks for the bug report. I've attached a fixed version to the second post.

Doitsu 11-13-2015 03:03 PM

Quote:

Originally Posted by eschwartz (Post 3205258)
Doitsu -- for python2+3 you can use:

Code:

from gi.repository import Gtk, Gdk
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
image = cb.wait_for_text()


Thanks for the suggestion. I'd actually experimented with the following similar code:

Code:

import tempfile
from gi.repository import Gtk, Gdk
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
img = clipboard.wait_for_image()

if img:   
    temp_file = tempfile.NamedTemporaryFile().name
    img.save(temp_file, 'jpeg')

but I got a weird error message for img.save() that I couldn't resolve:

Code:

AttributeError: 'Pixbuf' object has no attribute 'save'
Error: 'Pixbuf' object has no attribute 'save'

even though img is a Gtk 3.0 gi.repository.GdkPixbuf.Pixbuf object, which definitely should have a save attribute.

eschwartz 11-13-2015 03:16 PM

What lunacy. :)

Apparently the documentation is wrong, and you should use
Code:

img.savev(filename, image_type, ['quality'], ['100'])
https://stackoverflow.com/questions/...used-in-python

Doitsu 11-14-2015 09:13 AM

Quote:

Originally Posted by eschwartz (Post 3205309)
Apparently the documentation is wrong, and you should use
Code:

img.savev(filename, image_type, ['quality'], ['100'])

Thanks to your superior Google Fu, I was able to make this most important of all Sigil plugins Python 3.4 compatible for Linux users. :D

I've attached a new version to the second post. Windows users won't need to download this updated version, because it fixes a Linux-only issue.

eschwartz 11-15-2015 12:25 AM

:D

Any particular reason you use different methods for PY2 (pygtk) vs PY3 (gi.repository)?
Just curious.

Doitsu 11-15-2015 02:40 AM

Quote:

Originally Posted by eschwartz (Post 3206105)
Any particular reason you use different methods for PY2 (pygtk) vs PY3 (gi.repository)?

I decided to include Python 2.7 + gtk mainly for backwards compatibility, because not every Linux user has a bleeding edge distro with the latest libraries.

Also, IMHO, the additional if-then import statements make this plugin look more sophisticated. :D

eschwartz 11-15-2015 02:53 AM

Sophistication. :cool:

Doitsu 11-21-2015 01:26 AM

I've added partial OSX support to the plugin. The new version (attached to the second post) allows you to copy web browser images via the image URL. Simply right-click an image in your web browser, select Copy Image Location/URL and then select Plugins > Edit > PasteImage.

Of course, this feature also works with the Windows and Linux versions of Sigil.

KevinH 11-21-2015 07:56 PM

Added this to Sigil Plugin Index sticky

SHunter101 11-28-2015 12:10 PM

Thanks for the cool plugin. I have a feature request. I would like the ability to paste the image from clipboard to where I have positioned the cursor. Not sure if that is possible.

SH


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

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