Browse Source

Use pillow's context manager

pull/498/head
Simon Conseil 3 years ago
parent
commit
3cf4f0979a
  1. 4
      src/sigal/image.py
  2. 4
      src/sigal/plugins/watermark.py
  3. 4
      src/sigal/video.py
  4. 35
      tests/test_image.py

4
src/sigal/image.py

@ -282,6 +282,7 @@ def get_iptc_data(filename):
def get_image_metadata(filename):
logger = logging.getLogger(__name__)
exif, iptc, size = {}, {}, {}
img = None
try:
img = _read_image(filename)
@ -303,6 +304,9 @@ def get_image_metadata(filename):
size = get_size(img)
except Exception as e:
logger.warning("Could not read size from %s: %s", filename, e)
finally:
if img is not None:
img.close()
return {"exif": exif, "iptc": iptc, "size": size}

4
src/sigal/plugins/watermark.py

@ -86,10 +86,10 @@ def watermark(im, mark, position, opacity=1):
def add_watermark(img, settings=None):
logger = logging.getLogger(__name__)
logger.debug("Adding watermark to %r", img)
mark = Image.open(settings["watermark"])
position = settings.get("watermark_position", "scale")
opacity = settings.get("watermark_opacity", 1)
return watermark(img, mark, position, opacity)
with Image.open(settings["watermark"]) as mark:
return watermark(img, mark, position, opacity)
def register(settings):

4
src/sigal/video.py

@ -198,8 +198,8 @@ def generate_thumbnail(
logger.debug("Create thumbnail for video: %s", " ".join(cmd))
check_subprocess(cmd, source, outname)
if os.path.isfile(tmpfile) and black_retries > 0:
img = PILImage.open(tmpfile)
colors = img.getcolors(maxcolors=black_max_colors)
with PILImage.open(tmpfile) as img:
colors = img.getcolors(maxcolors=black_max_colors)
if colors is None:
# There were more colors than maxcolors in the image, it
# looks suitable for a valid thumbnail

35
tests/test_image.py

@ -42,8 +42,8 @@ def test_process_image(tmpdir):
image = Image(TEST_IMAGE, ".", settings)
status = process_image(image)
assert status == Status.SUCCESS
im = PILImage.open(os.path.join(str(tmpdir), TEST_IMAGE))
assert im.size == settings["img_size"]
with PILImage.open(os.path.join(str(tmpdir), TEST_IMAGE)) as im:
assert im.size == settings["img_size"]
def test_generate_image(tmpdir):
@ -56,8 +56,8 @@ def test_generate_image(tmpdir):
)
options = None if i == 0 else {"quality": 85}
generate_image(SRCFILE, dstfile, settings, options=options)
im = PILImage.open(dstfile)
assert im.size == size
with PILImage.open(dstfile) as im:
assert im.size == size
def test_generate_image_imgformat(tmpdir):
@ -73,8 +73,8 @@ def test_generate_image_imgformat(tmpdir):
)
options = {"quality": 85}
generate_image(SRCFILE, dstfile, settings, options=options)
im = PILImage.open(dstfile)
assert im.format == outfmt
with PILImage.open(dstfile) as im:
assert im.format == outfmt
def test_resize_image_portrait(tmpdir):
@ -89,12 +89,11 @@ def test_resize_image_portrait(tmpdir):
portrait_dst = str(tmpdir.join(portrait_image))
generate_image(portrait_src, portrait_dst, settings)
im = PILImage.open(portrait_dst)
# In the default mode, PILKit resizes in a way to never make an image
# smaller than either of the lengths, the other is scaled accordingly.
# Hence we test that the shorter side has the smallest length.
assert im.size[0] == 200
with PILImage.open(portrait_dst) as im:
# In the default mode, PILKit resizes in a way to never make an image
# smaller than either of the lengths, the other is scaled accordingly.
# Hence we test that the shorter side has the smallest length.
assert im.size[0] == 200
landscape_image = "KeckObservatory20071020.jpg"
landscape_src = os.path.join(
@ -103,8 +102,8 @@ def test_resize_image_portrait(tmpdir):
landscape_dst = str(tmpdir.join(landscape_image))
generate_image(landscape_src, landscape_dst, settings)
im = PILImage.open(landscape_dst)
assert im.size[1] == 200
with PILImage.open(landscape_dst) as im:
assert im.size[1] == 200
@pytest.mark.parametrize(
@ -158,13 +157,13 @@ def test_generate_thumbnail(tmpdir, image, path, wide_size, high_size):
dstfile = str(tmpdir.join(image))
for size in [(200, 150), (150, 200)]:
generate_thumbnail(path, dstfile, size)
im = PILImage.open(dstfile)
assert im.size == size
with PILImage.open(dstfile) as im:
assert im.size == size
for size, thumb_size in [((200, 150), wide_size), ((150, 200), high_size)]:
generate_thumbnail(path, dstfile, size, fit=False)
im = PILImage.open(dstfile)
assert im.size == thumb_size
with PILImage.open(dstfile) as im:
assert im.size == thumb_size
def test_get_exif_tags():

Loading…
Cancel
Save