Re: Copy image to clipboard
Vasilis Vlachoudis <[email protected]> Fri, 1 Mar 2024 10:52:19 +0000
| Newsgroups | gmane.comp.python.tkinter |
|---|---|
| Message-ID | <ZRAP278MB0221B601F2EE27BC286C3670845E2@ZRAP278MB0221.CHEP278.PROD.OUTLOOK.COM> |
--===============4567122108927920833== Content-Language: en-IE Content-Type: multipart/alternative; boundary="_000_ZRAP278MB0221B601F2EE27BC286C3670845E2ZRAP278MB0221CHEP_" --_000_ZRAP278MB0221B601F2EE27BC286C3670845E2ZRAP278MB0221CHEP_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Thank you Michael, it works nicely! ________________________________ From: Tkinter-discuss <tkinter-discuss-bounces+vasilis.vlachoudis=3Dcern.ch= @python.org> on behalf of Michael Lange via Tkinter-discuss <tkinter-discus= [email protected]> Sent: Wednesday 28 February 2024 11:52 To: [email protected] <[email protected]> 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 =3D io.BytesIO() image =3D Image.open(filename) image.save(image_buffer, format=3D"PNG") image_buffer.seek(0) root.clipboard_clear() root.clipboard_append(image_buffer.getvalue(), type=3D"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 <[email protected]> wrote: > On Thu, 22 Feb 2024 10:22:06 +0000 > Vasilis Vlachoudis <[email protected]> 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 =3D tk.Tk() > root.geometry("700x500") > root.title("Copy a Picture from Tkinter Canvas to Clipboard") > canvas =3D tk.Canvas(root, width=3D400, height=3D400) > canvas.pack() > > image =3D Image.open("image.jpg") > image_tk =3D ImageTk.PhotoImage(image) > canvas.create_image(0, 0, anchor=3D"nw", image=3Dimage_tk) > > def copy_image_to_clipboard(): > # Retrieve the image from the canvas > canvas_image =3D canvas.postscript() > > # Create an in-memory file-like object > image_buffer =3D io.BytesIO() > > # Save the canvas image to the buffer in PNG format > image =3D Image.open(io.BytesIO(canvas_image.encode('utf-8'))) > image.save(image_buffer, format=3D"PNG") > image_buffer.seek(0) > > # Copy the image to the clipboard > root.clipboard_clear() > root.clipboard_append(image_buffer.getvalue(), type=3D"image/png") > > copy_button =3D tk.Button(root, text=3D"Copy Image", > command=3Dcopy_image_to_clipboard) copy_button.pack() > > root.mainloop() > > ########################################## > > The trick is apparently to change format=3D"image/png" (suggested by our > artificial friend) to type=3D"image/png" in clipboard_append(). With > format=3D"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 > [email protected] > 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 [email protected] https://mail.python.org/mailman/listinfo/tkinter-discuss --_000_ZRAP278MB0221B601F2EE27BC286C3670845E2ZRAP278MB0221CHEP_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable <html> <head> <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"= > <style type=3D"text/css" style=3D"display:none;"> P {margin-top:0;margin-bo= ttom:0;} </style> </head> <body dir=3D"ltr"> <div class=3D"elementToProof" style=3D"font-family: Aptos, Aptos_EmbeddedFo= nt, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> Thank you Michael,</div> <div class=3D"elementToProof" style=3D"font-family: Aptos, Aptos_EmbeddedFo= nt, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> <br> </div> <div class=3D"elementToProof" style=3D"font-family: Aptos, Aptos_EmbeddedFo= nt, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; c= olor: rgb(0, 0, 0);"> it works nicely!</div> <div id=3D"Signature"></div> <div id=3D"appendonsend"></div> <hr style=3D"display:inline-block;width:98%" tabindex=3D"-1"> <div id=3D"divRplyFwdMsg" dir=3D"ltr"><font face=3D"Calibri, sans-serif" st= yle=3D"font-size:11pt" color=3D"#000000"><b>From:</b> Tkinter-discuss <t= [email protected]> on behal= f of Michael Lange via Tkinter-discuss <[email protected]><b= r> <b>Sent:</b> Wednesday 28 February 2024 11:52<br> <b>To:</b> [email protected] <[email protected]><br= > <b>Subject:</b> Re: [Tkinter-discuss] Copy image to clipboard</font> <div> </div> </div> <div class=3D"BodyFragment"><font size=3D"2"><span style=3D"font-size:11pt;= "> <div class=3D"PlainText">Hi again,<br> <br> for the archives, here is a simplified and more generalized example of the<= br> copy_image_to_clipboard() function that works without having to "detou= r"<br> with Canvas.postscript():<br> <br> def copy_image_to_clipboard(filename):<br> image_buffer =3D io.BytesIO()<br> <br> image =3D Image.open(filename)<br> image.save(image_buffer, format=3D"PNG")<br> image_buffer.seek(0)<br> <br> root.clipboard_clear()<br> root.clipboard_append(image_buffer.getvalue(), type=3D&q= uot;image/png")<br> <br> This seems to work well here under X11.<br> <br> Have a nice day,<br> <br> Michael<br> <br> <br> On Wed, 28 Feb 2024 11:29:53 +0100<br> Michael Lange via Tkinter-discuss <[email protected]> wrote:= <br> <br> > On Thu, 22 Feb 2024 10:22:06 +0000<br> > Vasilis Vlachoudis <[email protected]> wrote:<br> ><br> > > Dear all,<br> > ><br> > > I am trying in X11 to copy an image to clipboard but with out suc= cess.<br> > (...)<br> ><br> > Hi Vasilis,<br> ><br> > sorry for the belated reply.<br> ><br> > This is tricky, I tried several suggestions found in the web to no<br> > avail. Finally for once an AI engine proved useful and provided me wit= h<br> > an almost working solution which it seems I could fix with a little<br= > > try-and-error :-)<br> ><br> > Here's the fixed AI code example:<br> ><br> > ##########################################<br> ><br> > import tkinter as tk<br> > from PIL import Image, ImageTk<br> > import io<br> ><br> > root =3D tk.Tk()<br> > root.geometry("700x500")<br> > root.title("Copy a Picture from Tkinter Canvas to Clipboard"= )<br> > canvas =3D tk.Canvas(root, width=3D400, height=3D400)<br> > canvas.pack()<br> ><br> > image =3D Image.open("image.jpg")<br> > image_tk =3D ImageTk.PhotoImage(image)<br> > canvas.create_image(0, 0, anchor=3D"nw", image=3Dimage_tk)<b= r> ><br> > def copy_image_to_clipboard():<br> > # Retrieve the image from the canvas<br> > canvas_image =3D canvas.postscript()<br> ><br> > # Create an in-memory file-like object<br> > image_buffer =3D io.BytesIO()<br> ><br> > # Save the canvas image to the buffer in PNG f= ormat<br> > image =3D Image.open(io.BytesIO(canvas_image.e= ncode('utf-8')))<br> > image.save(image_buffer, format=3D"PNG&qu= ot;)<br> > image_buffer.seek(0)<br> ><br> > # Copy the image to the clipboard<br> > root.clipboard_clear()<br> > root.clipboard_append(image_buffer.getvalue(),= type=3D"image/png")<br> ><br> > copy_button =3D tk.Button(root, text=3D"Copy Image",<br> > command=3Dcopy_image_to_clipboard) copy_button.pack()<br> ><br> > root.mainloop()<br> ><br> > ##########################################<br> ><br> > The trick is apparently to change format=3D"image/png" (sugg= ested by our<br> > artificial friend) to type=3D"image/png" in clipboard_append= (). With<br> > format=3D"image/png" when trying to paste the clipboard into= lowriter I<br> > got only a mess of characters.<br> ><br> > Have a nice day,<br> ><br> > Michael<br> ><br> ><br> ><br> > _______________________________________________<br> > Tkinter-discuss mailing list<br> > [email protected]<br> > <a href=3D"https://mail.python.org/mailman/listinfo/tkinter-discuss">h= ttps://mail.python.org/mailman/listinfo/tkinter-discuss</a><br> <br> <br> <br> .-.. .. ...- . .-.. --- -. --. .- -. -..  = ; .--. .-. --- ... .--. . .-.<br> <br> You! What PLANET is this!<br> &nb= sp; -- McCoy, "The City on the Edge of Forever", star= date<br> 3134.0<br> _______________________________________________<br> Tkinter-discuss mailing list<br> [email protected]<br> <a href=3D"https://mail.python.org/mailman/listinfo/tkinter-discuss">https:= //mail.python.org/mailman/listinfo/tkinter-discuss</a><br> </div> </span></font></div> </body> </html> --_000_ZRAP278MB0221B601F2EE27BC286C3670845E2ZRAP278MB0221CHEP_-- --===============4567122108927920833== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Tkinter-discuss mailing list [email protected] https://mail.python.org/mailman/listinfo/tkinter-discuss --===============4567122108927920833==--