I'm trying to use the tkinter
solution for obtaining clipboard image data copied from GIMP, but cannot make it work, saving the data to file:
from tkinter import Tk
r = Tk()
r.withdraw()
clip = r.clipboard_get(type="image/png")
r.update()
r.destroy()
with open("testbytes.png", mode="bw+") as f:
f.write(clip.encode())
When I try to open the testbytes.png file, the Image Viewer reports a fatal error, not a PNG file. I obtained the type
parameter for the clipboard_get()
call with r.selection_get(selection='CLIPBOARD', type='TARGETS')
, which returned:
'TIMESTAMP TARGETS MULTIPLE SAVE_TARGETS image/png image/bmp image/x-bmp image/x-MS-bmp image/x-icon image/x-ico image/x-win-bitmap image/vnd.microsoft.icon application/ico image/ico image/icon text/ico image/tiff image/jpeg '
I think the format of the data on the clipboard is PNG. I've also tried JPEG, BMP and TIFF, but they result in similar errors.
What am I doing wrong?