https://github.com/robert-ancell/pygif
Python encoder and decoder for the GIF file format.
https://github.com/robert-ancell/pygif
gif python-library
Last synced: 10 months ago
JSON representation
Python encoder and decoder for the GIF file format.
- Host: GitHub
- URL: https://github.com/robert-ancell/pygif
- Owner: robert-ancell
- License: lgpl-3.0
- Created: 2018-11-27T09:04:44.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-10-17T09:14:47.000Z (over 2 years ago)
- Last Synced: 2025-04-20T17:40:41.566Z (11 months ago)
- Topics: gif, python-library
- Language: Python
- Homepage:
- Size: 318 KB
- Stars: 12
- Watchers: 2
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
This repository contains a Python encoder and decoder for the [GIF file format](https://www.w3.org/Graphics/GIF/spec-gif89a.txt).
The easiest way to get PyGIF is to install from the [Python Packaging Index](https://pypi.org/project/pygif/):
```
pip install pygif
```
The following will generate an 8x8 checkerboard image:
```python
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:
```python
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]
```