From f33496500f63dd6c7c64be41edcb3e2d165fb94c Mon Sep 17 00:00:00 2001 From: Philip Gutjahr Date: Wed, 16 Mar 2022 09:13:23 +0100 Subject: [PATCH 1/2] use pillow to save higher-quality jpg (w/o color subsampling) --- utils/plots.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/plots.py b/utils/plots.py index 6c3f5bcaef37..604b206ae29b 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -467,5 +467,7 @@ def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BG crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)] if save: file.parent.mkdir(parents=True, exist_ok=True) # make directory - cv2.imwrite(str(increment_path(file).with_suffix('.jpg')), crop) + # use pillow to save higher-quality jpg (w/o color subsampling) + crop_pil = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)) + crop_pil.save(str(increment_path(file).with_suffix('.jpg')), quality=95, subsampling=0) return crop From 4c2b631cfae948b72bfe502af290e0bfda021f06 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 20 Mar 2022 15:42:03 +0100 Subject: [PATCH 2/2] Cleanup and doc issue --- utils/plots.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/plots.py b/utils/plots.py index d14a9e6dc8ce..a30c0faf962a 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -458,7 +458,7 @@ def profile_idetection(start=0, stop=0, labels=(), save_dir=''): plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200) -def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, save=True): +def save_one_box(xyxy, im, file=Path('im.jpg'), gain=1.02, pad=10, square=False, BGR=False, save=True): # Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop xyxy = torch.tensor(xyxy).view(-1, 4) b = xyxy2xywh(xyxy) # boxes @@ -470,7 +470,7 @@ def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BG crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)] if save: file.parent.mkdir(parents=True, exist_ok=True) # make directory - # use pillow to save higher-quality jpg (w/o color subsampling) - crop_pil = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)) - crop_pil.save(str(increment_path(file).with_suffix('.jpg')), quality=95, subsampling=0) + f = str(increment_path(file).with_suffix('.jpg')) + # cv2.imwrite(f, crop) # https://github.com/ultralytics/yolov5/issues/7007 chroma subsampling issue + Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)).save(f, quality=95, subsampling=0) return crop