alpha = 0 else: alpha = 255 palette.append((entry.red, entry.green, entry.blue, alpha)) except IndexError: # pad with zeroes palette.append((0, 0, 0, 0)) rows = [] img = [] for i in range(1, (int(options.width)) + 1): rows.append(color_index) for i in range(0, int(options.height)): img.append(rows) w = png.Writer(int(options.width), int(options.height), palette=palette, bitdepth=8) w.write(f, img) else: # use RGBA rows = [] img = [] for i in range(1, (int(options.width) * 4) + 1): if i % 4 == 1: rows.append(colormap_entry.red) elif i % 4 == 2: rows.append(colormap_entry.green) elif i % 4 == 3: rows.append(colormap_entry.blue) elif i % 4 == 0:
for row in data[:, :, :, i]: data_set = () for pixel in row: #total=0 for value in pixel: data_set += (value, ) #total+=value input_pop_views[cnt].set('rate', input_rate / (256) * value) #print input_weights[cnt] cnt += 1 png_data.append(data_set) # write out a png for debugging with open(basepath + '/generated/' + str(i) + '.png', 'wb') as f: w = png.Writer(32, 32) w.write(f, png_data) print "[%d] Running simulation step %d" % (node, stop) pynn.run(tstop) # save final weights with open("%s_final_weights.wgt" % (file_stem, ), 'wb') as f: pickle.dump([ input_projection.getWeights(format='array'), l1_projection.getWeights(format='array') ], f) print "[%d] Writing spikes to disk" % node l0_exc_population.printSpikes('%s_exc_0.ras' % (file_stem, )) l1_exc_population.printSpikes('%s_exc_1.ras' % (file_stem, ))
def export_png_grid(table, width, height, title='output', pixel_size=16, bitdepth=8, alpha=True): print('Started rendering image for table ', title) #Takes a table from the database, and exports it into a png #width and height are that of the model itself #pixel_size is how many pixels represent one side of a patch (i.e., one small grid box) #The tables are obtained from meta.tables (make use of keys() of this to get what you want) initial_time = datetime.now() #Getting all patched in the grid s = table.select() all_patches = conn.execute(s) #If the alpha channel is used, 4 channels will be used, otherwise 3 channels = 4 if alpha else 3 #this_grid is an array that shall be used for holding the color info #By default, it will hold rgb(0,0,0) values, that is, all pixels are black #The png file is stored as a list of lists #The inner lists represent rows in the image. They contain the channels of each pixel #For example, [[0,0,0, 255,102,16],[123,21,23, 12,69,147]] has 2 pixels only in the rgb mode this_grid = [] for i in range(height): for j in range(pixel_size): row = [0] * width * channels * pixel_size this_grid.append(row) #Looping through the patches and changing the colors appropriately #The table here has 4 columns - id, x_coordinate, y_coordinate, energy (all are integers) for patch in all_patches: #Root condition for changing defaults (default values are rgb(0,0,0), i.e. black, everywhere) energy = patch[3] if energy > 5: #Nested for loops enables scaling a patch to multiple pixels as per 'pixel_size' for i in range(pixel_size): for j in range(pixel_size): #If the right parameters were not entered, out of bounds exception may occur try: #x_pos and y_pos are the indices in this_grid that represents a patch #The following code makes sure scaling to 'pixel_size' is made possible #Also, depending on presence of an alpha channel, the length can vary x_pos = pixel_size * channels * patch[1] y_pos = pixel_size * patch[2] color = patch_color(energy) this_grid[i + y_pos][j * channels + x_pos + 0] = color[0] this_grid[i + y_pos][j * channels + x_pos + 1] = color[1] this_grid[i + y_pos][j * channels + x_pos + 2] = color[2] if alpha: this_grid[i + y_pos][j * channels + x_pos + 3] = color[3] except Exception as e: print(e, patch) w = png.Writer( width=pixel_size * width, height=pixel_size * height, greyscale=False, alpha=alpha, bitdepth=bitdepth, ) f = open(os.path.join(create_output_dir(), title + '.png'), 'wb') w.write(f, this_grid) f.close() final_time = datetime.now() print('Time taken to render ', title, (final_time - initial_time), ' for total pixels ', height * width * (pixel_size**2))
def _save_png(data: np.ndarray, dest: BinaryIO, colors=None): w, h = data.shape writer = png.Writer(w, h, bitdepth=8, alpha=False, palette=colors) rows = (data[:, i].tobytes() for i in range(data.shape[1])) writer.write(dest, rows)
def WritePngFile(self, path): with open(path, "wb") as f: png.Writer(**self.metadata).write_array(f, self.pixels)
def extract(args, action, extractpath, a): codec = None foundhh = None ob = None extracttype = dict(extractboth=-1, extractcolor=oni.NODE_TYPE_IMAGE, extractdepth=oni.NODE_TYPE_DEPTH, extractir=oni.NODE_TYPE_IR)[action] targetnid = None seekframe = args.fseek endframe = args.fduration < 0 and -1 or seekframe + args.fduration # scan all and keep pre and last r = oni.Reader(a) while True: h = r.next() if h is None: break if h["rt"] == oni.RECORD_NODE_ADDED: hh = oni.parseadded(a, h) if hh["nodetype"] == extracttype: targetnid = h["nid"] codec = hh["codec"] foundhh = hh print "!!found matching for ", targetnid, codec if codec == "16zt": if xndec is None: print "xndec is missing cannot decode to png" sys.exit(-1) if xndec.doXnStreamUncompressDepth16ZWithEmbTable is None: print "xndec is doXnStreamUncompressDepth16ZWithEmbTable" sys.exit(-1) if png is None: print "pypng is missing" sys.exit(-1) elif h["nid"] == targetnid: if h["rt"] == oni.RECORD_NEW_DATA: pd = oni.parsedatahead(a, h) if pd["frameid"] >= seekframe and (endframe == -1 or pd["frameid"] < endframe): print "newdata", pd["frameid"], h["ps"], h["fs"], codec q = r.streams[h["nid"]] xres, yres = q["size"] if codec == "jpeg": ext = "jpeg" elif codec == "16zt": ext = "png" if ob is None: ob = xndec.allocoutput16(xres * yres) else: ext = "bin" outfile = "%s%d.%s" % (extractpath, pd["frameid"], ext) print outfile, codec of = open(outfile, "wb") if codec == "jpeg": of.write(a.read(h["ps"])) elif codec == "16zt": code, size = xndec.doXnStreamUncompressDepth16ZWithEmbTable( a.read(h["ps"]), ob) aa = array.array("H") aa.fromstring(ob) rows = [ aa[i * xres:(i + 1) * xres] for i in range(0, yres) ] if args.coloreddepth: # save content of ob to PNG 16bit with size xres,yres w = png.Writer(width=xres, height=yres, greyscale=True, bitdepth=8) w.write(of, [[int(x * 255.0 / 8000.0) for x in y] for y in rows]) else: # save content of ob to PNG 16bit with size xres,yres w = png.Writer(width=xres, height=yres, greyscale=True, bitdepth=16) w.write(of, rows) else: print "unsupported codec", foundhh sys.exit(0) of.close() elif endframe != -1 and pd["frameid"] >= endframe: break
scene_suzanne = bounding_box_hierarchy( suzanne + [CheckeredSphere(0, -2 - MAX_RAY_LENGTH, 0, MAX_RAY_LENGTH, WHITE, 0.5)] + balls) # scene_snowman = bounding_box_hierarchy(scene_snowman) # scene_test = [CheckeredSphere(0, -2 - MAX_RAY_LENGTH, 0, MAX_RAY_LENGTH, WHITE, 0.5)] pixelmap = [[(255, 0, 255) if (x // 8 + y // 8) % 2 else (0, 0, 0) for x in range(WIDTH)] for y in range(HEIGHT)] for y in tqdm(range(0, HEIGHT, 1), desc="Rendering", ascii=True): thread_list = [] for x in range(0, WIDTH, 1): t = threading.Thread(target=raytrace_screen, args=( y, x, pixelmap, scene_suzanne, lights_sky)) thread_list.append(t) for thread in thread_list: thread.start() for thread in thread_list: thread.join() pngmap = [[subpixel for x in range(WIDTH) for subpixel in pixelmap[y][x]] for y in range(HEIGHT)] output_file = open(png_file_name, 'wb') w = png.Writer(WIDTH, HEIGHT, alpha=False) w.write(output_file, pngmap) output_file.close()
def main(argv): """Run the PNG encoder with options from the command line.""" (options, infilename, infile, outfile) = parse_options(argv[1:]) if options.read_png: # Encode PNG to PPM pngObj = png.Reader(file=infile) width, height, pixels, meta = pngObj.asDirect() write_pnm(outfile, width, height, pixels, meta) else: # Encode PNM to PNG mode, width, height, depth, maxval = \ read_pnm_header(infile, ('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7')) # When it comes to the variety of input formats, we do something # rather rude. Observe that L, LA, RGB, RGBA are the 4 colour # types supported by PNG and that they correspond to 1, 2, 3, 4 # channels respectively. So we use the number of channels in # the source image to determine which one we have. We do not # care about TUPLTYPE. greyscale = depth <= 2 pamalpha = depth in (2, 4) supported = [2**x - 1 for x in range(1, 17)] try: bitdepth = supported.index(maxval) + 1 except ValueError: raise NotImplementedError( 'your maxval (%s) not in supported list %s' % (maxval, str(supported))) writer = png.Writer(width, height, greyscale=greyscale, bitdepth=bitdepth, interlace=options.interlace, transparent=options.transparent, background=options.background, alpha=bool(pamalpha or options.alpha), gamma=options.gamma, compression=options.compression) if mode == png.strtobytes('P4'): rows = pbmb_scanlines(infile, width, height) elif mode in (png.strtobytes('P1'), png.strtobytes('P2'), png.strtobytes('P3')): rows = ascii_scanlines(infile, width, height, depth, bitdepth) else: rows = file_scanlines(infile, width, height, depth, bitdepth) if options.alpha: apgmfile = open(options.alpha, 'rb') _, awidth, aheight, adepth, amaxval = \ read_pnm_header(apgmfile, ('P5', )) if amaxval != maxval: raise NotImplementedError( 'maxval %s of alpha channel mismatch %s maxval %s' % (amaxval, infilename, maxval)) if adepth != 1: raise ValueError("alpha image should have 1 channel") if (awidth, aheight) != (width, height): raise ValueError("alpha channel image size mismatch" " (%s has %sx%s but %s has %sx%s)" % (infilename, width, height, options.alpha, awidth, aheight)) arows = file_scanlines(apgmfile, width, height, 1, bitdepth) merged = png.MergedPlanes(rows, depth, arows, 1, bitdepth) writer.write(outfile, merged) apgmfile.close() else: writer.write(outfile, rows) if infilename != '-': # if open - then close infile.close()
# print(f"Emit RLE of {rep}") c = fb[ptr] ptr = ptr + 1 for _ in range(rep): o.append(c) if len(o) != w * h: raise Exception(f"{f}: output {len(o)}, expected {w*h}") oa = np.array(o, dtype=np.uint8) oa = np.reshape(oa, (h, w)) oa = np.flipud(oa) return oa, palette if __name__ == "__main__": import png f = sys.argv[1] try: oa, palette = read_ibg(f) h, w = oa.shape print(f"{f}: format {w} x {h}") with open(f.replace(".ibg", ".png"), "wb") as g: pw = png.Writer(width=w, height=h, palette=palette[:, 0:3]) pw.write(g, oa) except Exception as e: print(f"{f}: failed: {e}")
def get_writer(width, height): return png.Writer(width, height, greyscale=False, alpha=True)
def saveUint16(z, path): # Use pypng to write zgray as a grayscale PNG. with open(path, 'wb') as f: writer = png.Writer(width=z.shape[1], height=z.shape[0], bitdepth=16, greyscale=True) zgray2list = z.tolist() writer.write(f, zgray2list)
def extract(self): if self.verbose: print('Extracting...') basename_ = os.path.splitext(os.path.basename(self.filename))[0] glyph_widths = {} for cwdh in self.cwdh_sections: for index in range(cwdh['start'], cwdh['end'] + 1): glyph_widths[index] = cwdh['data'][index - cwdh['start']] glyph_mapping = {} for cmap in self.cmap_sections: if cmap['type'] == MAPPING_DIRECT: for code in range(cmap['start'], cmap['end']): try: #Python2 glyph_mapping[unichr(code)] = code - cmap['start'] + cmap['indexOffset'] except: glyph_mapping[chr(code)] = code - cmap['start'] + cmap['indexOffset'] elif cmap['type'] == MAPPING_TABLE: for code in range(cmap['start'], cmap['end']): index = cmap['indexTable'][code - cmap['start']] if index != 0xFFFF: try: #Python2 glyph_mapping[unichr(code)] = index except: glyph_mapping[chr(code)] = index elif cmap['type'] == MAPPING_SCAN: for code in cmap['entries'].keys(): glyph_mapping[code] = cmap['entries'][code] # save JSON manifest json_file_ = open('%s_manifest.json' % basename_, 'w') json_file_.write(json.dumps({ 'version': self.version, 'fileType': self.filetype, 'fontInfo': self.font_info, 'textureInfo': { 'glyph': self.tglp['glyph'], 'sheetCount': self.tglp['sheetCount'], 'sheetInfo': { 'cols': self.tglp['sheet']['cols'], 'rows': self.tglp['sheet']['rows'], 'width': self.tglp['sheet']['width'], 'height': self.tglp['sheet']['height'], 'colorFormat': PIXEL_FORMATS[self.tglp['sheet']['format']] } }, 'glyphWidths': glyph_widths, 'glyphMap': glyph_mapping }, indent=2, sort_keys=True)) json_file_.close() # save sheet bitmaps for i in range(self.tglp['sheetCount']): sheet = self.tglp['sheets'][i] width = sheet['width'] height = sheet['height'] png_data = [] for y in range(height): row = [] for x in range(width): for color in sheet['data'][x + (y * width)]: row.append(color) png_data.append(row) file_ = open('%s_sheet%d.png' % (basename_, i), 'wb') writer = png.Writer(width, height, alpha=True) writer.write(file_, png_data) file_.close() print('Done')
import png, array, sys # this script adds a red dot waypoint grid to the existing map image # for a better optimized waypoint distribution see script block_img.py def my_range(start, end, step): while start <= end: yield start start += step f = open('node_map.png', 'wb') # binary mode is important w = png.Writer(551, 384) reader = png.Reader(filename='phibian_map_1.png') l = reader.read() #width, height, pixels, metadata = reader.read() #print width,height,pixels,metadata pixels = list(l[2]) #print pixels[0] pix_list = [[0 for col in range(2200)] for row in range(384)] for y in range(0, 383): p_row = [] for x in my_range(0, 2200, 4): if pixels[y][x] == 57 and pixels[y][x + 1] == 57 and pixels[y][x + 2] == 54: p_row.append(0)
l = list(r.read()[2]) image2d = np.vstack(itertools.imap(np.uint16, l)) image3d = np.reshape(image2d, (651, 651, 3)) filter_edge = np.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]]) modified = [] for i in range(3): modified.append(sig.convolve2d(image3d[:, :, i], filter_edge, mode='valid')) output = np.abs(np.reshape(np.dstack(modified), (649, (649) * 3))) f = open('file.png', 'wb') w = png.Writer(649, 649) w.write(f, output) f.close() r = png.Reader('ascii-dos.png') l = list(r.read()[2]) asc = np.vstack(itertools.imap(np.uint16, l)) # make a training vector to hold the ascii values train_data = np.zeros((12 * 8, 16 * 16)) for row in range(np.shape(asc)[0]): for column in range(np.shape(asc)[1]): train_data[(row % 12) * 8 + column % 8, ((row / 12) * 16) + (column / 8) / 2] = asc[row, column] """ cutouts = np.zeros((96, 4374)) im = image3d[3:, 3:, :]
def savePNG(self, data, palette): print "Saving file" file = open(self.out_filename, 'wb') writer = png.Writer(data.shape[1], data.shape[0],palette=palette) writer.write(file, data) file.close()
train_img = array('B', train_imgs.read()) # 无符号字节array数组类型 train_lab = array('b', train_labs.read()) # 有符号字节array数据类型 # test_img = array('B', test_imgs.read()) # test_lab = array('b', test_labs.read()) train_imgs.close() # 关闭文件 train_labs.close() # test_imgs.close() # test_imgs.close() # 保存图像 for (i, label) in enumerate(train_lab): filename = os.path.join(train_folders[label], str(i) + '.png') print('writing ' + filename) with open(filename, 'wb') as img: image = png.Writer(28, 28, greyscale=True) data = [ train_img[(i * 28 * 28 + j * 28):(i * 28 * 28 + (j + 1) * 28)] for j in range(28) ] image.write(img, data) # 保存训练图像 """ for (i, label) in enumerate(test_lab): filename = os.path.join(test_folders[label], str(i) + '.png') print('writing ' + filename) with open(filename, 'wb') as img: image = png.Writer(28, 28, greyscale=True) data = [test_img[(i*28*28 + j*28) : (i*28*28 + (j+1)*28)] for j in range(28)] image.write(img, data) # 保存测试图像 """
def exportToImageFile(cls, texture, outputFilePath): binaryFileData = bytes() if texture.encoding == RAW: binaryFileData = texture.buffer else: width, height = texture.width, texture.height targetBitDepth = 8 flatPixelArray = cls.unpackPixels(texture, targetBitDepth) if texture.encoding.hasAlpha: if cls.trimmingEnabled: width, height, flatPixelArray = cls.trimTransparentEdges( flatPixelArray, width, height, texture.encoding.channels) if cls.blackeningEnabled: flatPixelArray = cls.blackenTransparentPixels( flatPixelArray, width, height, texture.encoding.channels) if any(flatPixelArray): # Create an in-memory stream to which we can write the png data. pngStream = io.BytesIO() # Attempt to create a palette channelsPerPixel = len(texture.encoding.channels) pixels = list( zip(*(flatPixelArray[i::channelsPerPixel] for i in range(channelsPerPixel)))) palette = list(set(pixels)) # Palettes cannot contain more than 256 colors. Also, using palettes for greyscale images typically takes more memory, not less. if len(palette) <= (2**8) and not texture.encoding.isGreyscale: getAlphaValue = lambda color: color[channelsPerPixel - 1] palette.sort(key=getAlphaValue) colorToIndex = dict( (color, paletteIndex) for paletteIndex, color in enumerate(palette)) paletteIndexArray = [ colorToIndex[color] for color in pixels ] # Remove alpha from opaque pixels if texture.encoding.hasAlpha: palette = [(color if getAlphaValue(color) < 0xFF else color[:-1]) for color in palette] elif texture.encoding.isGreyscale: palette = [(color[0], ) * 3 for color in palette] # Write the png data to the stream. pngWriter = png.Writer(width, height, palette=palette, bitdepth=targetBitDepth) pngWriter.write_array(pngStream, paletteIndexArray) else: # Write the png data to the stream. pngWriter = png.Writer( width, height, alpha=texture.encoding.hasAlpha, greyscale=texture.encoding.isGreyscale, bitdepth=targetBitDepth, planes=len(texture.encoding.channels)) pngWriter.write_array(pngStream, flatPixelArray) # Add the penultimate chunk. finalChunkSize = 12 pngFileByteArray = bytearray(pngStream.getvalue()) binaryFileData = bytes(pngFileByteArray[:-finalChunkSize] ) + cls.penultimateChunk + bytes( pngFileByteArray[-finalChunkSize:]) if any(binaryFileData): outputDirectory = os.path.dirname(outputFilePath) if not os.path.isdir(outputDirectory): os.makedirs(outputDirectory) with open(outputFilePath, 'wb') as outputFileHandle: outputFileHandle.write(binaryFileData)
def write_aln_to_fig(seqs, outfilename, sizeincrease=10, sizexincrease=5, sizeyincrease=20, breakup=1): #all amb are grey, a red, t blue, c yellow, g green position = { "A": 1, "C": 2, "G": 3, "T": 4, "-": 0, "N": 0, "Y": 5, "R": 5, "W": 5, "M": 5, "B": 5, "V": 5, "S": 5, "K": 5, "H": 5 } order = {} count = 0 for i in seqs: order[i.name] = count count += 1 array = [''] * len(order) palette = [(0xff, 0xff, 0xff), (0xff, 0x00, 0x00), (0xff, 0xff, 0x00), (0x00, 0xff, 0x00), (0x00, 0xff, 0xff), (0x80, 0x80, 0x80)] for i in seqs: #duplicateit std = "" for j in i.seq: for k in range(sizexincrease): std += str(position[j.upper()]) array[order[i.name]] = "".join(std) start = 0 end = 0 for i in range(breakup): nend = end start = nend end = min(start + (len(array) / breakup), len(array)) + 1 #size increase in blocks order2 = [] for j in array[start:end]: for k in range(sizeyincrease): order2.append(j) tarray = map(lambda x: map(int, x), order2) outfile = None if breakup == 1: outfile = open(outfilename + ".png", "wb") else: outfile = open(outfilename + "." + str(i) + ".png", "wb") w = png.Writer(len(tarray[0]), len(tarray), palette=palette, bitdepth=4) w.write(outfile, tarray) outfile.close()
def finish_image(self): f = open('images/output/test.png', 'wb') w = png.Writer(width=WIDTH * SCALE, height=HEIGHT * SCALE, bitdepth=8, greyscale=False) w.write(f, self.pixels) return f.name
last = now dev.wait_for_frames() c = dev.color c = cv2.cvtColor(c, cv2.COLOR_RGB2BGR) d = dev.dac # Visualize count down if time.time() - T_start > 5: filecad = folder + "JPEGImages/%s.jpg" % FileName filedepth = folder + "depth/%s.png" % FileName cv2.imwrite(filecad, c) with open(filedepth, 'wb') as f: writer = png.Writer(width=d.shape[1], height=d.shape[0], bitdepth=16, greyscale=True) zgray2list = d.tolist() writer.write(f, zgray2list) FileName += 1 if time.time() - T_start > RECORD_LENGTH + 5: dev.stop() serv.stop() break if time.time() - T_start < 5: cv2.putText(c, str(5 - int(time.time() - T_start)), (240, 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 4, (0, 0, 255), 2, cv2.LINE_AA)
def write(self): w = png.Writer(width=self.width, height=self.height, greyscale=True) with open(self.outfile, 'wb') as f: w.write(f, self.image)
import os import pydicom import matplotlib.pyplot as plt np.set_printoptions(threshold=sys.maxsize) dcm_dir = "/home/lin/Desktop/test/patient/" png_dir = "/home/lin/Desktop/test/png" for dcm_name in os.listdir(dcm_dir)[:20]: if dcm_name.endswith("dcm"): print(os.path.join(dcm_dir, dcm_name)) img = pydicom.dcmread(os.path.join(dcm_dir, dcm_name)) img = img.pixel_array wl, wh = (100, 150.0) img = img.astype("float32").clip(wl, wh) img = (img - wl) / (wh - wl) * 256 img = img.astype("uint8") # plt.imshow(img) # plt.show() writer = png.Writer(width=img.shape[1], height=img.shape[0], bitdepth=8, greyscale=True) img = img.tolist() img_path = os.path.join(png_dir, os.path.basename(dcm_name).replace("dcm", "png")) with open(img_path, "wb") as f: writer.write(f, img)
def save_ora(size: Tuple[int, int], layers: List["Layer"], colors: List[Tuple[int, int, int, int]], merged: np.ndarray, path: str, **kwargs): """ An ORA file is basically a zip archive containing an XML manifest and a bunch of PNGs. It can however contain any other application specific data too. """ w, h = size # Build "stack.xml" that specifies the structure of the drawing image_el = ET.Element("image", version="0.0.3", w=str(w), h=str(h)) stack_el = ET.SubElement(image_el, "stack") has_empty_frame = False for i, layer in reversed(list(enumerate(layers))): visibility = "visible" if layer.visible else "hidden" if len(layer.frames) == 1: # Non-animated layer ET.SubElement(stack_el, "layer", name=f"layer{i}_frame{0}", src=f"data/layer{i}_frame{0}.png") else: # Animated layer layer_el = ET.SubElement(stack_el, "stack", name=f"layer{i}", visibility=visibility) for j, frame in reversed(list(enumerate(layer.frames))): if frame is not None: ET.SubElement(layer_el, "layer", name=f"layer{i}_frame{j}", src=f"data/layer{i}_frame{j}.png") else: ET.SubElement(layer_el, "layer", name=f"layer{i}_frame{j}", src=f"data/empty.png") has_empty_frame = True stack_xml = b"<?xml version='1.0' encoding='UTF-8'?>" + ET.tostring(image_el) # Create ZIP archive with zipfile.ZipFile(path, mode="w", compression=zipfile.ZIP_DEFLATED) as orafile: orafile.writestr("mimetype", "image/openraster", compress_type=zipfile.ZIP_STORED) orafile.writestr("stack.xml", stack_xml) for i, layer in reversed(list(enumerate(layers))): for j, frame in reversed(list(enumerate(layer.frames))): if frame is not None: with io.BytesIO() as f: _save_png(frame, f, colors=colors) f.seek(0) orafile.writestr(f"data/layer{i}_frame{j}.png", f.read()) if has_empty_frame: empty_frame = np.zeros(size, dtype=np.uint8) with io.BytesIO() as f: _save_png(empty_frame, f, colors=colors) f.seek(0) orafile.writestr(f"data/empty.png", f.read()) # Thumbnail # Can be max 256 pixels in either dimension. aspect = w / h if aspect >= 1: wt = min(w, 256) ht = int(wt / aspect) else: ht = min(h, 256) wt = int(ht * aspect) thumbnail = scale(merged, wt, ht) rgba_thumbnail = make_rgba_image(thumbnail, colors) with io.BytesIO() as f: writer = png.Writer(width=wt, height=ht, bitdepth=8, greyscale=False, alpha=True) rows = (rgba_thumbnail[:, i].tobytes() for i in range(ht)) writer.write(f, rows) f.seek(0) orafile.writestr(f"Thumbnails/thumbnail.png", f.read()) # Merged image rgba_merged = make_rgba_image(merged, colors) with io.BytesIO() as f: writer = png.Writer(width=w, height=h, bitdepth=8, greyscale=False, alpha=True) rows = (rgba_merged[:, i].tobytes() for i in range(h)) writer.write(f, rows) f.seek(0) orafile.writestr(f"mergedimage.png", f.read()) # Other data (not part of ORA standard) orafile.writestr("oldpaint.json", json.dumps(kwargs))
def _png(code, version, file, scale=1, module_color=None, background=None): """See: pyqrcode.QRCode.png() This function was abstracted away from QRCode to allow for the output of QR codes during the build process, i.e. for debugging. It works just the same except you must specify the code's version. This is needed to calculate the PNG's size. This method will write the given file out as a PNG file. Note, it depends on the PyPNG module to do this. """ import png #Coerce scale parameter into an integer try: scale = int(scale) except ValueError: raise ValueError('The scale parameter must be an integer') def scale_code(code): """To perform the scaling we need to inflate the number of bits. The PNG library expects all of the bits when it draws the PNG. Effectively, we double, tripple, etc. the number of columns and the number of rows. """ #This is the row to show up at the top and bottom border border_module = [1] * scale border_row = [1] * _get_png_size(version, scale) border = [border_row] * scale #This is one row's worth of each possible module #PNG's use 0 for black and 1 for white, this is the #reverse of the QR standard black = [0] * scale white = [1] * scale #This will hold the final PNG's bits bits = [] #Add scale rows before the code as a border, #as per the standard bits.extend(border) #Add each row of the to the final PNG bits for row in code: tmp_row = [] #Add one all white module to the beginning #to create the vertical border tmp_row.extend(border_module) #Go through each bit in the code for item in row: #Add one scaled module if item == 0: tmp_row.extend(white) else: tmp_row.extend(black) #Add one all white module to the end #to create the vertical border tmp_row.extend(border_module) #Copy each row scale times for n in range(scale): bits.append(tmp_row) #Add the bottom border bits.extend(border) return bits def png_pallete_color(color): """This creates a palette color from a list or tuple. The list or tuple must be of length 3 (for rgb) or 4 (for rgba). The values must be between 0 and 255. Note rgb colors will be given an added alpha component set to 255. The pallete color is represented as a list, this is what is returned. """ if color: rgba = [] if not (3 <= len(color) <= 4): raise ValueError('Colors must be a list or tuple of length ' ' 3 or 4. You passed in ' '"{}".'.format(color)) for c in color: c = int(c) if 0 <= c <= 255: rgba.append(int(c)) else: raise ValueError('Color components must be between ' ' 0 and 255') #Make all all colors have an alpha channel if len(rgba) == 3: rgba.append(255) return rgba #If the user passes in one parameter, then they must pass in both or neither #Note, this is a logical xor if (not module_color) != (not background): raise ValueError('If you specify either the black or white parameter, ' 'then you must specify both.') #Create the pallete, or set greyscale to True if module_color: palette = [ png_pallete_color(module_color), png_pallete_color(background) ] greyscale = False else: palette = None greyscale = True #The size of the PNG size = _get_png_size(version, scale) #We need to increase the size of the code to match up to the #scale parameter. code = scale_code(code) #Write out the PNG with _get_file(file, 'wb') as f: w = png.Writer(width=size, height=size, greyscale=greyscale, palette=palette, bitdepth=1) w.write(f, code)
def compress(data, *args, **kwargs): '''PNG compression ''' TMPFOLDER = tempfile.mkdtemp() compressed_data = '' sizes = [] for iz in range(0, data.shape[0]): img = data[iz, :, :] colorized = np.zeros((3, img.shape[0], img.shape[1]), dtype=np.uint16) # for every value split into three 16 bit samples colorized[0, :, :] = img % (2**16) img = img >> 16 colorized[1, :, :] = img % (2**16) img = img >> 16 colorized[2, :, :] = img % (2**16) colorized = colorized.swapaxes(0, 1).swapaxes(1, 2) row_count, column_count, plane_count = colorized.shape pngfile = open(TMPFOLDER + '/tmp_' + str(iz) + '.png', 'wb') pngWriter = png.Writer(column_count, row_count, greyscale=False, alpha=False, bitdepth=16) pngWriter.write( pngfile, np.reshape(colorized, (-1, column_count * plane_count))) pngfile.close() with open(TMPFOLDER + '/tmp_' + str(iz) + '.png', 'rb') as fd: c_data = fd.read() compressed_data += c_data sizes.append(len(c_data)) frames = np.zeros((len(sizes)), dtype=np.uint64) for i, s in enumerate(sizes): frames[i] = s # # # no of frames output = np.uint64(len(sizes)).tobytes() # frame sizes output += frames.tobytes() output += compressed_data # print sizes return output
def geom_image(image_data, norm=None, vmin=None, vmax=None): """ Displays image specified by ndarray with shape (n, m) or (n, m, 3) or (n, m, 4). This geom is not as flexible as geom_raster or geom_tile but vastly superior in the terms of rendering efficiency. This geom doesn't understand any aesthetics. It doesn't support color scales either. The following images will be rendered depending on the input array: N x M - gray-scale N x M x 3 - RGB N x M x 4 - RGBA The type of values in array can be int, uint or float of any size. The value for each component of integer arrays should be in the range [0,255]. The value for each component of float arrays should be in the range [0,1] for RGB or RGBA images. If gray-scale is encoded as float array then the values will be normalized. If arguments vmin/vmax are specified, they will be used in normalization. Otherwise, min/max value will be computed from the image data. Parameters ---------- image_data : numpy.ndarray with shape (n, m) or (n, m, 3) or (n, m, 4) Specifies image type, size and pixel values. norm : bool False - disables default scaling of a 2-D float (luminance) input to the (0, 1) range. vmin, vmax : scalar, optional, default: None Uses normalized luminance data. Only applied to gray-scale images encoded as float array. Returns ------- geom object specification Examples -------- >>> import numpy as np >>> from lets_plot import * >>> image = np.random.choice([0.0, 1.0], [10, 100, 3]) >>> ggplot() + geom_image(image) """ if png == None: raise Exception("pypng is not installed") if not is_ndarray(image_data): raise Exception( "Invalid image_data: ndarray is expected but was {}".format( type(image_data))) if image_data.ndim not in (2, 3): raise Exception( "Invalid image_data: 2d or 3d array is expected but was {}-dimensional" .format(image_data.ndim)) vmin = float(vmin) if vmin else None vmax = float(vmax) if vmax else None if vmin and vmax and vmin >= vmax: raise Exception( "vmin value must be less then vmax value, was: {} >= {}".format( vmin, vmax)) # Figure out the type of the image if image_data.ndim == 2: height, width = image_data.shape image_type = 'gray' nchannels = 1 else: height, width, nchannels = image_data.shape if nchannels == 3: image_type = 'rgb' elif nchannels == 4: image_type = 'rgba' else: raise Exception( "Invalid image_data: num of channels in color image expected 3 (RGB) or 4 (RGBA) but was {}" .format(nchannels)) # Choose scaler function (sometimes - normalization) if image_data.dtype.kind == 'f': if image_type == 'gray': normaize = as_boolean(norm, default=True) if normaize: # normalize values (gray-scale, floats) lower = vmin if vmin else image_data.min() upper = vmax if vmax else image_data.max() if lower == upper: # 'normalize' to grey def scaler(v): return 127 else: offset = -lower ratio = 255. / (upper - lower) def scaler(v): return _scaler_to_byte(v, offset, ratio) else: # do not normalize scaler = _scaler_0_255_byte else: # do not normalize values (colors) scaler = _scaler_0_1_byte elif image_data.dtype.kind in ('i', 'u'): # do not normalize values (ints) scaler = _scaler_0_255_byte else: raise Exception( "Invalid image_data: floating point or integer dtype is expected but was '{}'" .format(image_data.dtype)) # set output type to int8 - pypng produces broken colors with other types scale = numpy.vectorize(scaler, otypes=[numpy.int8]) # from [[[R, G, B], [R, G, B]], ...] to [[R, G, B, R, G, B],..], or pypng will fail image_2d = scale(image_data).reshape(-1, width * nchannels) png_bytes = io.BytesIO() png.Writer(width=width, height=height, greyscale=(image_type == 'gray'), alpha=(image_type == 'rgba'), bitdepth=8).write(png_bytes, image_2d) href = 'data:image/png;base64,' + str( base64.standard_b64encode(png_bytes.getvalue()), 'utf-8') # image bounds (including 1/2 pixel expand in all directions) xmin = [-0.5] ymin = [-0.5] xmax = [width - 0.5] ymax = [height - 0.5] mapping = aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax) return _geom('image', mapping=mapping, href=href)
rows = int(dataset.Rows) cols = int(dataset.Columns) print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format( rows=rows, cols=cols, size=len(dataset.PixelData))) if 'PixelSpacing' in dataset: print("Pixel spacing....:", dataset.PixelSpacing) # use .get() if not sure the item exists, and want a default value if missing print("Slice location...:", dataset.get('SliceLocation', "(missing)")) # plot the image using matplotlib print(dataset.pixel_array) if for_save: ds = dataset shape = ds.pixel_array.shape image_2d = ds.pixel_array.astype(float) image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0 # Convert to uint image_2d_scaled = np.uint8(image_2d_scaled) # Write the PNG file with open('/media/j/DATA/ckpt/AS/2020_08_18_copy/1.png', 'wb') as png_file: w = png.Writer(shape[1], shape[0], greyscale=True) w.write(png_file, image_2d_scaled) else: plt.imshow(dataset.pixel_array, cmap=plt.cm.bone) plt.show()
def write_png(self, file_name, cell_size=10): s = self.to_png_rgbs(cell_size) w = png.Writer(len(s[0]) // 3, len(s), greyscale=False) with open(file_name, 'wb') as f: w.write(f, s)
#zoom=0.003 ## #latitude = 73 #longitude = -40 #zoom=1.0 # creating map using basicProjection newMap = [] n_ = 600 m_ = 700 getPixel = equirectangularProjection( map_rgb, n, m ) basicProjection = planeProjection( latitude*pi/180 , longitude*pi/180 , 1/float(zoom*min(n_, m_)) ) print "constructing new image ..." for i in range(n_): newMap.append([]) for j in range(m_): (lat, lon) = basicProjection(i-(n_/2), j-(m_/2)) (r,g,b) = getPixel(lat, lon) newMap[-1] += [r,g,b] f = open('newMap.png', 'wb') w = png.Writer(m_, n_) w.write(f, newMap) f.close()
def extract_from_bag(rgb_img_list, path_to_bags, path_to_annotations, tol_min=0.06, tol_max=0.2): """ Expects a list of paths to RGB data and the path to the folder containing the related bags with depth data Method to pick the nearest depth frame and pcl in time, based on set time tolerance (min, max). In our case param values where chosen based on a 30 fps sensor Based on how timestamps were formatted to create img files, see also DH_IO.py """ available_bags = [(datetime.datetime.strptime(objname[:-6], "%Y-%m-%d-%H-%M-%S"), objname[:-6], objname[-6:]) for objname in os.listdir(str(path_to_bags)) if objname[-4:] == '.bag'] available_bags.sort(key=lambda x: x[0]) # order chronologically dimg_list = [] # pcls =[] img_index = {} for imgp in rgb_img_list: filename = str(imgp.split("/")[-1]) out_path_rgb = imgp.split("TBA")[0] + "validated/" + filename if os.path.exists(out_path_rgb): continue # skip basestamp = filename.split('_')[0] stampsecs = filename.split('_')[1] if 'jpg' or 'png' in stampsecs: stampsecs = stampsecs[:-4] basestamp = basestamp[:10] + ' ' + basestamp[11:13] + ':' + basestamp[ 14:16] + ':' + basestamp[17:] timestring = basestamp + '.' + stampsecs rgb_time = datetime.datetime.strptime(timestring, "%Y-%m-%d %H:%M:%S.%f") try: img_index[rgb_time]["urls"].append(imgp) except: # new key img_index[rgb_time] = {} img_index[rgb_time]["urls"] = [] img_index[rgb_time]["urls"].append(imgp) for timekey, path_dict in img_index.items(): # array of images for that timekey imgps = path_dict["urls"] origin_path = imgps[0] filenam = origin_path.split("TBA")[1] out_path_rgb = origin_path.split("TBA")[0] + "validated/" + filenam out_path_depth = origin_path.split( "TBA")[0] + "validated/" + filenam[:-4] + 'depth.png' # copy RGB file to validated files folder for n, (bdate, bname, ext) in enumerate( available_bags ): # find bag file the img belongs to (time-wise) try: if timekey >= bdate and timekey < available_bags[n + 1][0]: tgt_bag = bname + ext break except IndexError: # reached end of bag list if timekey >= bdate: tgt_bag = bname + ext break bag = rosbag.Bag(os.path.join(str(path_to_bags), tgt_bag)) # iterate over bag within a certain time window before and after the rgb timestamp #t0_min, t0_max = timekey - datetime.timedelta(seconds=tol_min), timekey + datetime.timedelta(seconds=tol_min) t1_min, t1_max = timekey - datetime.timedelta( seconds=tol_max), timekey + datetime.timedelta(seconds=tol_max) #d_img, pcloud = find_nearest_frame(bag, timekey, t0_min, t0_max, search_list = ['/camera/depth/image_raw','/camera/depth/points']) # If none found, try again with less strict tolerance #if d_img is None: d_img, pcloud = find_nearest_frame( bag, timekey, t1_min, t1_max, search_list=['/camera/depth/image_raw']) #if pcloud is None: # d_img, pcloud = find_nearest_frame(bag, timekey, t1_min, t1_max, search_list=['/camera/depth/points']) if d_img is None: dimg_list.append(d_img) continue #Save copy of depth image locally, one copy for each crop at that timestamp d_img.astype(np.uint16) if path_to_annotations is None: # Just return/save full depth image dimg_list.append(d_img) # save as 16-bit one-channeled PNG shutil.copyfile(origin_path, out_path_rgb) with open(out_path_depth, 'wb') as f: # 16-bit PNG img, with values in millimeters writer = png.Writer(width=d_img.shape[1], height=d_img.shape[0], bitdepth=16) # Convert array to the Python list of lists expected by the png writer. gray2list = d_img.tolist() writer.write(f, gray2list) else: #crop depth image before adding to list # Crop to 2D bbox or polygon dimg_list = crop_test(imgps, path_to_annotations, None, d_img, dimg_list)