def run(width, height, name = 'cprime'): filename = name + '-{0}x{1}.png'.format(width, height) max_value = width * height print 'generating primes...' primes = primesByMaxValue(max_value) print 'Initializing image...' image = Png(width, height) for y in range(height): for x in range(width): image.set_pixel(x,y,[0,0,0]) a = [[1,0,-1],[-1,1,0],[0,-1,1]] print 'Drawing primes...' for p in primes: y = p / width x = p % width d = int( (x * x + y * y) ** 0.5 ) i = (d / 256) % 3 r = (a[i][0] * d) % 256 g = (a[i][1] * d) % 256 b = (a[i][2] * d) % 256 image.set_pixel(x,y,[r,g,b]) print 'Writing to disk...' image.write(filename) print 'All Done!'
def run(width, height, name='cprime'): filename = name + '-{0}x{1}.png'.format(width, height) max_value = width * height print 'generating primes...' primes = primesByMaxValue(max_value) print 'Initializing image...' image = Png(width, height) for y in range(height): for x in range(width): image.set_pixel(x, y, [0, 0, 0]) a = [[1, 0, -1], [-1, 1, 0], [0, -1, 1]] print 'Drawing primes...' for p in primes: y = p / width x = p % width d = int((x * x + y * y)**0.5) i = (d / 256) % 3 r = (a[i][0] * d) % 256 g = (a[i][1] * d) % 256 b = (a[i][2] * d) % 256 image.set_pixel(x, y, [r, g, b]) print 'Writing to disk...' image.write(filename) print 'All Done!'
def write(self, filename, forground=[0, 0, 0], background=[255, 255, 255]): self._normalize() width = 1 + (self.maximal.x + 1) + 1 height = 1 + (self.maximal.y + 1) + 1 image = Png(width, height) # print "image is {0}x{1} pixels".format(width, height) for y in range(height): for x in range(width): # print "setting background color of ({0},{1})".format(x,y) image.set_pixel(x, y, background) for p in self.points: image.set_pixel(1 + p.x, 1 + p.y, forground) # print "set forground of point ({0},{1})".format(p.x,p.y), # print "at pixel ({0},{1})".format( 1 + p.x, 1 + p.y) image.write(filename) return self
def write(self, filename, forground = [0,0,0], background = [255,255,255]): self._normalize() width = 1 + (self.maximal.x + 1) + 1 height = 1 + (self.maximal.y + 1) + 1 image = Png(width,height) # print "image is {0}x{1} pixels".format(width, height) for y in range(height): for x in range(width): # print "setting background color of ({0},{1})".format(x,y) image.set_pixel(x,y,background) for p in self.points: image.set_pixel(1 + p.x,1 + p.y,forground) # print "set forground of point ({0},{1})".format(p.x,p.y), # print "at pixel ({0},{1})".format( 1 + p.x, 1 + p.y) image.write(filename) return self
def run(width, height, color = [0,0,255], name = 'prime'): filename = name + '-{0}x{1}.png'.format(width, height) max_value = width * height print 'generating primes...' primes = primesByMaxValue(max_value) print 'Initializing image...' image = Png(width, height) for y in range(height): for x in range(width): image.set_pixel(x,y,[0,0,0]) print 'Drawing primes...' for p in primes: y = p / width x = p % width image.set_pixel(x,y,color) print 'Writing to disk...' image.write(filename) print 'All Done!'
def run(width, height, color=[0, 0, 255], name='prime'): filename = name + '-{0}x{1}.png'.format(width, height) max_value = width * height print 'generating primes...' primes = primesByMaxValue(max_value) print 'Initializing image...' image = Png(width, height) for y in range(height): for x in range(width): image.set_pixel(x, y, [0, 0, 0]) print 'Drawing primes...' for p in primes: y = p / width x = p % width image.set_pixel(x, y, color) print 'Writing to disk...' image.write(filename) print 'All Done!'
def run(width, height, spiral_size = None, name = 'ulam'): if spiral_size == None: spiral_size = min(width, height) filename = name + '-{0}x{1}.png'.format(width, height) max_value = 1 + spiral_size ** 2 print 'Generating Primes...' primes = primesByMaxValue(max_value) print 'Initializing Image...' image = Png(width, height) x = 0 y = 0 while y < height: while x < width: image.set_pixel(x,y, _background) x += 1 x = 0 y += 1 y = 0 print 'Drawing Primes...' if width % 2 == 0: x = width / 2 - 1 else: x = width / 2 y = height / 2 i = 0 p = primes[0] p_index = 0 p_final = primes[-1] dx,dy = 1,0 sc,sm,tc = 0,1,0 while i < max_value: i += 1 if i == p: image.set_pixel(x,y, _prime) if p != p_final: p_index += 1 p = primes[p_index] else: image.set_pixel(x,y,_composite) x += dx y += dy sc += 1 if sc == sm: # rotate direction t = dx dx = dy dy = -t # reset step count, increment turn count sc = 0 tc += 1 # increment length of next leg if we've turned twice if tc == 2: tc = 0 sm += 1 print 'Writing to Disk...' image.write(filename) print 'All Done!'
class Mandelbrot: # Members: # Complex position # Scalar width # Scalar height # int xPix # int yPix # boolean setup # Png image # boolean rendered def __init__(self): self.position = origin.clone() self.width = Scalar(4, 0) self.height = Scalar(4, 0) self.xPix = 1 self.yPix = 1 self.setup = False self.image = Png() self.rendered = False def setPosition(self, position): self.position.copy(position) def setWidth(self, width): self.width.copy(width) def setHeight(self, height): self.height.copy(height) def setWindowSize(self, width, height): self.width.copy(width) self.height.copy(height) def setPixels(self, xPix, yPix): self.xPix = xPix self.yPix = yPix self.setup = True def render(self, depth=32): if (not self.setup): print "You need to set up your fractal before you can render it!" return True if (self.rendered): print "You've already rendered this!" return True else: self.image.set_dimensions(self.xPix, self.yPix) x0 = self.width.clone() x0.halve() x0.scale(-1) x0.addBy(self.position.r) y0 = self.height.clone() y0.halve() y0.addBy(self.position.i) xStep = _right.clone() xStep.scale(self.width) xStep.divide(self.xPix) yStep = _down.clone() yStep.scale(self.height) yStep.divide(self.yPix) zed = Zed() p = Complex(x0, y0) y = 0 progress = self.yPix / 20 if (not progress): progress = 1 while (y < self.yPix): if (y % progress == 0): print "{}% done".format(100 * y / self.yPix) p.r.copy(x0) x = 0 while (x < self.xPix): zed.refresh(p) while (not zed.escaped and zed.generation <= depth): zed.iterate() self.image.set_pixel(x, y, colorize(zed, depth)) p.addBy(xStep) x += 1 p.addBy(yStep) y += 1 self.rendered = True return False def write(self, filename=None, interlace=True): if (not self.rendered): print "You should render before writing to file!" return True if (not filename): dims = "{0}x{1}x{2}".format(self.xPix, self.yPix, depth) filename = "mandelbrot-" + dims + ".png" self.image.write(filename, interlace) return False
from pypng import Png def dist(x,y): return (x ** 2.0 + y ** 2.0) ** 0.5 i = Png(256, 256) for x in range(256): for y in range(256): d = dist(x - 128, y - 128) if d < 64: i.set_pixel(x,y,[255,0,0]) else: i.set_pixel(x,y,[0,0,0]) if i.set_simple_alpha([0,0,0]): print 'error in set_simple_alpha method of Png class' i.write('simple_alpha.png')
class Mandelbrot: # Members: # Complex position # Scalar width # Scalar height # int xPix # int yPix # boolean setup # Png image # boolean rendered def __init__(self): self.position = origin.clone() self.width = Scalar(4, 0) self.height = Scalar(4, 0) self.xPix = 1 self.yPix = 1 self.setup = False self.image = Png() self.rendered = False def setPosition(self, position): self.position.copy(position) def setWidth(self, width): self.width.copy(width) def setHeight(self, height): self.height.copy(height) def setWindowSize(self, width, height): self.width.copy(width) self.height.copy(height) def setPixels(self, xPix, yPix): self.xPix = xPix self.yPix = yPix self.setup = True def render(self, depth = 32): if (not self.setup): print "You need to set up your fractal before you can render it!" return True if (self.rendered): print "You've already rendered this!" return True else: self.image.set_dimensions(self.xPix, self.yPix) x0 = self.width.clone() x0.halve() x0.scale(-1) x0.addBy(self.position.r) y0 = self.height.clone() y0.halve() y0.addBy(self.position.i) xStep = _right.clone() xStep.scale(self.width) xStep.divide(self.xPix) yStep = _down.clone() yStep.scale(self.height) yStep.divide(self.yPix) zed = Zed() p = Complex(x0,y0) y = 0 progress = self.yPix / 20 if (not progress): progress = 1 while (y < self.yPix): if (y % progress == 0): print "{}% done".format(100 * y / self.yPix) p.r.copy(x0) x = 0 while (x < self.xPix): zed.refresh(p) while (not zed.escaped and zed.generation <= depth): zed.iterate() self.image.set_pixel( x,y, colorize(zed, depth) ) p.addBy(xStep) x += 1 p.addBy(yStep) y += 1 self.rendered = True return False def write(self, filename = None, interlace = True): if (not self.rendered): print "You should render before writing to file!" return True if (not filename): dims = "{0}x{1}x{2}".format(self.xPix, self.yPix, depth) filename = "mandelbrot-" + dims + ".png" self.image.write(filename, interlace) return False
from pypng import Png def dist(x, y): return (x**2.0 + y**2.0)**0.5 i = Png(256, 256) for x in range(256): for y in range(256): d = dist(x - 128, y - 128) if d < 64: i.set_pixel(x, y, [255, 0, 0]) else: i.set_pixel(x, y, [0, 0, 0]) if i.set_simple_alpha([0, 0, 0]): print 'error in set_simple_alpha method of Png class' i.write('simple_alpha.png')