Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slicing: Quality focused default image format selection and jpg quality #956

Merged
merged 7 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI using quality="keep" fails (silently) for jpg ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @fcakyon - I'd raise an issue but these don't seem to be available on this repo. I'd probably try/except the code executed in the thread pool too, just so we don't silently fail again.

Copy link
Contributor Author

@jokober jokober Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the hint. Can you try with following line (only for jpegs)?

image_pil.save(slice_file_path, 'JPEG', quality="keep")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I just deleted the quality parameter to make it work = ) It looks like it won't work as this still errors for me.

from PIL import Image
Image.new("RGB", (100, 100)).save("tmp.jpg", "JPEG", quality="keep")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh from above:

According to the pillow documentation: "The value keep is only valid for JPEG files and will retain the original image quality level, subsampling, and qtables."

The problem is these aren't JPG files, these are slices of numpy arrays or similar, so I don't think anything will be kept. Maybe just revert to the quality argument you had?

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