title: Pillow - Resizing a GIF
body: <p>I have a <code>gif</code> that I would like to resize with <code>pillow</code> so that its size decreases. The current size of the <code>gif</code> is 2MB. </p>
  
  <p>I am trying to </p>
  
  <ol>
  <li><p>resize it so its height / width is smaller</p></li>
  <li><p>decrease its quality. </p></li>
  </ol>
  
  <p>With JPEG, the following piece of code is usually enough so that large image drastically decrease in size.</p>
  
  <pre><code>from PIL import Image
  
  im = Image.open("my_picture.jpg")
  im = im.resize((im.size[0] // 2, im.size[1] // 2), Image.ANTIALIAS)  # decreases width and height of the image
  im.save("out.jpg", optimize=True, quality=85)  # decreases its quality
  </code></pre>
  
  <p>With a GIF, though, it does not seem to work. The following piece of code even makes the <code>out.gif</code> bigger than the initial gif:</p>
  
  <pre><code>im = Image.open("my_gif.gif")
  im.seek(im.tell() + 1)  # loads all frames
  im.save("out.gif", save_all=True, optimize=True, quality=10)  # should decrease its quality
  
  print(os.stat("my_gif.gif").st_size)  # 2096558 bytes / roughly 2MB
  print(os.stat("out.gif").st_size)  # 7536404 bytes / roughly 7.5MB
  </code></pre>
  
  <p>If I add the following line, then only the first frame of the GIF is saved, instead of all of its frame.</p>
  
  <pre><code>im = im.resize((im.size[0] // 2, im.size[1] // 2), Image.ANTIALIAS)  # should decrease its size
  </code></pre>
  
  <p>I've been thinking about calling <code>resize()</code> on <code>im.seek()</code> or <code>im.tell()</code> but neither of these methods return an Image object, and therefore I cannot call <code>resize()</code> on their output.</p>
  
  <p>Would you know how I can use Pillow to decrease the size of my GIF while keeping all of its frames?</p>
  
  <p>[edit] Partial solution:</p>
  
  <p>Following <a href="https://stackoverflow.com/a/41755454/2564199">Old Bear's response</a>, I have done the following changes:</p>
  
  <ul>
  <li><p>I am using <a href="https://gist.github.com/BigglesZX/4016539" rel="noreferrer">BigglesZX's script</a> to extract all frames. It is useful to note that this is a Python 2 script, and my project is written in Python 3 (I did mention that detail initially, but it was edited out by the Stack Overflow Community). Running <code>2to3 -w gifextract.py</code> makes that script compatible with Python 3.</p></li>
  <li><p>I have been resicing each frame individually: <code>frame.resize((frame.size[0] // 2, frame.size[1] // 2), Image.ANTIALIAS)</code></p></li>
  <li><p>I've been saving all the frames together: <code>img.save("out.gif", save_all=True, optimize=True)</code>.</p></li>
  </ul>
  
  <p>The new gif is now saved and works, but there is 2 main problems :</p>
  
  <ul>
  <li><p>I am not sure that the resize method works, as <code>out.gif</code> is still 7.5MB. The initial gif was 2MB.</p></li>
  <li><p>The gif speed is increased and the gif does not loop. It stops after its first run.</p></li>
  </ul>
  
  <p>Example:</p>
  
  <p>original gif <code>my_gif.gif</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/WARSx.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/WARSx.gif" alt="Original gif"></a></p>
  
  <p>Gif after processing (<code>out.gif</code>) <a href="https://i.imgur.com/zDO4cE4.mp4" rel="noreferrer">https://i.imgur.com/zDO4cE4.mp4</a> (I could not add it to Stack Overflow ). Imgur made it slower (and converted it to mp4). When I open the gif file from my computer, the entire gif lasts about 1.5 seconds.</p>