artresizer: move deinterlace functions to backend classes

This commit is contained in:
wisp3rwind 2022-03-12 20:30:39 +01:00
parent 279c081f23
commit 1b92dea995
2 changed files with 30 additions and 40 deletions

View file

@ -194,6 +194,22 @@ class IMBackend(LocalBackend):
log.warning('Could not understand IM output: {0!r}', out)
return None
def deinterlace(self, path_in, path_out=None):
path_out = path_out or temp_file_for(path_in)
cmd = self.convert_cmd + [
syspath(path_in, prefix=False),
'-interlace', 'none',
syspath(path_out, prefix=False),
]
try:
util.command_output(cmd)
return path_out
except subprocess.CalledProcessError:
# FIXME: add a warning
return path_in
class PILBackend(LocalBackend):
NAME="PIL"
@ -282,39 +298,16 @@ class PILBackend(LocalBackend):
displayable_path(path_in), exc)
return None
def deinterlace(self, path_in, path_out=None):
path_out = path_out or temp_file_for(path_in)
from PIL import Image
def pil_deinterlace(backend, path_in, path_out=None):
path_out = path_out or temp_file_for(path_in)
from PIL import Image
try:
im = Image.open(syspath(path_in))
im.save(py3_path(path_out), progressive=False)
return path_out
except IOError:
return path_in
def im_deinterlace(backend, path_in, path_out=None):
path_out = path_out or temp_file_for(path_in)
cmd = backend.convert_cmd + [
syspath(path_in, prefix=False),
'-interlace', 'none',
syspath(path_out, prefix=False),
]
try:
util.command_output(cmd)
return path_out
except subprocess.CalledProcessError:
return path_in
DEINTERLACE_FUNCS = {
PIL: pil_deinterlace,
IMAGEMAGICK: im_deinterlace,
}
try:
im = Image.open(syspath(path_in))
im.save(py3_path(path_out), progressive=False)
return path_out
except IOError:
return path_in
def im_get_format(backend, filepath):
@ -512,8 +505,7 @@ class ArtResizer(metaclass=Shareable):
Only available locally.
"""
if self.local:
func = DEINTERLACE_FUNCS[self.local_method.ID]
return func(self.local_method, path_in, path_out)
return self.local_method.deinterlace(path_in, path_out)
else:
# FIXME: Should probably issue a warning?
return path_in

View file

@ -24,8 +24,6 @@ from beets.util import command_output, syspath
from beets.util.artresizer import (
IMBackend,
PILBackend,
pil_deinterlace,
im_deinterlace,
)
@ -120,10 +118,10 @@ class ArtResizerFileSizeTest(_common.TestCase, TestHelper):
def test_pil_file_deinterlace(self):
"""Test PIL deinterlace function.
Check if pil_deinterlace function returns images
Check if the `PILBackend.deinterlace()` function returns images
that are non-progressive
"""
path = pil_deinterlace(PILBackend(), self.IMG_225x225)
path = PILBackend().deinterlace(self.IMG_225x225)
from PIL import Image
with Image.open(path) as img:
self.assertFalse('progression' in img.info)
@ -132,11 +130,11 @@ class ArtResizerFileSizeTest(_common.TestCase, TestHelper):
def test_im_file_deinterlace(self):
"""Test ImageMagick deinterlace function.
Check if im_deinterlace function returns images
Check if the `IMBackend.deinterlace()` function returns images
that are non-progressive.
"""
im = IMBackend()
path = im_deinterlace(im, self.IMG_225x225)
path = im.deinterlace(self.IMG_225x225)
cmd = im.identify_cmd + [
'-format', '%[interlace]', syspath(path, prefix=False),
]