GIF & Animated Images

How to Reverse a GIF and Make a Back-and-Forth Loop

To reverse a GIF, decode every frame to its full picture first, then write those pictures in the opposite order, each with its own delay. Reversing the raw frames breaks GIFs that store only the changed area of each frame. For a back-and-forth (ping-pong) loop, play the frames forward and then backward without repeating the first and last picture, so N frames become 2N − 2. This is about reversing the animation; it is not a reverse image search.

By Tubelexity / Updated

Open Reverse GIF

Why a GIF cannot be reversed block by block

Many GIF frames are not complete pictures. A frame can store a small rectangle and a disposal rule: leave it, clear it, or restore what was there before. The picture you see is the result of all earlier frames plus those rules. Reverse the stored rectangles and each one lands on the wrong background.

A 4 × 4 test GIF shows the problem. Frame 1 is red for 100 ms; frame 2 stores only a 2 × 2 green square for 250 ms and asks to be undone; frame 3 stores one blue pixel for 40 ms and asks for its area to be cleared; frame 4 stores two pixels, one transparent, for 70 ms. Decoded first, the reversed file plays the full pictures 4, 3, 2, 1 with 70, 40, 250 and 100 ms. An independent decoder (libvips) returns exactly those pictures and delays from the exported file.

Ping-pong: where the loop turns around

A back-and-forth loop needs a rule for its end frames. Repeat them and the animation pauses at each turn. Tubelexity offers two explicit endings.

  • Use the seamless setting when the GIF loops forever.
  • Use End on the first frame when it plays once and should come to rest where it began.
  • A one-frame GIF does not change. A two-frame seamless ping-pong has the same order as the original, so the page suggests the ending option instead.
Frame order for a 4-frame GIF (frames numbered from 1)
SettingFrame orderFrames for N inputs
Reverse4, 3, 2, 1N
Ping-pong, seamless loop1, 2, 3, 4, 3, 2 (then 1 again)2N − 2
Ping-pong, end on the first frame1, 2, 3, 4, 3, 2, 12N − 1
Ping-pong, backward first, seamless4, 3, 2, 1, 2, 32N − 2

Timing and the loop count

Delays belong to pictures, so they move with them. In the measured sample below, the 400 ms hold on the first frame becomes the hold on the last frame of the reversed file. GIF stores delays in hundredths of a second. A delay of 0 is read as 0.1 s, which is how browsers play it, and written that way.

The loop field is copied unless you change it. Forever and Play once are explicit. A custom count is written to the GIF's loop field, but players do not agree on whether it counts total plays or repeats after the first play, so test it where the GIF will be used.

Measured example: size after reversing

A reproducible 160 × 120 sample with 12 frames (a gradient and a moving disc, 400 ms holds at each end and 80 ms between) was 77,194 bytes. Reversed, it was also 77,194 bytes, with the same 12 delays in the opposite order. A seamless ping-pong had 22 frames and was 141,557 bytes, because almost every picture is stored twice. Decoding both exports with libvips and comparing them with the source pictures found no pixel differences.

The GIF is encoded again with a palette per frame. When a frame already has 255 colours or fewer, as in this sample, those colours are written exactly. A frame that needs more colours than the palette setting allows is reduced again. If the result is too large, compress it as a separate step so you can compare.

Recommended workflow

  1. Open Reverse GIF and choose Upload GIF. Check the source line: size, frame count, total duration and loop.
  2. Leave Direction on Reverse (play backwards), or choose Ping-pong (forward, then backward).
  3. For ping-pong, pick Forward first or Backward first, then Seamless loop or End on the first frame.
  4. Check the Frame order line and the Planned playback preview before exporting.
  5. Choose Reverse GIF or Create ping-pong GIF, compare the measured size, and select Download GIF.
Open Reverse GIF

Frequently asked questions

Can I reverse a GIF without losing quality?

The pictures and delays are kept and the GIF is encoded again. Frames with 255 colours or fewer keep their exact colours; in our 12-frame test the reversed file matched the source pixel for pixel. Lowering Palette colors reduces colours on purpose.

How do I make a GIF play forward and then backward?

Choose Ping-pong. The seamless setting plays 1 to N and back to 2, so the loop does not stall on the end frames.

Why is my ping-pong GIF almost twice as large?

Most pictures appear twice, once each way. A 12-frame sample went from 77,194 to 141,557 bytes as a 22-frame ping-pong.

Is this the same as a reverse GIF search?

No. A reverse image search looks for where a GIF was published. This changes the direction the animation plays.

Sources and technical references

Related reading and tools