pyffi.formats.tga
— Targa (.tga)¶
Implementation¶
-
class
pyffi.formats.tga.
TgaFormat
¶ Bases:
pyffi.object_models.xml.FileFormat
This class implements the TGA format.
-
class
ColorMapType
(**kwargs)¶ Bases:
pyffi.object_models.xml.enum.EnumBase
An unsigned 8-bit integer, describing the color map type.
Bases:
pyffi.object_models.xml.basic.BasicBase
The Targa footer signature.
Return a hash value for the signature.
Returns: An immutable object that can be used as a hash.
Return number of bytes that the signature occupies in a file.
Returns: Number of bytes.
Get signature.
Returns: The signature.
Read signature from stream.
Parameters: stream (file) – The stream to read from.
Set signature.
Parameters: value (str) – The value to assign.
Write signature to stream.
Parameters: stream (file) – The stream to read from.
-
class
ImageType
(**kwargs)¶ Bases:
pyffi.object_models.xml.enum.EnumBase
An unsigned 8-bit integer, describing the image type.
-
PixelData
¶ alias of
UndecodedData
-
byte
¶ alias of
Byte
-
char
¶ alias of
Char
-
float
¶ alias of
Float
-
int
¶ alias of
Int
-
short
¶ alias of
Short
-
ubyte
¶ alias of
UByte
-
uint
¶ alias of
UInt
-
ushort
¶ alias of
UShort
-
class
Regression tests¶
Read a TGA file¶
>>> # check and read tga file
>>> import os
>>> from os.path import dirname
>>> dirpath = __file__
>>> for i in range(4): #recurse up to root repo dir
... dirpath = dirname(dirpath)
>>> repo_root = dirpath
>>> format_root = os.path.join(repo_root, 'tests', 'formats', 'tga')
>>> file = os.path.join(format_root, 'test.tga').replace("\\", "/")
>>> stream = open(file, 'rb')
>>> data = TgaFormat.Data()
>>> data.inspect(stream)
>>> data.read(stream)
>>> stream.close()
>>> data.header.width
60
>>> data.header.height
20
Parse all TGA files in a directory tree¶
>>> for stream, data in TgaFormat.walkData(format_root):
... try:
... # the replace call makes the doctest also pass on windows
... os_path = stream.name
... split = (os_path.split(os.sep))[-4:]
... rejoin = os.path.join(*split).replace("\\", "/")
... print("reading %s" % rejoin)
... except Exception:
... print(
... "Warning: read failed due corrupt file,"
... " corrupt format description, or bug.")
reading tests/formats/tga/test.tga
reading tests/formats/tga/test_footer.tga
Create a TGA file from scratch and write to file¶
>>> data = TgaFormat.Data()
>>> from tempfile import TemporaryFile
>>> stream = TemporaryFile()
>>> data.write(stream)
>>> stream.close()