From dc989007631714d9ee0d3b1c5094d59110132bcf Mon Sep 17 00:00:00 2001 From: cardset Date: Mon, 24 Jan 2022 14:48:17 +0100 Subject: [PATCH 1/2] 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 --- pysollib/images.py | 59 +++++++++++++++++++----------------- pysollib/ui/tktile/tkutil.py | 26 ++++++++++++++++ 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/pysollib/images.py b/pysollib/images.py index 54a32837..7a3c2790 100644 --- a/pysollib/images.py +++ b/pysollib/images.py @@ -188,34 +188,37 @@ class Images: cs_dir=self.cs.dir, fname=name)) if progress: progress.update(step=1) - # load bottoms - 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) - if progress: - progress.update(step=pstep) - # load negative bottoms - name = "bottom%02d-n" % (i + 1) - bottom = self.__loadBottom(name, color='white') - if bottom is not None: - self._bottom_negative.append(bottom) - if progress: - progress.update(step=pstep) - # load letters - for rank in range(self.cs.nletters): - name = "l%02d" % (rank + 1) - self._letter_positive.append( - self.__loadBottom(name, color='black')) - if progress: - progress.update(step=pstep) - # load negative letters - name = "l%02d-n" % (rank + 1) - self._letter_negative.append( - self.__loadBottom(name, color='white')) - if progress: - progress.update(step=pstep) + + if not USE_PIL: + # load bottoms + 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) + if progress: + progress.update(step=pstep) + # load negative bottoms + name = "bottom%02d-n" % (i + 1) + bottom = self.__loadBottom(name, color='white') + if bottom is not None: + self._bottom_negative.append(bottom) + if progress: + progress.update(step=pstep) + # load letters + for rank in range(self.cs.nletters): + name = "l%02d" % (rank + 1) + self._letter_positive.append( + self.__loadBottom(name, color='black')) + if progress: + progress.update(step=pstep) + # load negative letters + name = "l%02d-n" % (rank + 1) + self._letter_negative.append( + self.__loadBottom(name, color='white')) + if progress: + progress.update(step=pstep) + # shadow if not USE_PIL: for i in range(self.cs.nshadows): diff --git a/pysollib/ui/tktile/tkutil.py b/pysollib/ui/tktile/tkutil.py index 381bc61f..efdcd580 100644 --- a/pysollib/ui/tktile/tkutil.py +++ b/pysollib/ui/tktile/tkutil.py @@ -248,8 +248,13 @@ def after_cancel(t): if Image: class PIL_Image(ImageTk.PhotoImage): def __init__(self, file=None, image=None, pil_image_orig=None): + if file: image = Image.open(file).convert('RGBA') + + # eliminates the 0 in alphachannel + image = masking(image) + ImageTk.PhotoImage.__init__(self, image) self._pil_image = image if pil_image_orig: @@ -266,12 +271,32 @@ if Image: return im def resize(self, xf, yf): + 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) + 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): kw = {} if data is None: @@ -401,6 +426,7 @@ def createBottom(maskimage, color='white', backfile=None): return None maskimage = maskimage._pil_image out = _createBottomImage(maskimage, color, backfile) + return PIL_Image(image=out) From 94603866a7776f2da88636d72f459ca60afd62d1 Mon Sep 17 00:00:00 2001 From: cardset Date: Tue, 25 Jan 2022 08:43:38 +0100 Subject: [PATCH 2/2] Update bakersdozen.py Other redeal rules for game Cruel, and fixing only one card may move. --- pysollib/games/bakersdozen.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pysollib/games/bakersdozen.py b/pysollib/games/bakersdozen.py index 3a860fec..0bd44140 100644 --- a/pysollib/games/bakersdozen.py +++ b/pysollib/games/bakersdozen.py @@ -214,12 +214,20 @@ class Cruel_Talon(TalonStack): num_cards = 0 assert len(self.cards) == 0 rows = list(self.game.s.rows)[:] - rows.reverse() + # rows.reverse() for r in rows: for i in range(len(r.cards)): num_cards = num_cards + 1 self.game.moveMove(1, r, self, frames=0) assert len(self.cards) == num_cards + + temp_cards = [] + while len(self.cards) > 0: + temp = self.cards[:4] + temp_cards = temp + temp_cards + del self.cards[:4] + self.cards = temp_cards.copy() + if num_cards == 0: # game already finished return 0 # redeal in packs of 4 cards @@ -250,7 +258,9 @@ class Cruel_Talon(TalonStack): class Cruel(CastlesInSpain): Talon_Class = StackWrapper(Cruel_Talon, max_rounds=-1) - RowStack_Class = StackWrapper(SS_RowStack, base_rank=NO_RANK) + RowStack_Class = StackWrapper(SS_RowStack, + max_move=1, max_accept=1, base_rank=NO_RANK) + # Solver_Class = FreeCellSolverWrapper(preset='cruel') Solver_Class = None