提交 0cda175b 编写于 作者: T Tatiana Malygina 提交者: Kentaro Wada

add fix for PyQt4+python 2.7 to avoid window freeze.

When imageData cannot be read with PyQt4, the alert window is shown.
To avoid this, I do conversion from jpg to png (or from other
unsupported format to png).
上级 24a76ed8
import functools
import io
import os.path
import re
import warnings
import webbrowser
import PIL.Image
from qtpy import QtCore
from qtpy.QtCore import Qt
from qtpy import QtGui
......@@ -936,6 +939,16 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
for item, shape in self.labelList.itemsToShapes:
item.setCheckState(Qt.Checked if value else Qt.Unchecked)
def convertImageDataToPng(self, imageData):
if imageData is None:
return
img = PIL.Image.open(io.BytesIO(imageData))
with io.BytesIO() as imgBytesIO:
img.save(imgBytesIO, "PNG")
imgBytesIO.seek(0)
data = imgBytesIO.read()
return data
def loadFile(self, filename=None):
"""Load the specified file, or the last opened file if None."""
# changing fileListWidget loads file
......@@ -965,6 +978,10 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
# FIXME: PyQt4 installed via Anaconda fails to load JPEG
# and JSON encoded images.
# https://github.com/ContinuumIO/anaconda-issues/issues/131
if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
# tries to read image with PIL and convert it to PNG
self.labelFile.imageData = self.convertImageDataToPng(
self.labelFile.imageData)
if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
raise LabelFileError(
'Failed loading image data from label file.\n'
......@@ -991,6 +1008,8 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
if self.imageData is not None:
# the filename is image not JSON
self.imagePath = filename
if QtGui.QImage.fromData(self.imageData).isNull():
self.imageData = self.convertImageDataToPng(self.imageData)
self.labelFile = None
image = QtGui.QImage.fromData(self.imageData)
if image.isNull():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册