Code wäre nett

Doch das habe ich:BlackJack hat geschrieben:Dann hast Du Dir nicht angeschaut, wie die NetPBM Formate aussehen. Was kann man denn an der Struktur noch einfacher machen? Es wird das Format, Höhe und Breite, eventuell noch die Anzahl der Graustufen als Text in eine Datei geschrieben und dann folgen die Bilddaten, entweder als Höhe*Breite Bytes/Words bei den "Raw" Formaten oder als Zahlen als Text durch Whitespace getrennt.
Code: Alles auswählen
P3
# feep.ppm
4 4
15
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0
Code: Alles auswählen
import struct
class PGM:
def __init__(self, width, height, maxval=None, comment='producer: pnm.py'):
if width <= 0 or height <= 0:
raise ValueError("Dimensions must be > 0")
self.width = width
self.height = height
self.maxval = maxval
self.comment = comment
#
# Create empty image.
#
self.data = list()
for dummy in xrange(self.height):
self.data.append([0] * self.width)
def __getitem__(self, coordinate_or_row):
try:
x, y = coordinate_or_row
return self.data[y][x]
except TypeError:
return self.data[coordinate_or_row]
def __setitem__(self, coordinate_or_row, pixel_or_row):
try:
x, y = coordinate_or_row
self.data[y][x] = pixel_or_row
except TypeError:
self.data[coordinate_or_row] = pixel_or_row
def write(self, fileobj):
#
# Write header.
#
fileobj.write('P5\n')
if self.comment:
fileobj.write('# %s\n' % self.comment)
fileobj.write('%s %s\n' % (self.width, self.height))
if self.maxval is None:
self.maxval = max(map(max, self.data))
fileobj.write('%s\n' % self.maxval)
#
# Write image data.
#
row_format = '>%d%s' % (self.width, 'BH'[int(self.maxval > 255)])
for row in self.data:
fileobj.write(struct.pack(row_format, *row))
def save(self, filename):
image_file = open(filename, 'wb')
self.write(image_file)
image_file.close()
Code: Alles auswählen
from __future__ import division
import math
image = PGM(320, 240)
for y in xrange(image.height):
for x in xrange(image.width):
image[x, y] = int(abs((math.sin(x / 23)
+ math.sin(y / 42)
+ math.sin(x / 6.4)
+ math.sin(y / 3.2))) * (255 / (2*math.pi)))
image.save('sin.pgm')
image = PGM(320, 240)
for row_nr in xrange(image.height):
image[row_nr] = [row_nr] * image.width
image.save('gradient_h.pgm')
Code: Alles auswählen
try:
import pygame
import Numeric as N
from pygame.locals import *
surfarray = pygame.surfarray
if not surfarray: raise ImportError
except:
raise ImportError, 'Error Importing Pygame/surfarray or Numeric'
pygame.init()
def show(img):
screen = pygame.display.set_mode(img.shape[:2], 0, 32)
surfarray.blit_array(screen, img)
pygame.display.flip()
pygame.display.set_caption("Simple GFX Output")
while 1:
e = pygame.event.wait()
if e.type == QUIT: raise SystemExit
img = N.zeros((320, 240, 3))
for x in range(img.shape[0]):
for y in range(img.shape[1]):
img[x,y,:] = [100,155,255]
show(img)