From Vasilis.Vlachoudis at cern.ch Fri Mar 1 05:52:19 2024 From: Vasilis.Vlachoudis at cern.ch (Vasilis Vlachoudis) Date: Fri, 1 Mar 2024 10:52:19 +0000 Subject: [Tkinter-discuss] Copy image to clipboard In-Reply-To: <20240228115224.e3408a2fc51a3188f8b96c39@web.de> References: <20240228112953.8829d15a14ddb3540d72f059@web.de> <20240228115224.e3408a2fc51a3188f8b96c39@web.de> Message-ID: Thank you Michael, it works nicely! ________________________________ From: Tkinter-discuss on behalf of Michael Lange via Tkinter-discuss Sent: Wednesday 28 February 2024 11:52 To: tkinter-discuss at python.org Subject: Re: [Tkinter-discuss] Copy image to clipboard Hi again, for the archives, here is a simplified and more generalized example of the copy_image_to_clipboard() function that works without having to "detour" with Canvas.postscript(): def copy_image_to_clipboard(filename): image_buffer = io.BytesIO() image = Image.open(filename) image.save(image_buffer, format="PNG") image_buffer.seek(0) root.clipboard_clear() root.clipboard_append(image_buffer.getvalue(), type="image/png") This seems to work well here under X11. Have a nice day, Michael On Wed, 28 Feb 2024 11:29:53 +0100 Michael Lange via Tkinter-discuss wrote: > On Thu, 22 Feb 2024 10:22:06 +0000 > Vasilis Vlachoudis wrote: > > > Dear all, > > > > I am trying in X11 to copy an image to clipboard but with out success. > (...) > > Hi Vasilis, > > sorry for the belated reply. > > This is tricky, I tried several suggestions found in the web to no > avail. Finally for once an AI engine proved useful and provided me with > an almost working solution which it seems I could fix with a little > try-and-error :-) > > Here's the fixed AI code example: > > ########################################## > > import tkinter as tk > from PIL import Image, ImageTk > import io > > root = tk.Tk() > root.geometry("700x500") > root.title("Copy a Picture from Tkinter Canvas to Clipboard") > canvas = tk.Canvas(root, width=400, height=400) > canvas.pack() > > image = Image.open("image.jpg") > image_tk = ImageTk.PhotoImage(image) > canvas.create_image(0, 0, anchor="nw", image=image_tk) > > def copy_image_to_clipboard(): > # Retrieve the image from the canvas > canvas_image = canvas.postscript() > > # Create an in-memory file-like object > image_buffer = io.BytesIO() > > # Save the canvas image to the buffer in PNG format > image = Image.open(io.BytesIO(canvas_image.encode('utf-8'))) > image.save(image_buffer, format="PNG") > image_buffer.seek(0) > > # Copy the image to the clipboard > root.clipboard_clear() > root.clipboard_append(image_buffer.getvalue(), type="image/png") > > copy_button = tk.Button(root, text="Copy Image", > command=copy_image_to_clipboard) copy_button.pack() > > root.mainloop() > > ########################################## > > The trick is apparently to change format="image/png" (suggested by our > artificial friend) to type="image/png" in clipboard_append(). With > format="image/png" when trying to paste the clipboard into lowriter I > got only a mess of characters. > > Have a nice day, > > Michael > > > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. You! What PLANET is this! -- McCoy, "The City on the Edge of Forever", stardate 3134.0 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss at python.org https://mail.python.org/mailman/listinfo/tkinter-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: