Skip to content

Commit

Permalink
use pillow to save higher-quality jpg (w/o color subsampling)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaserBorg committed Mar 16, 2022
1 parent c09fb2a commit f334965
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 comment on commit f334965

@LaserBorg
Copy link
Contributor Author

@LaserBorg LaserBorg commented on f334965 Mar 16, 2022

Choose a reason for hiding this comment

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

opencv jpg compression does not support 'excellent' image quality since even the hightest quality setting uses 2x2 chroma subsampling (4:2:2), meaning that color information has only half resolution. This results in image artifacts on edges where colors change rapidly, especially when colors are highly saturated.
By using pillow, chroma subsampling can get disabled and therefore image quality raised.

Please sign in to comment.