gpt4 book ai didi

python - 将 WandImageObject 转换为 QPixmap

转载 作者:行者123 更新时间:2023-12-01 00:29:08 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com