Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressed GIF is larger than Original #154

Open
nunoperalta opened this issue May 1, 2020 · 9 comments
Open

Compressed GIF is larger than Original #154

nunoperalta opened this issue May 1, 2020 · 9 comments

Comments

@nunoperalta
Copy link

Please let me know of some email or place where I can send the original GIF used for this.

Several GIFs uploaded by our members become larger than the original, even though I only use gifsicle to process them. The main reason I use gifsicle is to reduce the space of uploaded GIFs.

Example here:

$ /usr/local/bin/gifsicle -O3 -Okeep-empty --lossy=80 --careful --no-extensions --resize-fit 720x720 Original.gif -o Compressed1.gif

$ /usr/local/bin/gifsicle -O3 -Okeep-empty --lossy=80 --careful --no-extensions Original.gif -o Compressed2.gif

$ /usr/local/bin/gifsicle -O3 -Okeep-empty --lossy=80 --no-extensions Original.gif -o Compressed3.gif

$ /usr/local/bin/gifsicle -O2 -Okeep-empty --lossy=80 --no-extensions Original.gif -o Compressed4.gif

$ /usr/local/bin/gifsicle -O3 -Okeep-empty --no-extensions Original.gif -o Compressed5.gif

$ ls -l
1010151 Original.gif
1353192 Compressed1.gif
1353192 Compressed2.gif
1349871 Compressed3.gif
1382477 Compressed4.gif
1076116 Compressed5.gif

"lossy" seems to have the biggest impact in making the file size larger.

Without lossy, and only with "-O3" the file is still slightly larger than the original.

@nunoperalta
Copy link
Author

Any ideas @denji ? @kohler ? @kornelski ?

@kornelski
Copy link
Contributor

kornelski commented May 5, 2020

First, if you're resizing, it's very common for resized GIF to take much more space than the original. Resizing adds more soft edges and more colors, which are two worst things for the GIF compression.

Lossy works well on GIFs that are video-like, with lots of similar colors and gradients. If your GIF is simple with hard edges, then there's nothing to gain from lossy.

I don't think there's much to be done about this. Don't assume recompression will always make images smaller. Keep the original image if the original is smaller.

Also, if you have GIFs that are MB in size, and care about file sizes, then don't use GIF. Convert them to MP4 or AV1 (APNG and AWebP are "meh", and may be larger in some cases too). Safari can even play MP4 directly from <img> tag, and you'll get 10x file size improvement.

@nunoperalta
Copy link
Author

Thank you for your comments.

The system doesn't know anything about the GIF, since it's anything that the user uploads.

Resizing is important, so the images don't cross the maximum width/height.
Very strange that it can increase the file size. I didn't know about that. I could only expect it to decrease, since we're reducing the number of pixels per frame.

Note that the biggest factor for increasing the file size was the "lossy" option.
I understand that in many images "there's nothing to gain from lossy". However, again, I wouldn't expect it to increase the size. Only to reduce or keep the same size in the worst case scenario.

I appreciate the suggestion of converting to movie. That seems to be more work, so have to put in the backlog for now :)
Seems that the best option is to resize the GIF (as it's a requirement), then try "lossy", compare the file sizes, and go with the smallest.

Have a good day.

@nunoperalta
Copy link
Author

Just to note, though,
in the example above, resizing actually has no impact at all. The image is smaller than 720x720, so the "resize-fit" option does nothing.

The problem is more the "optimizations":

$ /usr/local/bin/gifsicle -O3 -Okeep-empty --no-extensions Original.gif -o Compressed5.gif

$ ls -l
1010151 Original.gif
1076116 Compressed5.gif

Asking to optimize, with either -O1, -O2 or -O3, the file size increases.
This is what doesn't make sense to me. That's not an optimization.

This does happen to a number of GIF images.

@kornelski
Copy link
Contributor

It increases even without lossy? You must have a pretty well-compressed GIF to begin with.

@nunoperalta
Copy link
Author

Yes, that's what my examples above were saying.

--lossy has the biggest impact on causing the file to increase the most.

Without it, the file still increases.

Even this simple command:

$ /usr/local/bin/gifsicle Original.gif -o Compressed6.gif

The file increases.

I wish there was a way to 100% turn off optimizations.

For example, I may want to ONLY resize the GIF file (as that's the requirement).

THEN, I want to try optimizations, and see if the file reduced. If didn't, then I discard the optimization.

However, the way it works now, if I try to use the "resize-fit" option without any other options, gifsicle will default to -O1 (I think?), which can increase the file size.

@kornelski
Copy link
Contributor

kornelski commented May 30, 2020

Technically there doesn't exist such thing as resizing images. Resizing is done by generating a brand new image with new pixels and new compression. It's not modifying the old file to somehow make it have a different size.

There is no way around that in raster formats like GIF (apart from an uninteresting case where you request resizing to the same size, so there's no resizing to do). Compression and pixel dimensions are absolutely inseparable, so making new image size always requires making a new file with new compression done from scratch.

@nunoperalta
Copy link
Author

nunoperalta commented May 30, 2020

Thank you. I understand.

But see this:

$ /usr/local/bin/gifsicle --resize-fit 720x720 Original.gif -o Compressed6.gif
$ ls -l
1010151 Original.gif
1058866 Compressed6.gif

The image is smaller than 720x720, so it should not resize at all (and does not, but the file size increases).

What else is gifsicle doing, that causes the file size to increase from 1010151 bytes to 1058866 bytes?

I assume that gifsicle is trying the first level of optimizations, as per the documentation:

https://www.lcdf.org/gifsicle/man.html

-O1
Stores only the changed portion of each image. This is the default.

Is it possible to 100% disable any optimizations?
Meaning, only do the resizing work (if needed), without attempting to do other optimizations at all.

Thanks!

@kornelski
Copy link
Contributor

This isn't caused by optimizations, but by recompression. I don't think it's possible to tell gifsicle not to recompress anything.

You could check if the image already fits your required size, and then not run gifsicle at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants