Advertisement
asweigart

Copy Image linux

Aug 2nd, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. from PyQt5.QtGui import QImage, QPixmap
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtCore import Qt
  4. from PIL import Image
  5. import sys
  6.  
  7. def send_to_clipboard(image):
  8. # Convert the Pillow image to a QByteArray
  9. buffer = io.BytesIO()
  10. image.save(buffer, format="PNG")
  11. qimage = QImage.fromData(buffer.getvalue())
  12.  
  13. # Convert the QImage to a QPixmap
  14. pixmap = QPixmap.fromImage(qimage)
  15.  
  16. # Get the clipboard and set the QPixmap as its content
  17. app = QApplication.instance()
  18. clipboard = app.clipboard()
  19. clipboard.setPixmap(pixmap, mode=Qt.Clipboard)
  20.  
  21. # Open an image using Pillow
  22. image_path = 'example.png'
  23. image = Image.open(image_path)
  24.  
  25. # Create a Qt application instance (only if one doesn't exist already)
  26. app = QApplication(sys.argv) if not QApplication.instance() else QApplication.instance()
  27.  
  28. # Send the image to clipboard
  29. send_to_clipboard(image)
  30.  
  31. # (Optional) Run the Qt event loop if this is a standalone script
  32. # app.exec_()
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement