Pillow, sharp or mozjpeg for a white-background product photo? Same quality, different stray pixels
Against sharp’s default at the same quality and sampling, the mozjpeg files were 38 to 41% smaller at quality 75, 23 to 28% at 85 and 13 to 20% at 95, per photo. The stray share dropped too. The last two rows are the pair I’d point to: mozjpeg at quality 85 with 4:4:4 ended at about the same stray share as Pillow at 95, in under half the bytes.
Look for the orange sharp mozjpeg line sitting lower and further left than every other one in both panels, meaning smaller files with fewer stray pixels. The ImgIng convert line sits on top of the sharp default line in the left panel. Photos by Dan Bollinger, Kritzolina and JustinLiang522 from Wikimedia Commons, CC BY-SA 4.0, adapted here under the same license.
Switching it on
In Pillow subsampling is 2 for 4:2:0 and 0 for 4:4:4. In sharp the mozjpeg switch is one extra key, and nothing else differs between the second and third rows of the table.
src.save("shoe_pil.jpg", quality=85, subsampling=2) bg = cut == 0 # alpha of the cutout, 0 = left empty share = 100 * (np.array(Image.open(p).convert("RGB"))[bg] != 255).any(axis=1).mean() # pil 216 KB 0.656 % sharp 210 KB 0.656 % moz 162 KB 0.451 %
const opts = { quality: 85, chromaSubsampling: "4:2:0" }; await sharp("shoe_white.png").jpeg(opts).toFile("shoe_sharp.jpg"); await sharp("shoe_white.png").jpeg({ ...opts, mozjpeg: true }).toFile("shoe_moz.jpg");
The comment is the real output for the sneaker photo.
What I’d pick and what’s left
For a white product photo I’d use sharp with mozjpeg: true, at quality 85 with 4:4:4 when the stray share matters and with 4:2:0 when the file size does. Chroma sampling mattered more than quality in my numbers: moving Pillow from 4:2:0 to 4:4:4 at quality 85 roughly halved the stray share, and going from 85 to 95 at 4:2:0 cut it by about a third.
Nothing reached zero. Pillow at quality 100 with 4:4:4 still had 0.035% stray pixels at an average of 538 KB, and mozjpeg at 95 with 4:4:4 had 0.045%. So I’d check the finished JPEG instead of trusting a quality number. That’s three photos on one machine, so the numbers aren’t a promise about your images. I haven’t tried other mozjpeg builds, or checked whether any marketplace rejects a background that reads 254 in a few pixels.
Fuente: Artículo original