예제 #1
0
파일: thumbnails.py 프로젝트: Gondlar/beets
    def _check_local_ok(self):
        """Check that's everythings ready:
            - local capability to resize images
            - thumbnail dirs exist (create them if needed)
            - detect whether we'll use PIL or IM
            - detect whether we'll use GIO or Python to get URIs
        """
        if not ArtResizer.shared.local:
            self._log.warning(u"No local image resizing capabilities, "
                              u"cannot generate thumbnails")
            return False

        for dir in (NORMAL_DIR, LARGE_DIR):
            if not os.path.exists(dir):
                os.makedirs(dir)

        if get_im_version():
            self.write_metadata = write_metadata_im
            tool = "IM"
        else:
            assert get_pil_version()  # since we're local
            self.write_metadata = write_metadata_pil
            tool = "PIL"
        self._log.debug(u"using {0} to write metadata", tool)

        uri_getter = GioURI()
        if not uri_getter.available:
            uri_getter = PathlibURI()
        self._log.debug(u"using {0.name} to compute URIs", uri_getter)
        self.get_uri = uri_getter.uri

        return True
예제 #2
0
    def _check_local_ok(self):
        """Check that's everythings ready:
            - local capability to resize images
            - thumbnail dirs exist (create them if needed)
            - detect whether we'll use PIL or IM
            - detect whether we'll use GIO or Python to get URIs
        """
        if not ArtResizer.shared.local:
            self._log.warning(u"No local image resizing capabilities, "
                              u"cannot generate thumbnails")
            return False

        for dir in (NORMAL_DIR, LARGE_DIR):
            if not os.path.exists(dir):
                os.makedirs(dir)

        if get_im_version():
            self.write_metadata = write_metadata_im
            tool = "IM"
        else:
            assert get_pil_version()  # since we're local
            self.write_metadata = write_metadata_pil
            tool = "PIL"
        self._log.debug(u"using {0} to write metadata", tool)

        uri_getter = GioURI()
        if not uri_getter.available:
            uri_getter = PathlibURI()
        self._log.debug(u"using {0.name} to compute URIs", uri_getter)
        self.get_uri = uri_getter.uri

        return True
예제 #3
0
class ArtResizerFileSizeTest(_common.TestCase, TestHelper):
    """Unittest test case for Art Resizer to a specific filesize."""

    IMG_225x225 = os.path.join(_common.RSRC, b"abbey.jpg")
    IMG_225x225_SIZE = os.stat(syspath(IMG_225x225)).st_size

    def setUp(self):
        """Called before each test, setting up beets."""
        self.setup_beets()

    def tearDown(self):
        """Called after each test, unloading all plugins."""
        self.teardown_beets()

    def _test_img_resize(self, resize_func):
        """Test resizing based on file size, given a resize_func."""
        # Check quality setting unaffected by new parameter
        im_95_qual = resize_func(
            225,
            self.IMG_225x225,
            quality=95,
            max_filesize=0,
        )
        # check valid path returned - max_filesize hasn't broken resize command
        self.assertExists(im_95_qual)

        # Attempt a lower filesize with same quality
        im_a = resize_func(
            225,
            self.IMG_225x225,
            quality=95,
            max_filesize=0.9 * os.stat(syspath(im_95_qual)).st_size,
        )
        self.assertExists(im_a)
        # target size was achieved
        self.assertLess(
            os.stat(syspath(im_a)).st_size,
            os.stat(syspath(im_95_qual)).st_size)

        # Attempt with lower initial quality
        im_75_qual = resize_func(
            225,
            self.IMG_225x225,
            quality=75,
            max_filesize=0,
        )
        self.assertExists(im_75_qual)

        im_b = resize_func(
            225,
            self.IMG_225x225,
            quality=95,
            max_filesize=0.9 * os.stat(syspath(im_75_qual)).st_size,
        )
        self.assertExists(im_b)
        # Check high (initial) quality still gives a smaller filesize
        self.assertLess(
            os.stat(syspath(im_b)).st_size,
            os.stat(syspath(im_75_qual)).st_size)

    @unittest.skipUnless(get_pil_version(), "PIL not available")
    def test_pil_file_resize(self):
        """Test PIL resize function is lowering file size."""
        self._test_img_resize(pil_resize)

    @unittest.skipUnless(get_im_version(), "ImageMagick not available")
    def test_im_file_resize(self):
        """Test IM resize function is lowering file size."""
        self._test_img_resize(im_resize)
예제 #4
0
class ArtResizerFileSizeTest(_common.TestCase, TestHelper):
    """Unittest test case for Art Resizer to a specific filesize."""

    IMG_225x225 = os.path.join(_common.RSRC, b"abbey.jpg")
    IMG_225x225_SIZE = os.stat(syspath(IMG_225x225)).st_size

    def setUp(self):
        """Called before each test, setting up beets."""
        self.setup_beets()

    def tearDown(self):
        """Called after each test, unloading all plugins."""
        self.teardown_beets()

    def _test_img_resize(self, resize_func):
        """Test resizing based on file size, given a resize_func."""
        # Check quality setting unaffected by new parameter
        im_95_qual = resize_func(
            225,
            self.IMG_225x225,
            quality=95,
            max_filesize=0,
        )
        # check valid path returned - max_filesize hasn't broken resize command
        self.assertExists(im_95_qual)

        # Attempt a lower filesize with same quality
        im_a = resize_func(
            225,
            self.IMG_225x225,
            quality=95,
            max_filesize=0.9 * os.stat(syspath(im_95_qual)).st_size,
        )
        self.assertExists(im_a)
        # target size was achieved
        self.assertLess(
            os.stat(syspath(im_a)).st_size,
            os.stat(syspath(im_95_qual)).st_size)

        # Attempt with lower initial quality
        im_75_qual = resize_func(
            225,
            self.IMG_225x225,
            quality=75,
            max_filesize=0,
        )
        self.assertExists(im_75_qual)

        im_b = resize_func(
            225,
            self.IMG_225x225,
            quality=95,
            max_filesize=0.9 * os.stat(syspath(im_75_qual)).st_size,
        )
        self.assertExists(im_b)
        # Check high (initial) quality still gives a smaller filesize
        self.assertLess(
            os.stat(syspath(im_b)).st_size,
            os.stat(syspath(im_75_qual)).st_size)

    @unittest.skipUnless(get_pil_version(), "PIL not available")
    def test_pil_file_resize(self):
        """Test PIL resize function is lowering file size."""
        self._test_img_resize(pil_resize)

    @unittest.skipUnless(get_im_version(), "ImageMagick not available")
    def test_im_file_resize(self):
        """Test IM resize function is lowering file size."""
        self._test_img_resize(im_resize)

    @unittest.skipUnless(get_pil_version(), "PIL not available")
    def test_pil_file_deinterlace(self):
        """Test PIL deinterlace function.

        Check if pil_deinterlace function returns images
        that are non-progressive
        """
        path = pil_deinterlace(self.IMG_225x225)
        from PIL import Image
        with Image.open(path) as img:
            self.assertFalse('progression' in img.info)

    @unittest.skipUnless(get_im_version(), "ImageMagick not available")
    def test_im_file_deinterlace(self):
        """Test ImageMagick deinterlace function.

        Check if im_deinterlace function returns images
        that are non-progressive.
        """
        path = im_deinterlace(self.IMG_225x225)
        cmd = ArtResizer.shared.im_identify_cmd + [
            '-format',
            '%[interlace]',
            syspath(path, prefix=False),
        ]
        out = command_output(cmd).stdout
        self.assertTrue(out == b'None')