python - 将 WandImageObject 转换为 QPixmap

标签 python python-3.x pyside2 wand

我正在使用 wand.image 库将 PdfPages 转换为图像
我需要将它们存储到缓存中,而不是将它们存储在本地,但我无法弄清楚如何将此 WandImage-Object 转换为像素图。 我已经阅读了 wand.image 的文档并做了很多研究,但自己无法解决问题..

当我将其保存在本地时,我的代码运行得非常好:

def pdf_to_jpeg(self, pdf_path):
    list_of_images = []
    wand_image_pdf = WI(filename=pdf_path, resolution=20)
    wand_image_jpegs = wand_image_pdf.convert("jpeg")
    for page_number, wand_image_jpeg in enumerate(wand_image_jpegs.sequence):
        jpeg = WI(image=wand_image_jpeg)
        jpeg.save(filename="../output/{0}.jpeg".format(str(page_number)))
        list_of_images.append("../output/{0}.jpeg".format(str(page_number)))
    return list_of_images

但是使用我的新代码:

def pdf_to_jpeg(self, pdf_path):
    list_of_images = []
    wand_image_pdf = WI(filename=pdf_path, resolution=20)
    wand_image_jpegs = wand_image_pdf.convert("jpeg")
    for page_number, wand_image_jpeg in enumerate(wand_image_jpegs.sequence):
        jpeg = WI(image=wand_image_jpeg)
        list_of_images.append(jpeg)
    return list_of_images

我收到错误:

QImage::QImage(), XPM is not supported
Traceback (most recent call last):

File "/Users/darjusch.schrand/PycharmProjects/PdfConverter/pdf_converter/main.py", line 33, in setup
    self.push_button_to_image = self.logic.create_push_button(self.list_of_images)

File "/Users/darjusch.schrand/PycharmProjects/PdfConverter/pdf_converter/logic/logic.py", line 21, in create_push_button
    pixmap = QPixmap(pic)
TypeError: The argument must be a sequence of strings.

最小工作示例:

from PySide2.QtGui import QPixmap, QIcon
from PySide2.QtCore import QSize
from PySide2.QtWidgets import QPushButton

class Logic:

def create_push_button(list_of_images):
    push_button_to_image = {}
    for pic in list_of_images:
        push_button = QPushButton()
        pixmap = QPixmap(pic)
        button_icon = QIcon(pixmap)
        push_button.setIcon(button_icon)
        push_button.setIconSize(QSize(100, 100))
        push_button.setCheckable(True)
        push_button_to_image[push_button] = pic
    return push_button_to_image

def pdf_to_jpeg(pdf_path):
    list_of_images = []
    wand_image_pdf = WI(filename=pdf_path, resolution=20)
    wand_image_jpegs = wand_image_pdf.convert("jpeg")
    for page_number, wand_image_jpeg in enumerate(wand_image_jpegs.sequence):
        jpeg = WI(image=wand_image_jpeg)
        list_of_images.append(jpeg)
    return list_of_images

pdf_to_jpeg("example.pdf")

example.pdf

最佳答案

顺序是:

from wand.image import Image

from PySide2 import QtCore, QtGui, QtWidgets


def pdf_to_qimages(pdf_path, resolution=200, fmt="png"):
    qimages = []
    with Image(filename=pdf_path, resolution=resolution) as pdf_im:
        for page in pdf_im.sequence:
            with Image(page) as page_image:
                qimage = QtGui.QImage()
                data = page_image.make_blob(format=fmt)
                qimage.loadFromData(data)
                qimages.append(qimage)
    return qimages


if __name__ == "__main__":
    import os
    import sys

    app = QtWidgets.QApplication(sys.argv)
    current_dir = os.path.dirname(os.path.realpath(__file__))
    pdf_path = os.path.join(current_dir, "sample.pdf")

    qimages = pdf_to_qimages(pdf_path, resolution=20, fmt="jpeg")

    w = QtWidgets.QListWidget(
        viewMode=QtWidgets.QListWidget.IconMode,
        resizeMode=QtWidgets.QListWidget.Adjust,
        movement=QtWidgets.QListView.Static,
        iconSize=QtCore.QSize(200, 400),
    )
    for qimage in qimages:
        pixmap = QtGui.QPixmap.fromImage(qimage)

        it = QtWidgets.QListWidgetItem()
        it.setIcon(QtGui.QIcon(pixmap))
        w.addItem(it)
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

关于python - 将 WandImageObject 转换为 QPixmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58325916/

相关文章:

python - 加速 Pandas - 加快将整列中的 Unix 时间戳更改为日期的速度

python - 运行 func(df) 创建新的数据帧并重命名它们

python - 子流程中的变量

python-3.x - Xcom pull 返回 `ti` 上的 NameError

python - 是否可以更改 QProgressBar 的高度?

python - IPython autoreload 为重复调用 Python2 super() 提供错误

python - 在 Matplotlib/seaborn 中绘制 Pandas 系列

python - 使用 tkinter 窗口时隐藏 python shell

python - QlistWidget 图标 - 加载速度更快

python - 类型错误 : A constant property cannot have a WRITE method or a NOTIFY signal