title: Adding colored text to 256-color palette gif with Pillow
body: <p>I'm trying to add a colored text to a gif (256-color palette). However, I get the following error:</p>
  <blockquote>
  <p>ValueError: cannot allocate more than 256 colors</p>
  </blockquote>
  <p>The code:</p>
  <pre><code>img = Image.open(&quot;GIF.gif&quot;)
  frames = []
  for frame in ImageSequence.Iterator(img):
      d = ImageDraw.Draw(frame)
      width, height = img.size
      d1 = ImageDraw.Draw(img)
      font = ImageFont.truetype(&quot;arial.ttf&quot;, 40)
      d1.text((width/2, height-80), caption, font=font, anchor=&quot;ms&quot;, fill =(255, 225, 0), stroke_width=4, stroke_fill=&quot;#000000&quot;)
      b = io.BytesIO()
      frame.save(b, format=&quot;GIF&quot;)
      frame = Image.open(b)
      frames.append(frame)
  frames[0].save('out.gif', save_all=True, append_images=frames[1:])
  </code></pre>
  <p>Brief explanation of the code: This is a snippet from a fine-working script. Steps before, it saves a batch of video frames from a video file and later then it renders the images as a .gif file. In this code snippet I try to open the .gif and write some text in it, but it can't be saved because the gif has a 256 color palette already and adding this text adds two more colors which aren't in the palette. The error is explained in the first lines of the post.</p>
  <p>How can I solve this issue?</p>