Advertisement
asweigart

pyperclip image linux gtk

Aug 2nd, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import gi
  2. gi.require_version("Gtk", "3.0")
  3. from gi.repository import Gtk, Gdk, GdkPixbuf
  4. from PIL import Image
  5.  
  6. def send_to_clipboard(image):
  7. # Create a buffer in memory to hold the image data
  8. buffer = io.BytesIO()
  9. image.save(buffer, format="PNG")
  10. loaded_image = GdkPixbuf.PixbufLoader.new_with_type('png')
  11. loaded_image.write(buffer.getvalue())
  12. pixbuf = loaded_image.get_pixbuf()
  13. loaded_image.close()
  14.  
  15. # Get the clipboard
  16. clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
  17.  
  18. # Set the clipboard image
  19. clipboard.set_image(pixbuf)
  20. clipboard.store()
  21.  
  22. # Open an image using Pillow
  23. image_path = 'example.png'
  24. image = Image.open(image_path)
  25.  
  26. # Send the image to clipboard
  27. send_to_clipboard(image)
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement