4
$\begingroup$

Recently, the image hosting was changed (New service failing on .gif uploads) and now sometimes I have troubles uploading a .gif:

Image uploads are limited to 50 megapixels; please use a smaller image

So what does it mean exactly? Width × height × number of frames?

Here are two examples of gifs I failed to upload:

Align Vertices (constantly changing locations)

How to make Spiral in Geometry nodes [ but without node spiral ]

You can download these gifs and try to reupload them to reproduce the issue. The way I embedded these gifs there is using an <img> tag:

<img src="https://i.imgur.com/7DqAAp8.gif">
<img src="https://i.imgur.com/NgTA9UU.gif">

However, I know embedding images like that is problematic, for example some visitors to this site have the Imgur domain blocked. It's just that using gifs was already introducing a lot of friction given the harsh 2 MB limit. There's also some compatibility issues (or used to be), that made it impossible to upload gifs after optimizing them in some software, so there's at least 3 separate requirements for uploading gifs.

$\endgroup$
3
  • 3
    $\begingroup$ "Width × height × number of frames?" This was also my uninformed guess, both image size and number of frames counts towards total pixel count somehow, I've also been encountering that error. According to meta.stackexchange.com/a/399146/334476 there was an intentional pixel size limit added to prevent delays. Not sure what to make of it, the limitation seems restrictive and undesirable, but not a really bug strictly speaking. I'll tag this as [feature-request] for now, see if we can convince @slate to up the limit $\endgroup$ Commented Jun 28, 2024 at 9:07
  • $\begingroup$ Width × height × number of frames definitely seems to be it. I had a 400kB gif with 288 frames of 175,420 pixels each (that comes to 50,520,960 pixels) and got this error. I dropped about 7 frames to be safe, bringing it to 49,293,020 pixels…but I also increased the number of colors, bringing the total file size to ~750kB. The new, larger file was able to be uploaded. If the limit is actually useful, I don't mind it being in place…but it needs a more clear error message. $\endgroup$ Commented Oct 8, 2024 at 2:09
  • $\begingroup$ This is really annoying. Cannot share GIFs... Those were useful... $\endgroup$ Commented Nov 17, 2024 at 23:30

2 Answers 2

2
$\begingroup$

Based on my initial guess and a confirmation in comments and my experience, the animated GIF/APNG requirements are:

  • maximum of 2 MB file size;
  • maximum of 50 000 000 pixels counted across all frames: width × height × number of frames;
  • the file shouldn't use fringe settings, so using the best APNG/GIF compressors will produce a GIF that can be opened in a browser but won't be accepted by Stack Exchange.

Here's how I fix my GIFs:

  1. Upload my GIF to Ezgif.com - unfortunately it can no longer read imgur links, so you need to upload a file from your computer; for those who use ShareX, dragging a thumbnail from ShareX onto the website works.
  2. Below uploaded GIF, notice size info, e.g.

File size: 3.77MiB, width: 307px, height: 474px, frames: 542

  1. Open a calculator, multiply dimensions together to get 1 frame area, then divide 50000000 by that area, this gives you the number of frames you need to decrease your GIF to.

$$307 × 474 = 145 518$$

$$50.000.000 ÷ 145 518 = 343.6001044544318$$

So you need to reduce the GIF to 343 frames

  1. If you need to reduce the count of frames by a lot, consider cropping, resizing, or in the worst case, cutting the GIF by moving the end near the half, remembering where it is, and uploading another GIF that you cut by moving the start to the same time.

  2. In Optimization settings, choose "Drop Frames: Remove every 2nd frame", click "Optimize GIF!"

  3. In Optimization settings again, choose "Drop Frames: Remove duplicate frames". Repeatedly choose a "Fuzz" setting, and click "Optimize GIF!" until you find the setting that reduces the number of frames below calculated threshold.

  4. You can right-click the generated GIF, copy image link, and use that on SE to embed an image.

  5. If you also link to a mirror (because SE image hosting has problems), upload the original GIF to Imgur, rather than linking to Ezgif. If a lot of people load the image from Ezgif, the IP originally uploading the file will get banned [denied by Ezgif creator]! Of course when you use the link to upload an image on Stack Exchange, it creates its copy, so it's fine.

$\endgroup$
6
  • $\begingroup$ late to the party but I guess there could be some way to somewhat automate this or at least make the optimization have less trials to do? $\endgroup$ Commented Dec 3, 2024 at 20:08
  • $\begingroup$ "If a lot of people load the image from Ezgif, the IP originally uploading the file will get banned!" - this is false, but the file will be deleted tho ;) $\endgroup$ Commented Jan 24 at 17:30
  • $\begingroup$ @Maadinsh I experienced it first-hand. That is, I shared a link to ez-gif on a community where possibly over 100 people loaded the image, and then I couldn't access EZ-gif service. Maybe it wasn't an IP ban but some other kind of a ban, but it happened quickly enough, and I'm using ez-gif so much without such issues, I ruled out a coincidence. Oh and changing my IP restored the service. $\endgroup$ Commented Jan 25 at 18:03
  • $\begingroup$ I'm the author of ezgif, and I can assure you there is no IP ban. Must be a coincidence with some network issue if it didn't work. In the 12 years of running it I have never banned an IP :) $\endgroup$ Commented Jan 26 at 10:18
  • $\begingroup$ @Maadinsh maybe you're using some library/service that has done it? I'll try to reproduce it in nearest future. BTW, thanks for the service, it's a lifesaver! $\endgroup$ Commented Jan 26 at 11:43
  • $\begingroup$ Nope, just a script that I wrote myself, that periodically parses access log and deletes the file if it gets more than 10 requests. Absolutely nothing is done with the IP. $\endgroup$ Commented Jan 26 at 19:17
1
$\begingroup$

I record my screen recordings as mkv files because it works for my process.

I use a bash script file I called mkv_to_gif.sh to deal with this (don't forget to make the file executable). At the end the total megapixels maybe more than 50 but Blender Stackexchange still allows posting.

Note: I only tested using Ubuntu 20.04 but it's very quick.

Tested with the above animated images as mkv files and converted to animated gifs using script below.

Note: I converted animated images to mkv files first using the code below.

ffmpeg -i test1.gif -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags faststart -pix_fmt yuv420p test1.mkv

ffmpeg -i test2.gif -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags faststart -pix_fmt yuv420p test2.mkv

Test1

Test2

#!/bin/bash

# Jan 02 2025
# Tested with 400x714, may need to limit to 640x480 or less
# Run using ./mkv_to_gif.sh animated_mkv.mkv

input_file="$1"
output_file="${input_file%.*}.gif"

# Get original dimensions and frame count
dimensions=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=width,height,nb_read_packets -of csv=p=0 "$input_file")
width=$(echo $dimensions | cut -d',' -f1)
height=$(echo $dimensions | cut -d',' -f2)
frames=$(echo $dimensions | cut -d',' -f3)

# Calculate total megapixels (width * height * frames)
calculate_total_megapixels() {
    awk "BEGIN {printf \"%.2f\", $1 * $2 * $3 / 1000000}"
}

total_megapixels=$(calculate_total_megapixels $width $height $frames)

echo "Original dimensions: ${width}x${height}"
echo "Original frames: $frames"
echo "Original total megapixels: $total_megapixels"

# Set initial fps and colors
fps=15
colors=256

# Function to reduce dimensions
reduce_dimensions() {
    local factor=0.99  # Reduced the factor to make significant dimension changes
    width=$(awk "BEGIN {print int($width * $factor)}")
    height=$(awk "BEGIN {print int($height * $factor)}")
    total_megapixels=$(calculate_total_megapixels $width $height $frames)
}

# Reduce dimensions if initially over 50 megapixels
while (( $(echo "$total_megapixels > 50" | bc -l) )); do
    reduce_dimensions
done

# Convert to GIF
ffmpeg -i "$input_file" -vf "fps=$fps,scale=$width:$height:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=$colors[p];[s1][p]paletteuse" -y "$output_file"

# Check file size and reduce quality if necessary
while [ $(stat -c%s "$output_file") -gt 2044723 ] || (( $(echo "$total_megapixels > 50" | bc -l) )); do
    if (( $(echo "$total_megapixels > 50" | bc -l) )); then
        reduce_dimensions
    elif [ $fps -gt 5 ]; then
        fps=$((fps - 1))
        frames=$((frames * fps / 15))  # Adjust frame count based on new fps
    elif [ $colors -gt 64 ]; then
        colors=$((colors / 2))
    else
        reduce_dimensions
    fi
    
    total_megapixels=$(calculate_total_megapixels $width $height $frames)
    ffmpeg -i "$input_file" -vf "fps=$fps,scale=$width:$height:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=$colors[p];[s1][p]paletteuse" -y "$output_file"
done

# Display final dimensions and file size
if command -v identify >/dev/null 2>&1; then
    final_dimensions=$(identify -format "%wx%h" "$output_file")
    final_width=${final_dimensions%x*}
    final_height=${final_dimensions#*x}
    final_frames=$(identify -format "%n\n" "$output_file" | wc -l)
    final_total_megapixels=$(calculate_total_megapixels $final_width $final_height $final_frames)
    echo "Final dimensions: $final_dimensions"
    echo "Final frames: $final_frames"
    echo "Final total megapixels: $final_total_megapixels"
else
    echo "ImageMagick's 'identify' command not found. Unable to determine final dimensions and total megapixels."
fi

final_size=$(du -h "$output_file" | cut -f1)
echo "Final file size: $final_size"

Code Explanation:

This Bash script converts an MKV video file to a GIF format while optimizing its size and quality. Here's a breakdown of the code:

  1. Input and Output:

    • The script takes an MKV file as input and generates a GIF with the same name.
  2. Initial Analysis:

    • Uses ffprobe to get the original video dimensions and frame count.
    • Calculates the total megapixels (width * height * frames).
  3. Initial Settings:

    • Sets initial fps (frames per second) to 15 and colors to 256.
  4. Dimension Reduction:

    • If the total megapixels exceed 50, it reduces the dimensions by a factor of 0.99 until it's below 50 megapixels.
  5. Initial Conversion:

    • Uses ffmpeg to convert the MKV to GIF with the current settings.
  6. Quality Reduction Loop:

    • If the resulting GIF is larger than ~2MB or exceeds 50 megapixels, it enters a loop to reduce quality: a. Reduces dimensions if still over 50 megapixels. b. Decreases fps if it's above 5. c. Reduces color count if it's above 64. d. If all else fails, reduces dimensions again.
    • After each adjustment, it reconverts the video and checks the size again.
  7. Final Output:

    • If ImageMagick is available, it uses the 'identify' command to get the final dimensions and frame count.
    • Displays the final dimensions, frame count, total megapixels, and file size.

This script aims to create a GIF that balances quality and file size, making iterative adjustments to achieve this balance.

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.