将枕头Gif复制到Windows剪贴板

时间:2019-12-17 18:51:25

标签: python python-3.x python-imaging-library gif

我已经使用枕头从一些图像中创建了一个gif,并且试图将作为BytesIO对象存储的该gif复制到剪贴板,以作为gif图像保存到演示文稿中。下面的代码;其中imageList = []是图像文件名的列表。

从python复制gif到剪贴板的任何方法都会有用吗?谢谢

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()

1 个答案:

答案 0 :(得分:1)

尝试一下:

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_())

enter image description here

enter image description here