Skip to content

Commit

Permalink
Slicing: Quality focused default image format selection and jpg quali…
Browse files Browse the repository at this point in the history
…ty (#956)

Co-authored-by: jokober <glpat-xy-QA7sDBF1125TDuaBe>
Co-authored-by: fatih <34196005+fcakyon@users.noreply.github.com>
Co-authored-by: fcakyon <fcakyon@gmail.com>
  • Loading branch information
3 people authored Nov 5, 2023
1 parent 3061811 commit ad25062
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions sahi/slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 IMAGE_EXTENSIONS_LOSSLESS, IMAGE_EXTENSIONS_LOSSY, read_image_as_pil
from sahi.utils.file import load_json, save_json

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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'.
Expand All @@ -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="keep")
image_pil.close() # to fix https://github.com/obss/sahi/issues/565
verboselog("sliced image path: " + slice_file_path)

Expand Down Expand Up @@ -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}"
Expand Down
4 changes: 3 additions & 1 deletion sahi/utils/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

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"]


Expand Down

0 comments on commit ad25062

Please sign in to comment.