Skip to main content

Pure Python GIF image encoder/decoder

Project description

This repository contains a Python encoder and decoder for the GIF file format.

The easiest way to get PyGIF is to install from the Python Packaging Index:

pip install pygif

The following will generate an 8x8 checkerboard image:

import gif

writer = gif.Writer (open ('checkerboard.gif', 'wb'))
writer.write_header ()
writer.write_screen_descriptor (8, 8, has_color_table = True, depth = 1)
writer.write_color_table ([(0, 0, 0), (255, 255, 255)], 1)
writer.write_image (8, 8, 1, [ 1, 0, 1, 0, 1, 0, 1, 0,
                               0, 1, 0, 1, 0, 1, 0, 1,
                               1, 0, 1, 0, 1, 0, 1, 0,
                               0, 1, 0, 1, 0, 1, 0, 1,
                               1, 0, 1, 0, 1, 0, 1, 0,
                               0, 1, 0, 1, 0, 1, 0, 1,
                               1, 0, 1, 0, 1, 0, 1, 0,
                               0, 1, 0, 1, 0, 1, 0, 1 ])
writer.write_trailer ()

The following will decode that image:

import gif

file = open ('checkerboard.gif', 'rb')
reader = gif.Reader ()
reader.feed (file.read ())
if reader.has_screen_descriptor ():
    print ('Size: %dx%d' % (reader.width, reader.height))
    print ('Colors: %s' % repr (reader.color_table))
    for block in reader.blocks:
        if isinstance (block, gif.Image):
            print ('Pixels: %s' % repr (block.get_pixels ()))
    if reader.has_unknown_block ():
        print ('Encountered unknown block')
    elif not reader.is_complete ():
        print ('Missing trailer')
else:
    print ('Not a valid GIF file')

Giving the following output:

Size: 8x8
Colors: [(0, 0, 0), (255, 255, 255)]
Pixels: [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygif-0.4.tar.gz (112.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygif-0.4-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file pygif-0.4.tar.gz.

File metadata

  • Download URL: pygif-0.4.tar.gz
  • Upload date:
  • Size: 112.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pygif-0.4.tar.gz
Algorithm Hash digest
SHA256 d58668bec02878d696d5f0815b64758846bdfad419ab61fa7d8d472250857fda
MD5 dc1bd7da501022049607f5ae03b0c690
BLAKE2b-256 efa9672b06cdaf5ce787ae07e394a971f574775fef01a1fc17ba38baff28b70b

See more details on using hashes here.

File details

Details for the file pygif-0.4-py3-none-any.whl.

File metadata

  • Download URL: pygif-0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pygif-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1c7da24a78e8ff4f21e65969da37c45709ff2353b8966f4e36109428fb13781e
MD5 d3725b5ae6f5b4a1cf3ce648a6c086b1
BLAKE2b-256 c385396250cf6e077692bd8a864907412240b0eaaeff163befc941e9ac81ba9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page