Skip to content

Commit

Permalink
Add test for resize and reformat at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbaquerizo committed Sep 26, 2024
1 parent 1af1ea1 commit e71bad7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions beets/util/artresizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def convert(self, source, **kwargs):
if not self.local:
# FIXME: Should probably issue a warning?
return source

params = {}

if "new_format" in kwargs:
Expand All @@ -541,7 +541,7 @@ def convert(self, source, **kwargs):
fname, ext = os.path.splitext(source)

target = fname + b"." + new_format.encode("utf8")
params['target'] = target
params["target"] = target

if "maxwidth" in kwargs and kwargs["maxwidth"] > 0:
params["maxwidth"] = kwargs["maxwidth"]
Expand Down
8 changes: 4 additions & 4 deletions beetsplug/fetchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def convert(self, plugin):
self._log.debug(
"image needs rescaling ({} > {})", self.size[0], plugin.maxwidth
)
convert_params['maxwidth'] = plugin.maxwidth
convert_params["maxwidth"] = plugin.maxwidth

filesize = os.stat(syspath(self.path)).st_size
if self.should_downsize(plugin, filesize):
Expand All @@ -187,7 +187,7 @@ def convert(self, plugin):
filesize,
plugin.max_filesize,
)
convert_params['max_filesize'] = plugin.max_filesize
convert_params["max_filesize"] = plugin.max_filesize

fmt = ArtResizer.shared.get_format(self.path)
if self.should_reformat(plugin, fmt):
Expand All @@ -196,11 +196,11 @@ def convert(self, plugin):
fmt,
plugin.cover_format,
)
convert_params['new_format'] = plugin.cover_format
convert_params["new_format"] = plugin.cover_format

if self.should_deinterlace(plugin):
self._log.debug("image needs deinterlacing")
convert_params['deinterlace'] = plugin.deinterlace
convert_params["deinterlace"] = plugin.deinterlace

if convert_params:
self.path = ArtResizer.shared.convert(self.path, **convert_params)
Expand Down
33 changes: 24 additions & 9 deletions test/test_art_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,50 @@ def _test_img_resize(self, backend):
< os.stat(syspath(im_75_qual)).st_size
)

def _test_img_reformat(self, backend):
def _test_img_reformat(self, backend):
fname, ext = os.path.splitext(self.IMG_225x225)
target_jpg = fname + b"." + "png".encode("utf8")
target_png = fname + b"." + "png".encode("utf8")
# check reformat converts jpg to png
im_png = backend.convert(
self.IMG_225x225,
target=target_jpg,
target=target_png,
)
self.assertEqual(b"PNG", backend.get_format(im_png))
assert backend.get_format(im_png) == b"PNG"

# check reformat converts png to jpg with deinterlaced and maxwidth option
fname, ext = os.path.splitext(self.IMG_225x225_PNG)
target_png = fname + b"." + "jpg".encode("utf8")
target_jpg = fname + b"." + "jpg".encode("utf8")
im_jpg_deinterlaced = backend.convert(
self.IMG_225x225_PNG,
maxwidth=225,
target=target_png
target=target_jpg,
deinterlaced=True,
)

self.assertEqual(b"JPEG", backend.get_format(im_jpg_deinterlaced))
assert backend.get_format(im_jpg_deinterlaced) == b"JPEG"
self._test_img_deinterlaced(backend, im_jpg_deinterlaced)

# check reformat actually also resizes if maxwidth is also passed in
im_png_deinterlaced_smaller = backend.convert(
self.IMG_225x225_PNG,
maxwidth=100,
deinterlaced=True,
)

assert backend.get_format(im_png_deinterlaced_smaller) == b"PNG"
assert (
os.stat(syspath(im_png_deinterlaced_smaller)).st_size
< os.stat(syspath(self.IMG_225x225_PNG)).st_size
)
self._test_img_deinterlaced(backend, im_png_deinterlaced_smaller)

def _test_img_deinterlaced(self, backend, path):
if backend.NAME == 'PIL':
if backend.NAME == "PIL":
from PIL import Image

with Image.open(path) as img:
assert "progression" not in img.info
elif backend.NAME == 'IMImageMagick':
elif backend.NAME == "IMImageMagick":
cmd = backend.identify_cmd + [
"-format",
"%[interlace]",
Expand Down

0 comments on commit e71bad7

Please sign in to comment.