저는 tif 스택으로 작업 중이며 QImage
은 45도 각도로 일부 이미지를 비뚤어지게 보입니다. Matplotlib은 두 테스트 케이스 모두에서 문제없이 이미지를 표시 할 수 있습니다 (두 개의 tif 스택에 대한 링크가 아래에 나와 있습니다). 그래서 어딘가에서 배열을 망쳤다고 생각하지 않습니다. 여기QImage는 일부 이미지를 비뚤어 지지만 다른 이미지는 왜곡하지 않습니다.
이 동작 예이다 :
import matplotlib.pyplot as plt
import sys
from PIL import Image
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import (QMainWindow, QApplication, QVBoxLayout,
QWidget, QFileDialog, QGraphicsPixmapItem, QGraphicsView,
QGraphicsScene)
import numpy as np
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# set up a widget to hold a pixmap
wid = QWidget(self)
self.setCentralWidget(wid)
self.local_grview = QGraphicsView()
self.local_scene = QGraphicsScene()
vbox = QVBoxLayout()
self.local_grview.setScene(self.local_scene)
vbox.addWidget(self.local_grview)
wid.setLayout(vbox)
# load and display the image
self.loadImage()
# display the widget
self.show()
# also use matplotlib to display the data as it should appear
plt.imshow(self.dataUint8[0], cmap='gray')
plt.show()
def loadImage(self):
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')[0]
# use the tif reader to read in the tif stack
self.data = self.readTif(fname)
# convert to uint8 for display
self.dataUint8 = self.uint8Convert(self.data)
###############################################################################################################################
# I suspect this is where something goes wrong
###############################################################################################################################
# create a QImage object
self.im = QImage(self.dataUint8[0], self.dataUint8[0].shape[1], self.dataUint8[0].shape[0], QImage.Format_Grayscale8)
# if we save using self.im.save() we also have a skewed image
###############################################################################################################################
# send the QImage object to the pixmap generator
self.pixmap = QPixmap(self.im)
self.pixMapItem = QGraphicsPixmapItem(self.pixmap, None)
self.local_scene.addItem(self.pixMapItem)
def readTif(self, filename): # use this function to read in a tif stack and return a 3D numpy array
# read in the file
stack = Image.open(filename)
# extract each frame from the file and store in the frames variable
frames = []
i = 0
while True:
try:
stack.seek(i) # move to the ith position in the stack
frames.append(np.array(stack))
i += 1
except EOFError:
# end of stack
break
del stack # probably unnecessary but this presumably saves a bit of memory
return frames
def uint8Convert(self, frames): # use this function to scale a 3D numpy array of floats to 0-255 so it plays well with Qt methods
# convert float array to uint8 array
if np.min(frames)<0:
frames_uint8 = [np.uint8((np.array(frames[i]) - np.min(frames[i]))/np.max(frames[i])*255) for i in range(np.shape(frames)[0])]
else:
frames_uint8 = [np.uint8(np.array(frames[i])/np.max(frames[i])*255) for i in range(np.shape(frames)[0])]
return frames_uint8
if __name__=='__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
여기서 출력의 스크린 샷이다 (참고 :이 예에서는 단순화를위한 TIF 스택의 첫번째 이미지 표시)
Qimage하기 matplotlib VS를
올바르게 표시되는 tif 스택에 대한 링크는 다음과 같습니다.
표시 될 때 53,691,363,210https://drive.google.com/uc?export=download&id=0B9EG5AHWC9qzX3NrNTJRb2toV2c
그리고 여기가 비뚤어이되는 TIF 스택에 대한 링크입니다 :
https://drive.google.com/uc?export=download&id=0B9EG5AHWC9qzbFB4TDU4c2x1OE0
QImage
이 많이 주시면 감사하겠습니다이 이미지를 기울 이유를 이해 어떤 도움. 두 개의 tif 스택 사이의 유일한 주요 차이점은 비뚤어지게 표시되는 이미지에는 패딩 된 검은 색 영역 (제로)이있어 이미지를 더 크게 만들 수 있다는 것입니다.
업데이트 : 문제의 이미지를 1024x1024 또는 512x512 또는 1023x1024로 자르면 QImage
이 제대로 표시되지만 1024x1023 디스플레이로 자르기가 비뚤어 짐을 발견했습니다. 따라서 x (가로) 길이는 QImage
이 예상대로 처리하려면 2의 제곱수 여야합니다. 그것은 우스운 제한이다! 내가 이해하지 못하는 무언가가있을거야. 물론 임의의 모양의 배열을 처리 할 수있는 방법이 있습니다.
은 ... 나는 가정, 원칙적으로, 하나는 먼저 이미지에 왜곡을 적용하고 단지 QImage
가 ... 다시 뒤틀림을 방지 할 수있다 (<를 ==이 솔루션의 팬이가)
@ echumoro 동의합니다! 그 파일 호스팅 사이트를 처음 사용했습니다. 나는 그들을 다시 사용하지 않을 것이다. 직접 다운로드가되도록 링크를 업데이트했습니다. 나는 또한 그 파일들이 얼마나 방대한지 알지 못했다. 새로운 것들은 훨씬 가볍다. 다운로드 시간은 2 ~ 2 분 정도 소요됩니다. – onjre
감사합니다. FWIW, 문제를 재현 할 수는 있지만 현재로서는 원인에 대한 통찰력이 없습니다. – ekhumoro
QImage (fname)를 사용하십시오. Tiff는 QImage에서 지원됩니다. – eyllanesc