0

I've spent the past few hours trying to find a definitive answer, but all I have come across is a bunch of inconclusive, or conflicting, or seemingly-incompatible-with-python information.

output = io.BytesIO()
    image.save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()

    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
    win32clipboard.CloseClipboard()

The above method I found lets me paste the image into the application Discord for instance, but causes the transparency to be lost as I expected. Yet replacing any of the information passed in to those methods gets me invalid data.

This question from 2012 is exactly what I am looking for, but is a mix of inconclusive (from what I understand currently) info and seems to conflict with what else I've read.

Dreus
  • 1
  • 1
  • Which module is providing `image.save()`? – Mark Setchell Jan 31 '19 at 19:22
  • Ah, sorry! Had a feeling I missed something. It's Pillow @MarkSetchell – Dreus Jan 31 '19 at 19:41
  • I have been doing some experimenting and found some rather odd things. I loaded an RGBA PNG file into Pillow and saved it as a BMP file and macOS couldn't detect any transparency in the BMP file. Changing nothing else, I resized the image to a multiple of 4 pixels in each direction (it was 161x161 pixels and I made it 160x160 pixels with `im - im.resize((160,160))`) and macOS suddenly was able to detect transparency. I don't have Windows to test this on... – Mark Setchell Jan 31 '19 at 20:49
  • Hmm, just did the same and it's still a no go. The background of one image that is supposed to be transparent turns black, another to white for some reason. I really appreciate your help thus far, though. @MarkSetchell – Dreus Jan 31 '19 at 21:01
  • Why are you saving it as a BMP? Although the BMP spec technically supports transparency, support for this is uncommon. Saving it as a PNG would be the first step in the right direction. – Man Eating Monkey Feb 01 '19 at 05:22
  • @ManEatingMonkey AFAIK the Windows Clipboard only accepts BMP/DIB format. – Mark Setchell Feb 01 '19 at 07:09
  • 1
    According to this https://stackoverflow.com/a/46424800/395685 PNG is possible. – Man Eating Monkey Feb 03 '19 at 23:37
  • Ah, I went over that post myself during my long search before I posted this question. Couldn't figure out how to work it into Python code (if possible) @ManEatingMonkey. I ended up figuring out a way to bypass the clipboard entirely for the time being, until I become more experienced at the very least. – Dreus Feb 08 '19 at 00:07
  • Yea, not sure how the clipboard in Python works, but basically you have to figure out how to put a raw byte array on the clipboard, with a custom string as clipboard ID. Once you got that, you can do all the stuff described in my answer there. Do note that DIB isn't just BMP; it's basically 32-bit BMP with 'compression' type BITFIELDS, and, notably, without a BMP _file_ header (only the _image_ header). – Nyerguds Feb 14 '19 at 14:47

1 Answers1

0

Correct me if I am wrong but from what I have read, PNG is not supported in the windows clipboard, however, you can create a PNG format that works for some programs. The following code works at least in word and chrome.

output = BytesIO()
final.save(output, "PNG")
data = output.getvalue()
output.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.RegisterClipboardFormat('PNG'),data)
win32clipboard.CloseClipboard()

where ¨final¨ is a PIL image object

alex
  • 1
  • 1