'Pillow Python3 - "ValueError: cannot allocate more than 256 colors" using ImageOps to GIF format
def addBorder():
r = requests.get("https://estra-source.herokuapp.com/assets/images/Smile/Smile4.gif")
with open("Image.gif",'wb') as f:
f.write(r.content)
a = Image.open(BytesIO(r.content))
print(a.is_animated)
print(a.n_frames)
print(a.mode)
width = 10
color = 'black'
for frame in range(0, a.n_frames):
if isinstance(width, int) or isinstance(width, tuple):
bimg = ImageOps.expand(a, border=width, fill=color)
bimg.seek(frame)
bimg.save("Test.gif", save_all=True, loop=0)
I want to make a function that add border to a GIF, UPDATE: I got new error EOFError. I added the GIF that I mention, Hope it help my question.
Solution 1:[1]
The GIF format only supports a maximum of 256 colours because it is a palettised format, a.k.a. indexed. Description here.
As you have not shared your image (same as your previous question), I cannot say for sure but I assume it already has its maximum possible 256 colours and you are trying to introduce a new one.
Your options are either:
find a colour close to the new border colour from the existing palette and use that instead
convert your image to RGB mode before adding the border so it is no longer palettised
reduce the number of colours in your image so you have space to allocate a new one
I m a bit busy at the moment, but if you do this:
magick https://estra-source.herokuapp.com/assets/images/Smile/Smile4.gif -format 'Frame: %s , Colours: %k\n' info:
You will see that you are right on the threshold of the max colours in a GIF:
Frame: 0 , Colours: 255
Frame: 1 , Colours: 255
Frame: 2 , Colours: 256
Frame: 3 , Colours: 256
Frame: 4 , Colours: 256
Frame: 5 , Colours: 256
Frame: 6 , Colours: 255
Frame: 7 , Colours: 256
Frame: 8 , Colours: 256
Frame: 9 , Colours: 256
Frame: 10 , Colours: 255
Frame: 11 , Colours: 256
Frame: 12 , Colours: 256
Frame: 13 , Colours: 256
Frame: 14 , Colours: 241
Frame: 15 , Colours: 256
Frame: 16 , Colours: 256
Frame: 17 , Colours: 250
Frame: 18 , Colours: 256
Frame: 19 , Colours: 253
Frame: 20 , Colours: 247
Frame: 21 , Colours: 255
Frame: 22 , Colours: 254
Frame: 23 , Colours: 256
Frame: 24 , Colours: 254
Frame: 25 , Colours: 256
Frame: 26 , Colours: 256
Frame: 27 , Colours: 254
Frame: 28 , Colours: 237
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
