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.5.tar.gz (114.0 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.5-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pygif-0.5.tar.gz
  • Upload date:
  • Size: 114.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygif-0.5.tar.gz
Algorithm Hash digest
SHA256 3c700240bf97f1872843306e8f1734d40c15d07543decc0d646e5f9ebeddcbb4
MD5 9e75683f1234a1be4d74cfd9161d2732
BLAKE2b-256 0ce7e9a055424a27301504d291c71af093b01b55aa02206fa2392b9dc3d874c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygif-0.5.tar.gz:

Publisher: publish.yml on robert-ancell/pygif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pygif-0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygif-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c45d0719da98c2e8f5306415c434ee2f58bf28001ab95d81f70918b455129a7d
MD5 91df89fc70211a3f2da6d99c02e0664b
BLAKE2b-256 6a90bcdff2edebbeed24cf48a44e0e897b5963f399068421b30a86393120bafb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygif-0.5-py3-none-any.whl:

Publisher: publish.yml on robert-ancell/pygif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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