I have created a gif using pillow from some images and I am trying to copy this gif stored as a BytesIO object to the clipboard to save onto a presentation as a gif image. code below; where imageList = [] is a list of image file names.
any method of copying a gif to the clipboard from python would be helpful?! thanks
def copyGif(self):
imageList = []
gif = BytesIO()
imageList[0].save(gif, format='GIF', save_all=True, append_images=imageList[1:], optimize=False, duration=1000,
loop=0)
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, gif.getvalue())
win32clipboard.CloseClipboard()
Try it:
import sys
from PyQt5.Qt import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(50, 50, 600, 600)
# Create a URL.
url1 = QUrl.fromLocalFile('Loader.gif')
url2 = QUrl.fromLocalFile('animated-dancing-image-0028.gif')
# Create MIME data with URL.
mime_data = QMimeData()
mime_data.setUrls([url1, url2])
self.labelList = [ i for i in range(len([url1, url2]))]
# Copy the MIME data to the clipboard.
clipboard = QApplication.clipboard()
clipboard.setMimeData(mime_data)
cm = clipboard.mimeData()
for i, m in enumerate(cm.urls()):
self.labelList[i] = QMovie(m.toLocalFile())
self.labelList[i].frameChanged.connect(self.repaint)
self.labelList[i].start()
def paintEvent(self, event):
currentFrame1 = self.labelList[0].currentPixmap()
currentFrame2 = self.labelList[1].currentPixmap()
frameRect2 = currentFrame2.rect()
frameRect2.moveCenter(self.rect().center())
if frameRect2.intersects(event.rect()):
painter = QPainter(self)
painter.drawPixmap(frameRect2.left(), frameRect2.top(), currentFrame2)
frameRect1 = currentFrame1.rect()
frameRect1.moveCenter(self.rect().center())
if frameRect1.intersects(event.rect()):
painter.drawPixmap(frameRect1.left(), frameRect1.top(), currentFrame1)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
QTimer.singleShot(10000, app.quit)
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.