User:Lidia.Pereira/PNM/Raw: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<syntaxhighlight lang="python"> import struct, array import random width = 320 height = 240 filename="output2.tga" datafile = open(filename, "wb") # TGA format: http://gpw...")
 
No edit summary
Line 68: Line 68:
datafile.close()
datafile.close()
</syntaxhighlight>
</syntaxhighlight>
[[File:OutputRAW]]
[[File:output2.png]]

Revision as of 09:54, 14 October 2013

import struct, array
import random
 
width = 320
height = 240
 
filename="output2.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 = ''
attempt = height/2

for y in xrange(height):
    for x in xrange(width/2):
        r, g, b = 0, 0, 0
        if y <= attempt:
            r = 255
        if y > 15 and y < 30:
            b = 144
        if y > attempt and y < 177:
            r = 155
        if y > 177:
            r = 44
        data += struct.pack('B', b)
        data += struct.pack('B', g)
        data += struct.pack('B', r)
    for x in xrange(width/4):
        r, g, b = 0, 0, 0
        if y <= attempt:
            g = 155
        if y > 70 and y < 90:
            r = 144
        if y > attempt/2 and y < 91:
            b = 155
        if x > width/4 and x < width - width/4:
            b = 255
        else:
            b = 77
        data += struct.pack('B', b)
        data += struct.pack('B', g)
        data += struct.pack('B', r)
    for x in xrange(width/4):
        r, g, b = 0, 0, 0
        if y <= attempt:
            b = 155
        if y > 99 and y < 191:
            g = 144
        if y > attempt and y < 190:
            r = 155
        if x > width - width/4:
            b = 255
        else:
            b = 55
        data += struct.pack('B', b)
        data += struct.pack('B', g)
        data += struct.pack('B', r)



datafile.write(data)
datafile.close()

Output2.png