From e7fc55a4ac96207a6ce445831e0d5a62dc8c65f1 Mon Sep 17 00:00:00 2001 From: jokober Date: Mon, 9 Oct 2023 14:11:46 +0200 Subject: [PATCH 1/4] - add parameter quality to pillow.save() set to 95% as default - add suffix default extension selection based on input images data format quality --- sahi/slicing.py | 10 +++++++--- sahi/utils/cv.py | 5 +++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/sahi/slicing.py b/sahi/slicing.py index 163bcfb44..422feb11f 100644 --- a/sahi/slicing.py +++ b/sahi/slicing.py @@ -15,7 +15,7 @@ from sahi.annotation import BoundingBox, Mask from sahi.utils.coco import Coco, CocoAnnotation, CocoImage, create_coco_dict -from sahi.utils.cv import read_image_as_pil +from sahi.utils.cv import read_image_as_pil, IMAGE_EXTENSIONS_LOSSY, IMAGE_EXTENSIONS_LOSSLESS from sahi.utils.file import load_json, save_json logger = logging.getLogger(__name__) @@ -317,7 +317,7 @@ def _export_single_slice(image: np.ndarray, output_dir: str, slice_file_name: st image_pil = read_image_as_pil(image) slice_file_path = str(Path(output_dir) / slice_file_name) # export sliced image - image_pil.save(slice_file_path) + image_pil.save(slice_file_path, quality=95) image_pil.close() # to fix https://github.com/obss/sahi/issues/565 verboselog("sliced image path: " + slice_file_path) @@ -371,8 +371,12 @@ def _export_single_slice(image: np.ndarray, output_dir: str, slice_file_name: st else: try: suffix = Path(image_pil.filename).suffix + if suffix in IMAGE_EXTENSIONS_LOSSY: + suffix = '.png' + elif suffix in IMAGE_EXTENSIONS_LOSSLESS: + suffix = Path(image_pil.filename).suffix except AttributeError: - suffix = ".jpg" + suffix = ".png" # set image file name and path slice_file_name = f"{output_file_name}_{slice_suffixes}{suffix}" diff --git a/sahi/utils/cv.py b/sahi/utils/cv.py index 0204df498..54cb8c25a 100644 --- a/sahi/utils/cv.py +++ b/sahi/utils/cv.py @@ -13,8 +13,9 @@ from PIL import Image from sahi.utils.file import Path - -IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".tiff", ".bmp"] +IMAGE_EXTENSIONS_LOSSY =[".jpg", ".jpeg"] +IMAGE_EXTENSIONS_LOSSLESS = [".png", ".tiff", ".bmp"] +IMAGE_EXTENSIONS = IMAGE_EXTENSIONS_LOSSY + IMAGE_EXTENSIONS_LOSSLESS VIDEO_EXTENSIONS = [".mp4", ".mkv", ".flv", ".avi", ".ts", ".mpg", ".mov", "wmv"] From bf619ed6f8721be815bb9cad1e688aec16969fb2 Mon Sep 17 00:00:00 2001 From: jokober Date: Mon, 9 Oct 2023 14:15:49 +0200 Subject: [PATCH 2/4] - change docstring --- sahi/slicing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sahi/slicing.py b/sahi/slicing.py index 422feb11f..13a8ed944 100644 --- a/sahi/slicing.py +++ b/sahi/slicing.py @@ -295,7 +295,7 @@ def slice_image( min_area_ratio (float): If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. out_ext (str, optional): Extension of saved images. Default is the - original suffix. + original suffix for lossless image formats and png for lossy formats ('.jpg','.jpeg'). verbose (bool, optional): Switch to print relevant values to screen. Default 'False'. From 88782b80a177f3cd9491a6b9b51b1d96a136ec2e Mon Sep 17 00:00:00 2001 From: JonathanKossick Date: Wed, 18 Oct 2023 21:16:55 +0200 Subject: [PATCH 3/4] use quality='keep' instead of quality='95' --- sahi/slicing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sahi/slicing.py b/sahi/slicing.py index 13a8ed944..818d2ae26 100644 --- a/sahi/slicing.py +++ b/sahi/slicing.py @@ -317,7 +317,7 @@ def _export_single_slice(image: np.ndarray, output_dir: str, slice_file_name: st image_pil = read_image_as_pil(image) slice_file_path = str(Path(output_dir) / slice_file_name) # export sliced image - image_pil.save(slice_file_path, quality=95) + image_pil.save(slice_file_path, quality='keep') image_pil.close() # to fix https://github.com/obss/sahi/issues/565 verboselog("sliced image path: " + slice_file_path) From 7cfd03d8faa230f5e12a2f1f17e849fe16579d41 Mon Sep 17 00:00:00 2001 From: fcakyon Date: Mon, 6 Nov 2023 01:12:13 +0300 Subject: [PATCH 4/4] fix styling --- sahi/slicing.py | 6 +++--- sahi/utils/cv.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sahi/slicing.py b/sahi/slicing.py index 818d2ae26..eb3ff748e 100644 --- a/sahi/slicing.py +++ b/sahi/slicing.py @@ -15,7 +15,7 @@ from sahi.annotation import BoundingBox, Mask from sahi.utils.coco import Coco, CocoAnnotation, CocoImage, create_coco_dict -from sahi.utils.cv import read_image_as_pil, IMAGE_EXTENSIONS_LOSSY, IMAGE_EXTENSIONS_LOSSLESS +from sahi.utils.cv import IMAGE_EXTENSIONS_LOSSLESS, IMAGE_EXTENSIONS_LOSSY, read_image_as_pil from sahi.utils.file import load_json, save_json logger = logging.getLogger(__name__) @@ -317,7 +317,7 @@ def _export_single_slice(image: np.ndarray, output_dir: str, slice_file_name: st image_pil = read_image_as_pil(image) slice_file_path = str(Path(output_dir) / slice_file_name) # export sliced image - image_pil.save(slice_file_path, quality='keep') + image_pil.save(slice_file_path, quality="keep") image_pil.close() # to fix https://github.com/obss/sahi/issues/565 verboselog("sliced image path: " + slice_file_path) @@ -372,7 +372,7 @@ def _export_single_slice(image: np.ndarray, output_dir: str, slice_file_name: st try: suffix = Path(image_pil.filename).suffix if suffix in IMAGE_EXTENSIONS_LOSSY: - suffix = '.png' + suffix = ".png" elif suffix in IMAGE_EXTENSIONS_LOSSLESS: suffix = Path(image_pil.filename).suffix except AttributeError: diff --git a/sahi/utils/cv.py b/sahi/utils/cv.py index 40bdfed73..06434a086 100644 --- a/sahi/utils/cv.py +++ b/sahi/utils/cv.py @@ -13,7 +13,8 @@ from PIL import Image from sahi.utils.file import Path -IMAGE_EXTENSIONS_LOSSY =[".jpg", ".jpeg"] + +IMAGE_EXTENSIONS_LOSSY = [".jpg", ".jpeg"] IMAGE_EXTENSIONS_LOSSLESS = [".png", ".tiff", ".bmp"] IMAGE_EXTENSIONS = IMAGE_EXTENSIONS_LOSSY + IMAGE_EXTENSIONS_LOSSLESS VIDEO_EXTENSIONS = [".mp4", ".mkv", ".flv", ".avi", ".ts", ".mpg", ".mov", "wmv"]