Skip to content

Commit

Permalink
Merge pull request #15 from soeren-kirchner/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
soeren-kirchner authored Oct 16, 2020
2 parents 8265821 + c24b180 commit 700db59
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
27 changes: 15 additions & 12 deletions pyimgbatch/pyimgbatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from os import makedirs
from os.path import basename, join, exists, abspath
from glob import glob
from tqdm import tqdm
# from contextlib import suppress


from pprint import pformat

from time import time

from PIL import Image
from PIL import ImageCms
from tqdm import tqdm


WEBSETS = {'@2x': 2, '@3x': 3}
Expand All @@ -25,6 +27,8 @@
'box': Image.BOX,
'antialias': Image.ANTIALIAS}

debug = logging.debug


class CONSTANTS(type):
pass
Expand Down Expand Up @@ -235,7 +239,7 @@ def _process_configs(self, raw_config):
configs.extend(self._create_webset_entries(entry))
else:
configs.append(entry)
logging.debug("raw config:")
debug("raw config:")
logging.debug(pformat(raw_config))
logging.debug("solved config:")
logging.debug(pformat(configs))
Expand Down Expand Up @@ -305,7 +309,7 @@ def generate(self):
with Image.open(self.source_filename) as image:
if self.config_entry.mode != image.mode:
logging.debug(f"Source and destination mode differ. Source: {image.mode}, Destination: {self.config_entry.mode}")
self.convert(image)
image = self.convert(image)

profile = self.get_image_profile(image)
if profile is not None:
Expand All @@ -327,17 +331,18 @@ def convert(self, image):
source_profile = self.get_image_profile(image)
if source_profile is not None:
destination_profile = self.profile()
logging.debug(f"Transforming color profiles. Source: {self.profile_name(source_profile)}, Destination: {self.profile_name(destination_profile)} ")
inMode = image.mode
outMode = self.config_entry.mode
logging.debug(f"Transforming {self.profile_name(source_profile)} -> {self.profile_name(destination_profile)}, {inMode} -> {outMode}")
transform = ImageCms.buildTransform(inputProfile=source_profile,
outputProfile=destination_profile,
inMode=image.mode,
outMode=self.config_entry.mode,
inMode=inMode,
outMode=outMode,
renderingIntent=ImageCms.INTENT_RELATIVE_COLORIMETRIC)
ImageCms.applyTransform(image, transform)
return ImageCms.applyTransform(image, transform)
else:
logging.debug(f"Converting color modes. Source: {image.mode}, Destination: {self.config_entry.mode}")
image = image.convert(mode=self.config_entry.mode)
return image
return image.convert(mode=self.config_entry.mode)

def get_image_profile(self, image):
try:
Expand Down Expand Up @@ -388,7 +393,6 @@ def size(self):
return self._size

def destination_size(self, designated_size):
# print(designated_size)
if designated_size.width is not None and designated_size.height is not None:
return designated_size
elif designated_size.width is None and designated_size.height is not None:
Expand Down Expand Up @@ -446,7 +450,6 @@ def __del__(cls):

def to_int_or_none(value, multiplier=1):
try:
value = int(value) * multiplier
return value
return int(value) * multiplier
except:
return None
17 changes: 17 additions & 0 deletions pyimgbatch/test_pyimgbatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from pyimgbatch import to_int_or_none


class TestToIntOrNone(unittest.TestCase):

def test_to_int_or_none(self):
result = to_int_or_none(5)
self.assertIsNotNone(result)
self.assertEqual(result, 5)
result = to_int_or_none("5")
self.assertIsNotNone(result)
self.assertEqual(result, 6)


if __name__ == "__main__":
unittest.main()

0 comments on commit 700db59

Please sign in to comment.