Your basic approach (which is implemented in Austin's answer) will work for a while, but it is important to remember that quality! = File size . Although they tend to correlate, it is possible (even general) that a decrease in the quality of the jpeg file will actually lead to a LARGER file. This is due to the fact that any JPEG downloaded to your system is already performed using the JPEG compression formula (often with quality 79 or 80). Depending on the source image, this process will create artifacts / modify the resulting image. When you run this already optimized image using the jpeg compression algorithm for the second time, it doesn’t “know” what the original image looked like ... so it processes the incoming jpeg as if it is a completely new file without losses and tries to copy as much it's possible ... including any artifacts created in the original process. Combine this with the fact that the original jpeg compression has already used most of the “simple” compression tricks; in the end, it is likely that the compression will create a crappier image (copy of the copy problem) for the second time, but not less than the file.
I did some tests to see where the crop was, and it is not surprising if the original image had a low compression ratio (q = 99), a lot of space was saved with re-compression to q = 75. If the original was compressed at q = 75 (quite common for graphical program defaults), the secondary compression q = 75 looked worse, but resulted in the same file size as the original. If the initial level had a lower compression level (q = 50), then the secondary compression q = 75 led to a significantly larger file (for these tests I used three complex photographs ... it is obvious that images with specific tastes / compositions will have different characteristics through these compression). Note. I use Fireworks cs4 for this test ... I understand that these quality indicators do not have standardization between platforms .
As noted in the comments below, the transition from file formats such as PNG to JPEG will ultimately be much smaller (albeit without any transparency), but from JPEG → JPEG (or GIF-> JPEG, especially for simple or small format , heaven) often does not help.
Regardless, you can still try using the compression method described by Austin, but make sure you compare the file sizes of the two images when you're done. If there is a small increase or a new file is larger, then the original image is returned by default.
Ben D Sep 24 2018-12-12T00: 00Z
source share