Home > Blockchain >  how do i increase the resolution of my gif file?
how do i increase the resolution of my gif file?

Time:03-26

As I am trying to create a gif file, the file has been created successfully but it is pixelating. So if anyone can help me out with how to increase resolution.

.Here is the code:-

import PIL
from PIL import Image
import NumPy as np

image_frames = []
days = np.arange(0, 12)

for i in days:
    new_frame = PIL.Image.open(
        r"C:\Users\Harsh Kotecha\PycharmProjects\pythonProject1\totalprecipplot"   "//"   str(i)   ".jpg"
    )
    image_frames.append(new_frame)

image_frames[0].save(
    "precipitation.gif",
    format="GIF",
    append_images=image_frames[1:],
    save_all="true",
    duration=800,
    loop=0,
    quality=100,
)

Here is the Gif file:-

enter image description here

Here are the original images:-

enter image description here

Original Answer

Your original images are JPEGs which means they likely have many thousands of colours 2. When you make an animated GIF (or even a static GIF) each frame can only have 256 colours in its palette.

This can create several problems:

  • each frame gets a new, distinct palette stored with it, thereby increasing the size of the GIF (each palette is 0.75kB)

  • colours get dithered in an attempt to make the image look as close as possible to the original colours

  • different colours can get chosen for frames that are nearly identical which means colours flicker between distinct shades on successive frames - can cause "twinkling" like stars

If you want to learn about GIFs, you can learn 3,872 times as much as I will ever know by reading Anthony Thyssen's excellent notes here, here and here.

Your image is suffering from the first problem because it has 12 "per frame" local colour tables as well as a global colour table3. It is also suffering from the second problem - dithering.

To avoid the dithering, you probably want to do some of the following:

  • load all images and append them all together into a 12x1 monster image, and find the best palette for all the colours. As all your images are very similar, I think that you'll get away with generating a palette just from the first image without needing to montage all 12 - that'll be quicker

  • now palettize each image, with dithering disabled and using the single common palette

  • save your animated sequence of the palletised images, pushing in the singe common palette from the first step above


2: You can count the number of colours in an image with ImageMagick, using:

magick YOURIMAGE -format %k info: 

3: You can see the colour tables in a GIF with gifsicle using:

gifsicle -I YOURIMAGE.GIF
  • Related