@@ -129,13 +129,15 @@ def align8to32(bytes, width, mode):
129129def _toqclass_helper (im ):
130130 data = None
131131 colortable = None
132+ exclusive_fp = False
132133
133134 # handle filename, if given instead of image name
134135 if hasattr (im , "toUtf8" ):
135136 # FIXME - is this really the best way to do this?
136137 im = str (im .toUtf8 (), "utf-8" )
137138 if isPath (im ):
138139 im = Image .open (im )
140+ exclusive_fp = True
139141
140142 qt_format = QImage .Format if qt_version == "6" else QImage
141143 if im .mode == "1" :
@@ -158,10 +160,15 @@ def _toqclass_helper(im):
158160 data = im .tobytes ("raw" , "BGRA" )
159161 format = qt_format .Format_ARGB32
160162 else :
163+ if exclusive_fp :
164+ im .close ()
161165 raise ValueError (f"unsupported image mode { repr (im .mode )} " )
162166
163- __data = data or align8to32 (im .tobytes (), im .size [0 ], im .mode )
164- return {"data" : __data , "im" : im , "format" : format , "colortable" : colortable }
167+ size = im .size
168+ __data = data or align8to32 (im .tobytes (), size [0 ], im .mode )
169+ if exclusive_fp :
170+ im .close ()
171+ return {"data" : __data , "size" : size , "format" : format , "colortable" : colortable }
165172
166173
167174if qt_is_installed :
@@ -183,8 +190,8 @@ def __init__(self, im):
183190 self .__data = im_data ["data" ]
184191 super ().__init__ (
185192 self .__data ,
186- im_data ["im" ]. size [0 ],
187- im_data ["im" ]. size [1 ],
193+ im_data ["size" ] [0 ],
194+ im_data ["size" ] [1 ],
188195 im_data ["format" ],
189196 )
190197 if im_data ["colortable" ]:
@@ -198,8 +205,8 @@ def toqimage(im):
198205def toqpixmap (im ):
199206 # # This doesn't work. For now using a dumb approach.
200207 # im_data = _toqclass_helper(im)
201- # result = QPixmap(im_data['im']. size[0], im_data['im']. size[1])
202- # result.loadFromData(im_data[' data' ])
208+ # result = QPixmap(im_data[" size"] [0], im_data[" size"] [1])
209+ # result.loadFromData(im_data[" data" ])
203210 # Fix some strange bug that causes
204211 if im .mode == "RGB" :
205212 im = im .convert ("RGBA" )
0 commit comments