cross-posted from: https://linkage.ds8.zone/post/523771
Before posting an image to the fedi, I want to be mindful about the network burden it will cause. I’m only uploading the image once but potentially thousands of people could end up downloading it.
If it’s a color image, then JPG is typically best. This #ImageMagick command reduces the filesize quite a bit, trading off quality:
$ convert "$original_image_file" \ +dither \ -posterize 8 \ -sampling-factor 4:2:0 \ -strip \ -quality 75 \ -interlace Plane \ -gaussian-blur 0.05 \ -colorspace RGB \ -strip \ smaller_file.jpg
If it’s a pic of a person, this processing will likely be a disaster. But for most things where color doesn’t matter too much, it can be quite useful. Play with different
-posterize
values.If you can do with fewer pixels, adding a
-resize
helps.
$ convert "$original_image_file" -resize 215x smaller_file.jpg
If you can get away with black and white, jpeg is terrible. Use PNG instead. E.g.
$ convert "$original_image_file" -threshold 30% -type bilevel smaller_file.png
For privacy, strip the metadata
The ImageMagick
-strip
option supposedly strips out metadata. But it’s apparently not thorough because the following command yields a slightly smaller file size:
$ exiftool -all= image.jpg
What else?
Did I miss anything? Any opportunities to shrink images further? In principle the DjVu format would be more compact but it’s not mainstream and apparently not accepted by Lemmy.
Sometimes PNG is better, sometimes JPG. So if - for example - you make a screenshot of your desktop that has a photo as a background, a JPG is much smaller but still good quality. If it has only clear UI boxes and a background with only few clearly separated colors, PNG will be better and smaller.
There also exist tools to compress PNGs even more, like
pngquant
. Sometimes huge reduction! Come to think of it, similar tools also exist for JPG, e.g.jpegoptim
You didn’t name it but WEBP is the format you are looking for.
It’s been fully supported for so long already and has better compression than JPG and PNG by far. You could even technically compress it with specific algorithms for text, icons, photographs… Or simply the default compression algorithm.
You’ll encounter virtually 0 issues with it on the fediverse.
There are even better formats but these definitely are not as supported currently.
avif is also already fairly widely supported.
I have the same goals as you (lowering the file size and stripping metadata), but am much more lazy. I’ll bring up the image I want to share in a viewer on screen, then do a cropped screengrab. So it creates a file the size of screen resolution, has no metadata from a camera, and better yet I can copy from the screengrab and paste directly into a Lemmy post with no file saving needed in the middle.