Skip to content

Commit

Permalink
Ensure file is closed if it is opened by ImageQt.ImageQt
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 11, 2021
1 parent 1722b8d commit a447e92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
13 changes: 11 additions & 2 deletions Tests/test_imageqt.py
Expand Up @@ -4,11 +4,14 @@

from .helper import hopper

pytestmark = pytest.mark.skipif(
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
)

if ImageQt.qt_is_installed:
from PIL.ImageQt import qRgba


@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
def test_rgb():
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
# typedef QRgb
Expand Down Expand Up @@ -38,7 +41,13 @@ def checkrgb(r, g, b):
checkrgb(0, 0, 255)


@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
def test_image():
for mode in ("1", "RGB", "RGBA", "L", "P"):
ImageQt.ImageQt(hopper(mode))


def test_closed_file():
with pytest.warns(None) as record:
ImageQt.ImageQt("Tests/images/hopper.gif")

assert not record
19 changes: 13 additions & 6 deletions src/PIL/ImageQt.py
Expand Up @@ -129,13 +129,15 @@ def align8to32(bytes, width, mode):
def _toqclass_helper(im):
data = None
colortable = None
exclusive_fp = False

# handle filename, if given instead of image name
if hasattr(im, "toUtf8"):
# FIXME - is this really the best way to do this?
im = str(im.toUtf8(), "utf-8")
if isPath(im):
im = Image.open(im)
exclusive_fp = True

qt_format = QImage.Format if qt_version == "6" else QImage
if im.mode == "1":
Expand All @@ -158,10 +160,15 @@ def _toqclass_helper(im):
data = im.tobytes("raw", "BGRA")
format = qt_format.Format_ARGB32
else:
if exclusive_fp:
im.close()
raise ValueError(f"unsupported image mode {repr(im.mode)}")

__data = data or align8to32(im.tobytes(), im.size[0], im.mode)
return {"data": __data, "im": im, "format": format, "colortable": colortable}
size = im.size
__data = data or align8to32(im.tobytes(), size[0], im.mode)
if exclusive_fp:
im.close()
return {"data": __data, "size": size, "format": format, "colortable": colortable}


if qt_is_installed:
Expand All @@ -183,8 +190,8 @@ def __init__(self, im):
self.__data = im_data["data"]
super().__init__(
self.__data,
im_data["im"].size[0],
im_data["im"].size[1],
im_data["size"][0],
im_data["size"][1],
im_data["format"],
)
if im_data["colortable"]:
Expand All @@ -198,8 +205,8 @@ def toqimage(im):
def toqpixmap(im):
# # This doesn't work. For now using a dumb approach.
# im_data = _toqclass_helper(im)
# result = QPixmap(im_data['im'].size[0], im_data['im'].size[1])
# result.loadFromData(im_data['data'])
# result = QPixmap(im_data["size"][0], im_data["size"][1])
# result.loadFromData(im_data["data"])
# Fix some strange bug that causes
if im.mode == "RGB":
im = im.convert("RGBA")
Expand Down

0 comments on commit a447e92

Please sign in to comment.