Actually - JPEG is not lossless even at 100% quality, there is a little noise at +/-1 of pixel value, some rare pixels at 2. Grayscale is not needed, you may skip RGB->YUV conversion and encode in RGB colors, libjpeg can do that.
Re:Visualizing and Understanding JPEG Format
by unnouinceput on Friday July 10, 2020 @02:42AM
(#23788836)
1 - No.
2 - JPEG algorithm allows to have lossless data without padding
3 - For lossless data rar/arc/ace are better then JPEG
> to make JPEG store data losslessly: use grayscale, 100% quality, then either width or eight to 1 pixel, or duplicate the padded data 8 times (JPEG images are stored in 8x8 blocks).
Interesting. I wonder if this is used internally in any applications.