Added exception handling/capture for convert-to-jpeg, issue #322
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
""" version info """
|
||||
|
||||
__version__ = "0.39.5"
|
||||
__version__ = "0.39.6"
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
# reference: https://stackoverflow.com/questions/59330149/coreimage-ciimage-write-jpg-is-shifting-colors-macos/59334308#59334308
|
||||
|
||||
import logging
|
||||
import pathlib
|
||||
|
||||
import Metal
|
||||
@@ -16,6 +15,11 @@ from Foundation import NSDictionary
|
||||
from wurlitzer import pipes
|
||||
|
||||
|
||||
class ImageConversionError(Exception):
|
||||
"""Base class for exceptions in this module. """
|
||||
|
||||
pass
|
||||
|
||||
class ImageConverter:
|
||||
""" Convert images to jpeg. This class is a singleton
|
||||
which will re-use the Core Image CIContext to avoid
|
||||
@@ -60,6 +64,7 @@ class ImageConverter:
|
||||
Raises:
|
||||
ValueError if compression quality not in range 0.0 to 1.0
|
||||
FileNotFoundError if input_path doesn't exist
|
||||
ImageConversionError if error during conversion
|
||||
"""
|
||||
|
||||
# accept input_path or output_path as pathlib.Path
|
||||
@@ -89,8 +94,7 @@ class ImageConverter:
|
||||
input_image = Quartz.CIImage.imageWithContentsOfURL_(input_url)
|
||||
|
||||
if input_image is None:
|
||||
logging.debug(f"Could not create CIImage for {input_path}")
|
||||
return False
|
||||
raise ImageConversionError(f"Could not create CIImage for {input_path}")
|
||||
|
||||
output_colorspace = input_image.colorSpace() or Quartz.CGColorSpaceCreateWithName(
|
||||
Quartz.CoreGraphics.kCGColorSpaceSRGB
|
||||
@@ -105,8 +109,7 @@ class ImageConverter:
|
||||
if not error:
|
||||
return True
|
||||
else:
|
||||
logging.debug(
|
||||
raise ImageConversionError(
|
||||
"Error converting file {input_path} to jpeg at {output_path}: {error}"
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
2
setup.py
2
setup.py
@@ -51,7 +51,7 @@ with open(os.path.join(this_directory, "README.md"), encoding="utf-8") as f:
|
||||
setup(
|
||||
name="osxphotos",
|
||||
version=about["__version__"],
|
||||
description="Manipulate (read-only) Apple's Photos app library on Mac OS X",
|
||||
description="Export photos from Apple's macOS Photos app and query the Photos library database to access metadata about images.",
|
||||
long_description=about["long_description"],
|
||||
long_description_content_type="text/markdown",
|
||||
author="Rhet Turnbull",
|
||||
|
||||
@@ -89,14 +89,15 @@ def test_image_converter_bad_file():
|
||||
""" Try to convert a file that's not an image """
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
from osxphotos.imageconverter import ImageConverter, ImageConversionError
|
||||
|
||||
converter = ImageConverter()
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with tempdir:
|
||||
imgfile = pathlib.Path(TEST_NOT_AN_IMAGE)
|
||||
outfile = pathlib.Path(tempdir.name) / f"{imgfile.stem}.jpeg"
|
||||
assert not converter.write_jpeg(imgfile, outfile)
|
||||
with pytest.raises(ImageConversionError):
|
||||
converter.write_jpeg(imgfile, outfile)
|
||||
|
||||
|
||||
def test_image_converter_missing_file():
|
||||
|
||||
Reference in New Issue
Block a user