Why does a GIF increase in size when I add a logo although number of colors stays the same?
I have an animated GIF that's about 2 MB large. When I open it in Photoshop CS 5, add a layer with a logo, then save it for web, its suddenly 4 MB large.
Why? There aren't more colors or anything, so why does the size increase?
The image in question is NSFW and I cannot post it here.
Color number is just half the game. The other is to compress the picture after color reduction. This lossless compression, searches for repeated patterns in scanline order.
Long story, in short: When you add the logo you are increasing the image variability, entropy. Compression gets worse the more entropy there is in the image, as the computer can nolonger find as many repeated patterns. Thus doubling complexity of image roughly speaking doubles your file size.
Although joojaa is mostly correct, actually GIFs do not use Run Length Encoding. They use the LZW algorithm.
Basically, this algorithm can take advantage of EXACT repetitions of horizontal strips of pixels. This works very well for solid colours and regular dithering patterns (e.g. checkerboard patterns).
However LZW can only "remember" 4096 different pixel strips, so the more variation in your image, the shorter these strips are on average, and the less compression you get.
The bottom line: if you simplify your logo (more solid colours & regular patterns), or "borrow" common pixel strips from the rest of the image within your logo, or change the colours of the logo to be colours that appear more frequently in the rest of the image, the file size should reduce.