| 1 |
"""Tests for image resizing based on filesize.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import unittest |
| 5 |
from pathlib import Path |
| 6 |
from unittest.mock import patch |
| 7 |
|
| 8 |
from beets.test import _common |
| 9 |
from beets.test.fixtures import DummyIMBackend |
| 10 |
from beets.test.helper import BeetsTestCase, CleanupModulesMixin |
| 11 |
from beets.util import command_output |
| 12 |
from beets.util.artresizer import IMBackend, PILBackend |
| 13 |
|
| 14 |
|
| 15 |
class ArtResizerFileSizeTest(CleanupModulesMixin, BeetsTestCase): |
| 16 |
"""Unittest test case for Art Resizer to a specific filesize.""" |
| 17 |
|
| 18 |
modules = (IMBackend.__module__,) |
| 19 |
|
| 20 |
IMG_225x225 = _common.RSRC / "abbey.jpg" |
| 21 |
|
| 22 |
def _test_img_resize(self, backend): |
| 23 |
"""Test resizing based on file size, given a resize_func.""" |
| 24 |
# Check quality setting unaffected by new parameter |
| 25 |
im_95_qual = backend.resize( |
| 26 |
225, self.IMG_225x225, quality=95, max_filesize=0 |
| 27 |
) |
| 28 |
# check valid path returned - max_filesize hasn't broken resize command |
| 29 |
assert Path(os.fsdecode(im_95_qual)).exists() |
| 30 |
|
| 31 |
# Attempt a lower filesize with same quality |
| 32 |
im_a = backend.resize( |
| 33 |
225, |
| 34 |
self.IMG_225x225, |
| 35 |
quality=95, |
| 36 |
max_filesize=0.9 * os.stat(im_95_qual).st_size, |
| 37 |
) |
| 38 |
assert Path(os.fsdecode(im_a)).exists() |
| 39 |
# target size was achieved |
| 40 |
assert os.stat(im_a).st_size < os.stat(im_95_qual).st_size |
| 41 |
|
| 42 |
# Attempt with lower initial quality |
| 43 |
im_75_qual = backend.resize( |
| 44 |
225, self.IMG_225x225, quality=75, max_filesize=0 |
| 45 |
) |
| 46 |
assert Path(os.fsdecode(im_75_qual)).exists() |
| 47 |
|
| 48 |
im_b = backend.resize( |
| 49 |
225, |
| 50 |
self.IMG_225x225, |
| 51 |
quality=95, |
| 52 |
max_filesize=0.9 * os.stat(im_75_qual).st_size, |
| 53 |
) |
| 54 |
assert Path(os.fsdecode(im_b)).exists() |
| 55 |
# Check high (initial) quality still gives a smaller filesize |
| 56 |
assert os.stat(im_b).st_size < os.stat(im_75_qual).st_size |
| 57 |
|
| 58 |
@unittest.skipUnless(PILBackend.available(), "PIL not available") |
| 59 |
def test_pil_file_resize(self): |
| 60 |
"""Test PIL resize function is lowering file size.""" |
| 61 |
self._test_img_resize(PILBackend()) |
| 62 |
|
| 63 |
@unittest.skipUnless(IMBackend.available(), "ImageMagick not available") |
| 64 |
def test_im_file_resize(self): |
| 65 |
"""Test IM resize function is lowering file size.""" |
| 66 |
self._test_img_resize(IMBackend()) |
| 67 |
|
| 68 |
@unittest.skipUnless(PILBackend.available(), "PIL not available") |
| 69 |
def test_pil_file_deinterlace(self): |
| 70 |
"""Test PIL deinterlace function. |
| 71 |
|
| 72 |
Check if the `PILBackend.deinterlace()` function returns images |
| 73 |
that are non-progressive |
| 74 |
""" |
| 75 |
path = PILBackend().deinterlace(self.IMG_225x225) |
| 76 |
from PIL import Image |
| 77 |
|
| 78 |
with Image.open(path) as img: |
| 79 |
assert "progression" not in img.info |
| 80 |
|
| 81 |
@unittest.skipUnless(IMBackend.available(), "ImageMagick not available") |
| 82 |
def test_im_file_deinterlace(self): |
| 83 |
"""Test ImageMagick deinterlace function. |
| 84 |
|
| 85 |
Check if the `IMBackend.deinterlace()` function returns images |
| 86 |
that are non-progressive. |
| 87 |
""" |
| 88 |
im = IMBackend() |
| 89 |
path = im.deinterlace(self.IMG_225x225) |
| 90 |
cmd = [*im.identify_cmd, "-format", "%[interlace]", os.fsdecode(path)] |
| 91 |
out = command_output(cmd).stdout |
| 92 |
assert out == b"None" |
| 93 |
|
| 94 |
@patch("beets.util.artresizer.util") |
| 95 |
def test_write_metadata_im(self, mock_util): |
| 96 |
"""Test writing image metadata.""" |
| 97 |
metadata = {"a": "A", "b": "B"} |
| 98 |
im = DummyIMBackend() |
| 99 |
im.write_metadata("foo", metadata) |
| 100 |
command = [*im.convert_cmd, *"foo -set a A -set b B foo".split()] |
| 101 |
mock_util.command_output.assert_called_once_with(command) |