Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save bitmap to clipboard #48

Open
techtonik opened this issue Nov 6, 2015 · 8 comments
Open

Save bitmap to clipboard #48

techtonik opened this issue Nov 6, 2015 · 8 comments

Comments

@techtonik
Copy link

It would be nice if there was API that can save captured bitmaps to clipboard. Something that accepts PyCBitmap object from pywin32:

SetClipboardData(CF_BITMAP, dataBitMap)
@asweigart
Copy link
Owner

Good idea. I'd like to keep Pyperclip platform-independent. But if it's not that difficult to implement this on Windows, then having this be a Windows-only feature for now would be good.

@mhils
Copy link
Contributor

mhils commented Nov 10, 2015

Implementing this is quite easy - one just needs to expose a format parameter which defaults to CF_UNICODETEXT and then replace the two occurences of CF_UNICODETEXT with that.
The harder problem is that different function signatures per OS are really ugly. One could add the format parameter to every clipboard and raise RuntimeErrors if the clipboard is text only add a dedicated copy_bitmap method. The latter is difficult again because there isn't really a finite list of clipboard data types (https://msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx)

@techtonik
Copy link
Author

Well, ability of clipboard formats is not a problem - as long as there is room for extension people will add what they need when they need it. The current problem is to understand where to add this and how to debug possible "unable to convert format XXX to YYY" errors. At least information about XXX and YYY should be easily accessible.

My fullstop is that I don't know how to convert PyCBitmap wrapper from pywin32 back into the system level object to call SetClipboardData WinAPI through ctypes. I also know that need to be careful about memory and check that it is not freed all of a sudden.

@mhils
Copy link
Contributor

mhils commented Nov 11, 2015

The current implementation of copy should handle memory allocation just fine for all cases. If you look at the way it works, there's no actual conversion (just copying which would need to be fixed to work with non-unicode lengths, probably by just converting unicode to bytes in the Python level).

@jaraco
Copy link
Contributor

jaraco commented Aug 8, 2016

See the jaraco.clipboard for an implementation that handles clipboard images on Windows.

@compute-e
Copy link

compute-e commented Nov 16, 2017

This would be a useful feature - allowing code to copy/get an image in the clipboard with Python on different operating systems, using this simple cross-platform clipboard module.
The jaraco.clipboard module described above (based on pyperclip and xerox) appears to do the job but only supports other formats on MacOS (HTML) and Windows (HTML and images).
So image data use is still Windows-only.

@stuaxo
Copy link

stuaxo commented Oct 14, 2020

Under Linux would format end up being a mimetype ?

See -
https://askubuntu.com/questions/759651/how-to-copy-an-image-to-the-clipboard-from-a-file-using-command-line

@Erriez
Copy link

Erriez commented Sep 18, 2023

I could not find a Python library to load/save clipboard to/from a file. Also this library cannot handle clipboard images, so I created win32clipboard_example.py for Windows only. It can copy screenshots or images to/from MSPaint and probably more applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants