Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtGui import QImage, QPixmap
- from PyQt5.QtWidgets import QApplication
- from PyQt5.QtCore import Qt
- from PIL import Image
- import sys
- def send_to_clipboard(image):
- # Convert the Pillow image to a QByteArray
- buffer = io.BytesIO()
- image.save(buffer, format="PNG")
- qimage = QImage.fromData(buffer.getvalue())
- # Convert the QImage to a QPixmap
- pixmap = QPixmap.fromImage(qimage)
- # Get the clipboard and set the QPixmap as its content
- app = QApplication.instance()
- clipboard = app.clipboard()
- clipboard.setPixmap(pixmap, mode=Qt.Clipboard)
- # Open an image using Pillow
- image_path = 'example.png'
- image = Image.open(image_path)
- # Create a Qt application instance (only if one doesn't exist already)
- app = QApplication(sys.argv) if not QApplication.instance() else QApplication.instance()
- # Send the image to clipboard
- send_to_clipboard(image)
- # (Optional) Run the Qt event loop if this is a standalone script
- # app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement