1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

changes because of slow loading of bottoms

Because ImageTK.PhotoImage and Image.resizing have problems witht zeros in alphachannel, i masked the image to eliminate the 0s
This commit is contained in:
cardset 2022-01-24 14:48:17 +01:00
parent 54e34bb4c0
commit dc98900763
2 changed files with 57 additions and 28 deletions

View file

@ -188,34 +188,37 @@ class Images:
cs_dir=self.cs.dir, fname=name)) cs_dir=self.cs.dir, fname=name))
if progress: if progress:
progress.update(step=1) progress.update(step=1)
# load bottoms
for i in range(self.cs.nbottoms): if not USE_PIL:
name = "bottom%02d" % (i + 1) # load bottoms
bottom = self.__loadBottom(name, color='black') for i in range(self.cs.nbottoms):
if bottom is not None: name = "bottom%02d" % (i + 1)
self._bottom_positive.append(bottom) bottom = self.__loadBottom(name, color='black')
if progress: if bottom is not None:
progress.update(step=pstep) self._bottom_positive.append(bottom)
# load negative bottoms if progress:
name = "bottom%02d-n" % (i + 1) progress.update(step=pstep)
bottom = self.__loadBottom(name, color='white') # load negative bottoms
if bottom is not None: name = "bottom%02d-n" % (i + 1)
self._bottom_negative.append(bottom) bottom = self.__loadBottom(name, color='white')
if progress: if bottom is not None:
progress.update(step=pstep) self._bottom_negative.append(bottom)
# load letters if progress:
for rank in range(self.cs.nletters): progress.update(step=pstep)
name = "l%02d" % (rank + 1) # load letters
self._letter_positive.append( for rank in range(self.cs.nletters):
self.__loadBottom(name, color='black')) name = "l%02d" % (rank + 1)
if progress: self._letter_positive.append(
progress.update(step=pstep) self.__loadBottom(name, color='black'))
# load negative letters if progress:
name = "l%02d-n" % (rank + 1) progress.update(step=pstep)
self._letter_negative.append( # load negative letters
self.__loadBottom(name, color='white')) name = "l%02d-n" % (rank + 1)
if progress: self._letter_negative.append(
progress.update(step=pstep) self.__loadBottom(name, color='white'))
if progress:
progress.update(step=pstep)
# shadow # shadow
if not USE_PIL: if not USE_PIL:
for i in range(self.cs.nshadows): for i in range(self.cs.nshadows):

View file

@ -248,8 +248,13 @@ def after_cancel(t):
if Image: if Image:
class PIL_Image(ImageTk.PhotoImage): class PIL_Image(ImageTk.PhotoImage):
def __init__(self, file=None, image=None, pil_image_orig=None): def __init__(self, file=None, image=None, pil_image_orig=None):
if file: if file:
image = Image.open(file).convert('RGBA') image = Image.open(file).convert('RGBA')
# eliminates the 0 in alphachannel
image = masking(image)
ImageTk.PhotoImage.__init__(self, image) ImageTk.PhotoImage.__init__(self, image)
self._pil_image = image self._pil_image = image
if pil_image_orig: if pil_image_orig:
@ -266,12 +271,32 @@ if Image:
return im return im
def resize(self, xf, yf): def resize(self, xf, yf):
w, h = self._pil_image_orig.size w, h = self._pil_image_orig.size
w0, h0 = int(w*xf), int(h*yf) w0, h0 = int(w*xf), int(h*yf)
im = self._pil_image_orig.resize((w0, h0), Image.ANTIALIAS) im = self._pil_image_orig.resize((w0, h0), Image.ANTIALIAS)
im = masking(im)
return PIL_Image(image=im, pil_image_orig=self._pil_image_orig) return PIL_Image(image=im, pil_image_orig=self._pil_image_orig)
def masking(image):
# eliminates the 0 in alphachannel
# because PhotoImage and Rezising
# have problems with it
image = image.convert("RGBA") # make sure it has alphachannel
mask = image.copy()
# important alpha must be bigger than 0
mask.putalpha(1)
mask.paste(image, (0, 0), image)
image = mask.copy()
return image
def makeImage(file=None, data=None, dither=None, alpha=None): def makeImage(file=None, data=None, dither=None, alpha=None):
kw = {} kw = {}
if data is None: if data is None:
@ -401,6 +426,7 @@ def createBottom(maskimage, color='white', backfile=None):
return None return None
maskimage = maskimage._pil_image maskimage = maskimage._pil_image
out = _createBottomImage(maskimage, color, backfile) out = _createBottomImage(maskimage, color, backfile)
return PIL_Image(image=out) return PIL_Image(image=out)