jaraco / jaraco.clipboard

MIT License
21 stars 1 forks source link

Copying an image #7

Open kezibooo opened 5 years ago

kezibooo commented 5 years ago

How can ı copy an image to clipboard? Can you write a simple example?

jaraco commented 5 years ago

I don't recall a specific example where I've attempted to copy an image to the clipboard.

I have pasted images from the clipboard, mainly with this code in lpaste. You'll notice that I have to do bitmap manipulation. I don't recall if this code worked on anything except Windows.

In fact, looking at the code, I don't see that copy_image is defined for any OS, so I may not have explored what it takes to effectively put an image on the clipboard. I'm afraid someone will have to step up and dig into the code (and APIs) to add support for this function.

StakFallT commented 1 year ago

I don't recall a specific example where I've attempted to copy an image to the clipboard.

I'm currently looking for a solution to copy and paste images as well. The scenario I need it for is this:

I've written an AI bot for the IT team I work with at work. This bot uses Selenium to interface with Microsoft Teams (yes, I know they offer a bot framework... for a price, and I don't know how limited it could be). However, I'd like to add image generation (chart / graph / plots / etc.) functionality to the AI bot. Generating the image isn't necessarily the problem, there's libraries to do just that even for data plotting. However, I'm not looking to save it to a file and then attach the file to the message and then delete the file (that's kind've messy and seems pretty unnecessary). It'd be nice to, where I generate the image, copy it to clipboard, and then paste it in the message with the text generated. Since this library doesn't appear to support that, I'll probably keep looking rather than wait for the functionality to become present, but I thought I would pass along a scenario where such functionality would be used.

jaraco commented 1 year ago

The code for getting an image is pretty straightforward on Windows. Probably setting an image would be something as simple as:

def set_image(dib: bytes):
    with context():
        SetClipboardData(clipboard.CF_DIB, dib)

However, that approach would only be useful for setting a device-independent bitmap. Is that a useful format? Would Windows-only support be useful, or would users need support for other platforms. To add support from other platforms, it would need to be contributed to pyperclip (Linux) or richxerox (macOS) or to another library that could replace one of those.

bmorris-ocsd commented 1 year ago

For me, unfortunately no. The VM I'm running my bot on is Linux (kUbuntu distro). I managed to utilize a workaround (creating an image and specifying it to xclip -- not ideal but for now, it works). The relevant reference information I think is here:

https://tronche.com/gui/x/icccm/

It refers to the events and process that occurs with X11's clipboard data. Granted, I suppose other frameworks Gnome, Kde, Qt, et al. have their own clipboard implementation, and I don't know how different (that is, if it is different) Wayland APIs are from X11. I just saw in my news feed today that Gnome is looking at making the push to drop support for X11 and continue forward with only Wayland since they're already, apparently, defaulting to that. Regardless, I think being able to copy images to the clipboard under Linux is still a valued functionality since it seems like there's almost nothing really out there that does it (short of doing like what I'm doing -- calling out to an external program). Using the GDK / GTK documentation, I was able to store images in the clipboard from Python and can even see the image, but when I use Selenium to simulate the paste via action_chains (sending the ctrl+v), it doesn't paste anything. To me, this indicates an issue with the target needing to be image/png but I can't seem to be able to set the target manually (like there's a call or two missing from the Python bindings to GTK / GDK or something)