From 4ad11dc85aba5ad3e93917267943a5587e3569c9 Mon Sep 17 00:00:00 2001 From: cardset Date: Sun, 13 Feb 2022 21:57:18 +0100 Subject: [PATCH] masking bottoms Should mask now load bottoms, resize bottoms and subsample. Separated load bottoms from resize bottoms --- pysollib/images.py | 52 +++++++++++++++++------------------ pysollib/ui/tktile/tkutil.py | 53 ++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/pysollib/images.py b/pysollib/images.py index 2b2b1245..2913ef28 100644 --- a/pysollib/images.py +++ b/pysollib/images.py @@ -112,11 +112,8 @@ class Images: if (not USE_PIL and TOOLKIT != 'kivy') or imagedir is None: # load image img = self.__loadCard(filename+self.cs.ext, check_w, check_h) - if USE_PIL and img is not None: - # we have no bottom images - # (data/images/cards/bottoms/) - img = img.resize(self._xfactor, self._yfactor) return img + # create image d = os.path.join('images', 'cards', 'bottoms', cs_type) try: @@ -189,7 +186,6 @@ class Images: if progress: progress.update(step=1) - # todo - fix duplication of loading bottoms # load bottoms for i in range(self.cs.nbottoms): name = "bottom%02d" % (i + 1) @@ -434,29 +430,33 @@ class Images: # back for b in self._back: b.image = b.image.resize(xf, yf) + # stack bottom image - neg = self._bottom is self._bottom_negative - self._bottom_negative = [] - self._bottom_positive = [] - for i in range(self.cs.nbottoms): - name = "bottom%02d" % (i + 1) - bottom = self.__loadBottom(name, color='black') - if bottom is not None: - self._bottom_positive.append(bottom) - name = "bottom%02d-n" % (i + 1) - bottom = self.__loadBottom(name, color='white') - if bottom is not None: - self._bottom_negative.append(bottom) + neg = self._bottom is self._bottom_negative # dont know + + bottom_negative = [] + bottom_positive = [] + for c in self._bottom_negative: + c = c.resize(xf, yf) + bottom_negative.append(c) + self._bottom_negative = bottom_negative + for c in self._bottom_positive: + c = c.resize(xf, yf) + bottom_positive.append(c) + self._bottom_positive = bottom_positive + # letters - self._letter_positive = [] - self._letter_negative = [] - for rank in range(self.cs.nletters): - name = "l%02d" % (rank + 1) - self._letter_positive.append( - self.__loadBottom(name, color='black')) - name = "l%02d-n" % (rank + 1) - self._letter_negative.append( - self.__loadBottom(name, color='white')) + letter_negative = [] + letter_positive = [] + for c in self._letter_negative: + c = c.resize(xf, yf) + letter_negative.append(c) + self._letter_negative = letter_negative + for c in self._letter_positive: + c = c.resize(xf, yf) + letter_positive.append(c) + self._letter_positive = letter_positive + self._createMissingImages() self.setNegative(neg) # diff --git a/pysollib/ui/tktile/tkutil.py b/pysollib/ui/tktile/tkutil.py index 9d904f12..b64cfbce 100644 --- a/pysollib/ui/tktile/tkutil.py +++ b/pysollib/ui/tktile/tkutil.py @@ -21,6 +21,7 @@ # # --------------------------------------------------------------------------- +import os import re from pysollib.mfxutil import Image, ImageOps, ImageTk @@ -252,8 +253,15 @@ if Image: if file: image = Image.open(file).convert('RGBA') - # eliminates the 0 in alphachannel - image = masking(image) + basename = os.path.basename(file) + file_name = os.path.splitext(basename)[0] + + findsum = findfile(file_name) + + if findsum != -3: # -1 for every check + image = masking(image) + + image.filename = file_name ImageTk.PhotoImage.__init__(self, image) self._pil_image = image @@ -266,7 +274,16 @@ if Image: im = self._pil_image w, h = im.size w, h = int(float(w)/r), int(float(h)/r) + im = im.resize((w, h)) + + try: + findsum = findfile(self._pil_image_orig.filename) + if findsum != -3: # -1 for every check + im = masking(im) + except Exception: + im = masking(im) + im = PIL_Image(image=im) return im @@ -274,9 +291,15 @@ if Image: w, h = self._pil_image_orig.size w0, h0 = int(w*xf), int(h*yf) + im = self._pil_image_orig.resize((w0, h0), Image.ANTIALIAS) - im = masking(im) + try: + findsum = findfile(self._pil_image_orig.filename) + if findsum != -3: # -1 for every check + im = masking(im) + except Exception: + im = masking(im) return PIL_Image(image=im, pil_image_orig=self._pil_image_orig) @@ -289,18 +312,6 @@ def masking(image): image = image.convert("RGBA") # make sure it has alphachannel mask = image.copy() - - # calculate median transparency value - # this will determine if the masking will have a significant - # effect on performance. - transparency = [row[3] for row in image.getdata()] - n = len(transparency) - s = sorted(transparency) - median = (s[n // 2 - 1] / 2.0 + s[n // 2] / 2.0, s[n // 2])[n % 2] - - if median > 0: - return image - # important alpha must be bigger than 0 mask.putalpha(1) mask.paste(image, (0, 0), image) @@ -309,6 +320,18 @@ def masking(image): return image +def findfile(file_name): + + find1 = file_name.find("bottom") + find2 = file_name.find("shad") + find3 = file_name.find("l0") + # find4 = file_name.find("back") + + findsum = find1 + find2 + find3 + + return findsum + + def makeImage(file=None, data=None, dither=None, alpha=None): kw = {} if data is None: