Algorithmic Media

From XPUB & Lens-Based wiki
Revision as of 15:38, 23 April 2009 by Michael Murtaugh (talk | contribs) (New page: == Image Generation == <source lang="python"> import os, struct import math f = open("test.rgba", "wb") def do (cmd): os.system(cmd) (red, green, blue) = (255, 0, 0) for x in range(4...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Image Generation

import os, struct
import math

f = open("test.rgba", "wb")

def do (cmd):
	os.system(cmd)

(red, green, blue) = (255, 0, 0)

for x in range(480):
	for y in range(480):
		# alpha = int(255 * (y / 480.0))
		dx = (240 - x)
		dy = (240 - y)
		d = math.sqrt(dy**2 + dx**2)

		alpha = min(255, max(0 , int(255 * (d / 240.0)) ))
		data = struct.pack("<BBBB", red, green, blue, alpha)
		f.write(data)

f.close()

do("convert -size 480x480 -depth 8 rgba:test.rgba test.png")
import os, struct

i = open("papert.raw", "rb")

f = open("test.rgb", "wb")

for y in range(480):
	for x in range(480):
		
		pixel = i.read(3)
		(r, g, b) = struct.unpack("<BBB", pixel)


		if y % 2 == 0:
			red = x % 255
			green = y % 255
			blue = 0
			data = struct.pack("<BBB", red, green, blue)
		else:
			data = struct.pack("<BBB", r, g, b)

		f.write(data)