def display_func(t, x): if np.size(x) > 14 * 90: input_shape = (1, 28, 90) else: input_shape = (1, 14, 90) values = x.reshape(input_shape) values = values.transpose((1, 2, 0)) values = (values + 1) / 2 * 255. values = values.astype('uint8') if values.shape[-1] == 1: values = values[:, :, 0] png = PIL.Image.fromarray(values) buffer = cStringIO.StringIO() png.save(buffer, format="PNG") img_str = base64.b64encode(buffer.getvalue()) display_func._nengo_html_ = ''' <svg width="100%%" height="100%%" viewbox="0 0 %i %i"> <image width="100%%" height="100%%" xlink:href="data:image/png;base64,%s" style="image-rendering: auto;"> </svg>''' % (input_shape[2] * 2, input_shape[1] * 2, ''.join(img_str))
def display_func(t, x): if np.size(x) > 14*90: input_shape = (1, 28, 90) else: input_shape = (1,14,90) values = x.reshape(input_shape) values = values.transpose((1, 2, 0)) values = (values + 1) / 2 * 255. values = values.astype('uint8') if values.shape[-1] == 1: values = values[:, :, 0] png = PIL.Image.fromarray(values) buffer = cStringIO.StringIO() png.save(buffer, format="PNG") img_str = base64.b64encode(buffer.getvalue()) display_func._nengo_html_ = ''' <svg width="100%%" height="100%%" viewbox="0 0 %i %i"> <image width="100%%" height="100%%" xlink:href="data:image/png;base64,%s" style="image-rendering: auto;"> </svg>''' % (input_shape[2]*2, input_shape[1]*2, ''.join(img_str))
picture[index+2] = (i*25)%256 picture[index+3] = 255 width = 4096 height = 4096 picture = bytearray(width*height*4) zoom = 1 xs = -2.1 ys = -1.3 s = Stopwatch.Stopwatch() dx = (2.6/zoom) / width dy = (2.6/zoom) / height i = 0 for y in range(height): for x in range(width): c = complex(xs+x*dx, ys+y*dy) colorIndex = mandelbrot(c) indexToColor(picture, i*4, colorIndex) i += 1 print("Elapsed ms:", s.elapsedTime()*1000) png.save("fractal.png", picture, width, height)
maxDepth = 10 world = randomSphereScene() lookFrom = Vec3(13,2,3) lookAt = Vec3(0,0,0) vUp = Vec3(0,1,0) distToFocus = 10.0 aperture = 0.1 cam = Camera(lookFrom, lookAt, vUp, 20, aspectRatio, aperture, distToFocus) stopwatch = Stopwatch() image = imageHeight*imageWidth*[[0,0,0]] index = 0 for j in range(imageHeight): showProgress(stopwatch, j, imageHeight) for i in range(imageWidth): pixelColor = Vec3(0,0,0) for s in range(samplesPerPixel): u = (i + random.random()) / (imageWidth-1) v = (j + random.random()) / (imageHeight-1) r = cam.getRay(u, v) pixelColor = pixelColor + rayColor(r, world, maxDepth) image[index] = vecToColor(pixelColor, samplesPerPixel) index += 1 print("\nTotal time: {0:2.2f} sec.".format(stopwatch.elapsedTime())) print("Saving Image ...") png.save("result.png", image, imageWidth, imageHeight) print("Done.")
def extract_png(image): # TODO: Maybe just use APNG: https://pypi.org/project/apng/ img = APNG.open(image.veritas.file_name) if len(img.frames) == 1: logger.debug('Only one frame detected.') return print("Extracting {} PNG frames.".format(len(img.frames))) for i, (png, control) in enumerate(img.frames): outfile = os.path.join( image.veritas.results_directory, '{}_frame_{}.png'.format(os.path.basename(image.veritas.file_name), i)) png.save(outfile) return ### Below is some original work before finding that library # PIL apparently can't handle this. Neither can ffmpeg. ImageMagick can be fooled. # Time to write my own animated png extractor img = png.Reader(image.veritas.file_name) chunks = list(img.chunks()) num_frames = len(list(x for x in chunks if x[0] in [b'fcTL'])) """ actl = next(x for x in chunks if x[0] == b'acTL') # How many frames does the image claim to have given_num_frames, _ = unpack('>II', actl[1]) if given_num_frames != num_frames: print('Image only claims {} frames, but I discovered {}'.format(given_num_frames, num_frames)) print('Patching and attempting to extract the frames') new_actl = (b'acTL', pack('>II',num_frames,0)) new_chunks = [] frame_number = 0 for t,v in chunks: if t == b'acTL': new_chunks.append(new_actl) elif t == b'fcTL': new_chunks.append((t, pack('>I', frame_number) + v[4:])) frame_number += 1 else: new_chunks.append((t,v)) with open('blerg.png','wb') as f: png.write_chunks(f, new_chunks) return """ ####### if num_frames == 1: logger.debug('Only one frame discovered.') return print('Discovered multuple PNG frames. Attempting to extract them...') header_chunk = next(x for x in chunks if x[0] in [b'IHDR']) end_chunk = (b'IEND', b'') actl_chunk = (b'acTL', b'\x00\x00\x00\x01\x00\x00\x00\x00' ) # looping, 1 frame new_image = [] num_frames = 0 # Type and value for the chunks for t, val in chunks: print(t) # We're done with this image if t in [b'fcTL', b'IEND']: # We have something to save if new_image != []: # Put propper chunks in place new_image.insert(0, copy(actl_chunk)) new_header_chunk = list(copy(header_chunk)) #new_header_chunk[1] = pack('>II', width, height) + new_header_chunk[1][8:] #new_header_chunk[1] = width_height + new_header_chunk[1][8:] new_header_chunk = tuple(new_header_chunk) new_image.insert(0, new_header_chunk) #new_image.insert(0, copy(header_chunk)) new_image.append(copy(end_chunk)) print(new_image) outfile = os.path.join( image.veritas.results_directory, '{}_frame_{}.png'.format( os.path.basename(image.veritas.file_name), num_frames)) print(outfile) with open(outfile, 'wb') as f: png.write_chunks(f, new_image) #new_image = [(t, val)] new_image = [] num_frames += 1 if t == b'fcTL': # Update our frame size #width, height = unpack('>II', val[4:12]) #print('width and height: ' + str(width) + ' ' + str(height)) #width_height = val[4:12] #new_image.append((t, b'\x00\x00\x00\x00' + val[4:])) new_image.append((t, val)) # Nothing to save, just add our header in #else: # new_image.append((t, val)) elif t == b'IDAT': new_image.append((t, val)) elif t == b'fdAT': # fdAT is simply IDAT with 4 bytes at the beginning new_image.append((b'IDAT', val[4:])) """
" using " + str(processCount) + " processes") try: arr = Array('B', height * width) processes = [] for i in range(processCount): p = Process(target=computePortion, args=(i, width, height, processCount, arr)) p.start() processes.append(p) for p in processes: p.join() print("Buffer to Color Conversion") for i in range(height * width): writePixel(buf, i, arr[i]) except Exception as e: print("Error: Process Error: " + e) quit() end = time.perf_counter() print("\nDone after " + str(int(end - start)) + " seconds. Now Saving " + filename) try: png.save(filename, buf, width, height) except: print("Error saving " + filename)
image = width * height * [None] maxDepth = 10 sampleCount = 1 lookFrom = vec3(13, 2, 3) lookAt = vec3(0, 0, 0) vUp = vec3(0, 1, 0) distToFocus = 10 aperture = 0 # 0.1 fovy = 20 myCamera = camera(lookFrom, lookAt, vUp, fovy, aspectRatio, aperture, distToFocus) scene = generateScene() i = 0 for y in range(height): print("Berechne Zeile", y) for x in range(width): c = vec3(0, 0, 0) for s in range(sampleCount): u = (x + random.random()) / (width - 1) v = (y + random.random()) / (height - 1) primary = myCamera.getRay(u, v) c += rayColor(primary, scene, maxDepth) / sampleCount image[i] = c i += 1 png.save("test.ppm", image, width, height)