diff --git a/betty/cropper/tasks.py b/betty/cropper/tasks.py index cd1d553..2eac6bc 100644 --- a/betty/cropper/tasks.py +++ b/betty/cropper/tasks.py @@ -51,16 +51,25 @@ def optimize_image(image_id): image.optimized.name = optimized_upload_to(image, filename) if format == "JPEG": # For JPEG files, we need to make sure that we keep the quantization profile - im.save( - image.optimized.name, - icc_profile=icc_profile, - quality="keep", - subsampling=subsampling - ) + try: + im.save( + image.optimized.name, + icc_profile=icc_profile, + quality="keep", + subsampling=subsampling + ) + except TypeError as e: + # Maybe the image already had an invalid quant table? + if e.message.startswith("Not a valid numbers of quantization tables"): + im.save( + image.optimized.name, + icc_profile=icc_profile + ) + else: + raise else: im.save(image.optimized.name, icc_profile=icc_profile) image.save() - print(image.optimized) return image_id diff --git a/tests/test_image_files.py b/tests/test_image_files.py index 4c46908..a93c749 100644 --- a/tests/test_image_files.py +++ b/tests/test_image_files.py @@ -155,22 +155,3 @@ def test_gif_upload(self): def tearDown(self): shutil.rmtree(settings.BETTY_IMAGE_ROOT, ignore_errors=True) - - # def test_large_image_upload(self): - # assert self.client.login(username="admin", password=self.password) - # image_path = os.path.join(TEST_DATA_PATH, 'huge.jpg') - # with open(image_path, "rb") as huge: - # data = {"image": huge, "name": "A COOL TRAIN"} - # res = self.client.post('/images/api/new', data) - # self.assertEqual(res.status_code, 200) - # response_json = json.loads(res.content.decode("utf-8")) - # self.assertEqual(response_json.get('name'), 'A COOL TRAIN') - - # image = Image.objects.get(id=response_json['id']) - # self.assertTrue(os.path.exists(image.path())) - # self.assertTrue(os.path.exists(image.optimized.path)) - # img = PILImage.open(image.optimized.path) - # self.assertEqual(img.size[0], settings.BETTY_MAX_WIDTH) - - # self.assertEqual(os.path.basename(image.source.path), "huge.jpg") - # self.assertEqual(image.name, "A COOL TRAIN")