简体   繁体   English

ImageIO PSD 到 PyQt QImage

[英]ImageIO PSD to PyQt QImage

I am trying to display a photoshop image in PyQt.我正在尝试在 PyQt 中显示 Photoshop 图像。 I have been using the python imageio module to load images and through a series of steps convert them to a QImage as below.我一直在使用 python imageio 模块来加载图像,并通过一系列步骤将它们转换为 QImage,如下所示。 This works for many different things such as.exr, .hdr, etc. However when trying to load a.psd I get strange results like rainbow colors all over the place and a lot of noise.这适用于许多不同的东西,例如 .exr、.hdr 等。但是,当尝试加载 a.psd 时,我会得到奇怪的结果,比如彩虹 colors 到处都是,而且噪音很大。 It seems that something is going wrong within the conversion process, but I cannot find enough documentation online from FreeImage, ImageIO or PIL to explain what is going wrong.转换过程中似乎出现了问题,但我无法从 FreeImage、ImageIO 或 PIL 在线找到足够的文档来解释问题所在。

Here's a simple example of the conversion process I am using:这是我正在使用的转换过程的简单示例:

import os
from PIL import Image
from PyQt5 import QtGui
import imageio, numpy

path = 'path/to/image.psd'

imio = imageio.imread(path)
imio_np = numpy.uint8(imio) # convert from 16bpc to 8bpc
im = Image.fromarray(imio_np)
im.convert('RGB')
data = im.tobytes('raw', 'RGB')
qim = QtGui.QImage(data, im.size[0], im.size[1], QtGui.QImage.Format_RGB888)

# then just apply that qim to a QLabel

Ok turns out this is easier than I thought.好吧,事实证明这比我想象的要容易。 I found the answer here: Convert 16-bit Tiff image to 8-bit RGB .我在这里找到了答案: Convert 16-bit Tiff image to 8-bit RGB The problem was just converting from 16bpc to 8bpc, and I'll post that here in case anyone else has this issue.问题只是从 16bpc 转换为 8bpc,我会在这里发布,以防其他人有这个问题。

import os
from PIL import Image
from PyQt5 import QtGui
import imageio, numpy

path = 'path/to/image.psd'

imio = imageio.imread(path)
if imio.dtype == 'uint16': # 16bpc
    imio = (imio >> 8).astype('uint8') # convert to 8bp with a bit-shift

im = Image.fromarray(imio)
im.convert('RGB')
data = im.tobytes('raw', 'RGB')
qim = QtGui.QImage(data, im.size[0], im.size[1], QtGui.QImage.Format_RGB888)

# then just apply that qim to a QLabel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM