pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

Simply opening a photo and saving it doubles the file size with Q=100

Open mcosti opened this issue 3 years ago • 3 comments

As the title says, simply doing this:

file = open(sys.argv[1], mode="rb")
image = pyvips.Image.new_from_buffer(file.read(), options="", access="sequential", autorotate=True)
image.write_to_file("applied_layer.jpg", Q=100)

Results in a photo being double the size. 1.8MB results in 3.6MB. I've tested this with 3 photos taken with different DSLR cameras and they all reproduce.

My code actually adds a watermark to the photo, so I don't want to have so much extra storage, but I don't want to lose any photo quality.

Could anyone help me with this? I uploaded a sample photo but I am not sure if github will save it properly to reproduce the issue Thanks

6C7B9450

mcosti avatar Nov 27 '22 16:11 mcosti

Hello @mcosti,

Like most systems, libvips automatically disables chroma subsample if Q >= 90. Try:

$ vips copy sample.jpg x.jpg[subsample-mode=on,Q=100]
$ ls -l sample.jpg x.jpg
-rw-rw-r-- 1 john john 1837881 Nov 27 17:31 sample.jpg
-rw-r--r-- 1 john john 2578151 Nov 27 17:33 x.jpg

So they are closer in size. I'm not sure why libvips output is still larger -- perhaps your original wasn't saved with Q=100?

jcupitt avatar Nov 27 '22 17:11 jcupitt

Hey @jcupitt , thanks for the quick answer. Indeed, that option helps reduce the file size by quite a lot. I tested also with a 6.2MB photo and the result was 8.6MB.

In reality, we are talking about photos taken straight out of the camera at full size, not processed at all.
So, this still increases the file size which I would prefer to avoid if possible. Maybe other people have ran into this and found a solution?

Thanks

mcosti avatar Nov 28 '22 08:11 mcosti

Camera JPG encoders will often produce images with slightly different file sizes. The Q factor is not a standardised feature and you can't compare it between different jpeg encoder libraries.

jcupitt avatar Nov 28 '22 08:11 jcupitt