All formats

Pillow

LanguagePython
LicenseMIT-CMU (permissive)
Repositorygithub.com/python-pillow/Pillow
DocumentationTGA in the Pillow file format docs
Depends onnothing for TGA (wheels include all native code)
Read / writeyes / yes; modes L, LA, P, RGB, RGBA (and 1-bit), raw or RLE

Pillow reads and writes TGA in grayscale, grayscale with alpha, palette, RGB and RGBA modes, uncompressed or run-length encoded. It keeps the image ID field and the origin in im.info, so re-saving a file keeps its compression, ID and orientation unless you override them. 15 and 16-bit files are read and converted to RGB/RGBA.

Installation#

Shell
pip install pillow

Basic usage: open a TGA and save it as PNG#

Python
from PIL import Image

with Image.open("in.tga") as im:
    print(im.size, im.mode)  # e.g. (512, 512) RGBA
    print(im.info)           # orientation, compression, id_section
    im.save("out.png")

How to convert PNG to TGA with RLE compression#

Python
from PIL import Image

with Image.open("in.png") as im:
    im.save("out.tga", compression="tga_rle")

Without compression, a new TGA is written uncompressed. A TGA opened from an RLE file keeps RLE when re-saved; pass compression=None to write it raw.

How to save a TGA with a top-left origin#

By default Pillow writes rows bottom to top (the TGA default). Some tools expect top-down files:

Python
im.save("out.tga", orientation=1)  # 1: top-left origin, -1: bottom-left

How to read and set the image ID field#

Python
from PIL import Image

with Image.open("in.tga") as im:
    print(im.info.get("id_section"))  # bytes, or None if empty
    im.save("out.tga", id_section=b"made with Pillow")  # up to 255 bytes

How to check if a TGA has alpha#

Python
from PIL import Image

with Image.open("in.tga") as im:
    has_alpha = im.mode in ("RGBA", "LA") or "transparency" in im.info
    print(im.mode, "alpha" if has_alpha else "no alpha")

How to convert a folder of TGA files to PNG#

Python
from pathlib import Path
from PIL import Image

for path in Path("textures").glob("*.tga"):
    with Image.open(path) as im:
        im.save(path.with_suffix(".png"), optimize=True)

Tips#