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

masking bottoms

Should mask now load bottoms, resize bottoms and subsample.

Separated load bottoms from resize bottoms
This commit is contained in:
cardset 2022-02-13 21:57:18 +01:00 committed by Joe R
parent 4adb91dffd
commit 4ad11dc85a
2 changed files with 64 additions and 41 deletions

View file

@ -112,11 +112,8 @@ class Images:
if (not USE_PIL and TOOLKIT != 'kivy') or imagedir is None: if (not USE_PIL and TOOLKIT != 'kivy') or imagedir is None:
# load image # load image
img = self.__loadCard(filename+self.cs.ext, check_w, check_h) 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/<cs_type>)
img = img.resize(self._xfactor, self._yfactor)
return img return img
# create image # create image
d = os.path.join('images', 'cards', 'bottoms', cs_type) d = os.path.join('images', 'cards', 'bottoms', cs_type)
try: try:
@ -189,7 +186,6 @@ class Images:
if progress: if progress:
progress.update(step=1) progress.update(step=1)
# todo - fix duplication of loading bottoms
# load bottoms # load bottoms
for i in range(self.cs.nbottoms): for i in range(self.cs.nbottoms):
name = "bottom%02d" % (i + 1) name = "bottom%02d" % (i + 1)
@ -434,29 +430,33 @@ class Images:
# back # back
for b in self._back: for b in self._back:
b.image = b.image.resize(xf, yf) b.image = b.image.resize(xf, yf)
# stack bottom image # stack bottom image
neg = self._bottom is self._bottom_negative neg = self._bottom is self._bottom_negative # dont know
self._bottom_negative = []
self._bottom_positive = [] bottom_negative = []
for i in range(self.cs.nbottoms): bottom_positive = []
name = "bottom%02d" % (i + 1) for c in self._bottom_negative:
bottom = self.__loadBottom(name, color='black') c = c.resize(xf, yf)
if bottom is not None: bottom_negative.append(c)
self._bottom_positive.append(bottom) self._bottom_negative = bottom_negative
name = "bottom%02d-n" % (i + 1) for c in self._bottom_positive:
bottom = self.__loadBottom(name, color='white') c = c.resize(xf, yf)
if bottom is not None: bottom_positive.append(c)
self._bottom_negative.append(bottom) self._bottom_positive = bottom_positive
# letters # letters
self._letter_positive = [] letter_negative = []
self._letter_negative = [] letter_positive = []
for rank in range(self.cs.nletters): for c in self._letter_negative:
name = "l%02d" % (rank + 1) c = c.resize(xf, yf)
self._letter_positive.append( letter_negative.append(c)
self.__loadBottom(name, color='black')) self._letter_negative = letter_negative
name = "l%02d-n" % (rank + 1) for c in self._letter_positive:
self._letter_negative.append( c = c.resize(xf, yf)
self.__loadBottom(name, color='white')) letter_positive.append(c)
self._letter_positive = letter_positive
self._createMissingImages() self._createMissingImages()
self.setNegative(neg) self.setNegative(neg)
# #

View file

@ -21,6 +21,7 @@
# #
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
import os
import re import re
from pysollib.mfxutil import Image, ImageOps, ImageTk from pysollib.mfxutil import Image, ImageOps, ImageTk
@ -252,8 +253,15 @@ if Image:
if file: if file:
image = Image.open(file).convert('RGBA') image = Image.open(file).convert('RGBA')
# eliminates the 0 in alphachannel basename = os.path.basename(file)
image = masking(image) 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) ImageTk.PhotoImage.__init__(self, image)
self._pil_image = image self._pil_image = image
@ -266,7 +274,16 @@ if Image:
im = self._pil_image im = self._pil_image
w, h = im.size w, h = im.size
w, h = int(float(w)/r), int(float(h)/r) w, h = int(float(w)/r), int(float(h)/r)
im = im.resize((w, h)) 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) im = PIL_Image(image=im)
return im return im
@ -274,9 +291,15 @@ if Image:
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) 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) 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 image = image.convert("RGBA") # make sure it has alphachannel
mask = image.copy() 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 # important alpha must be bigger than 0
mask.putalpha(1) mask.putalpha(1)
mask.paste(image, (0, 0), image) mask.paste(image, (0, 0), image)
@ -309,6 +320,18 @@ def masking(image):
return 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): def makeImage(file=None, data=None, dither=None, alpha=None):
kw = {} kw = {}
if data is None: if data is None: