Raw image: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
Outputting a 10 x 10 image in RGBA (red-green-blue-alpha, 4 bytes per pixel): | |||
<source lang="python"> | |||
#!/usr/bin/env python | |||
#-*- coding:utf-8 -*- | |||
import struct | |||
f = open("blah.data", 'w') | |||
x=255 | |||
for _ in range(10): | |||
for _ in range(10): | |||
f.write(struct.pack('B', 0) ) | |||
f.write(struct.pack('B', 0) ) | |||
f.write(struct.pack('B', 255) ) | |||
f.write(struct.pack('B', x) ) | |||
x = x - 20 | |||
f.close() | |||
</source> | |||
<source lang="python"> | <source lang="python"> | ||
import struct, array | import struct, array | ||
Line 38: | Line 61: | ||
[[File:Rawimage.png]] | [[File:Rawimage.png]] | ||
== Resources == | == Resources == | ||
* This example is based on this [http://www.python.org.br/wiki/ImagemTGA Raw image example] (in Portugese) | * This example is based on this [http://www.python.org.br/wiki/ImagemTGA Raw image example] (in Portugese) |
Revision as of 12:17, 14 October 2013
Outputting a 10 x 10 image in RGBA (red-green-blue-alpha, 4 bytes per pixel):
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import struct
f = open("blah.data", 'w')
x=255
for _ in range(10):
for _ in range(10):
f.write(struct.pack('B', 0) )
f.write(struct.pack('B', 0) )
f.write(struct.pack('B', 255) )
f.write(struct.pack('B', x) )
x = x - 20
f.close()
import struct, array
width = 320
height = 240
filename="output.tga"
datafile = open(filename, "wb")
# TGA format: http://gpwiki.org/index.php/TGA
# Offset, ColorType, ImageType, PaletteStart, PaletteLen, PalBits, XOrigin, YOrigin, Width, Height, BPP, Orientation
header = struct.pack("<BBBHHBHHHHBB", 0, 0, 2, 0, 0, 8, 0, 0, width, height, 24, 1 << 5)
datafile.write(header)
data = ''
for y in xrange(height):
for x in xrange(width):
r, g, b = 0, 0, 0
if y < 32:
r = 255
if x > 64 and x < 256:
g = 255
if y > 120:
r = 128
data += struct.pack('B', b)
data += struct.pack('B', g)
data += struct.pack('B', r)
datafile.write(data)
datafile.close()
This script outputs a TGA format, which has been opened in the GIMP and exported to PNG
Resources
- This example is based on this Raw image example (in Portugese)