def __call__(image): from gamera.plugins import _string_io pixel_type = image.data.pixel_type shape = (image.nrows, image.ncols) typecode = _typecodes[pixel_type] if pixel_type == RGB: shape += (3,) return n.fromstring(_string_io._to_raw_string(image), typecode=typecode, shape=shape)
def binread_sequence(self): self.skip_blanks_and_comments() seqtype = self.get() elemtype = self.get() if seqtype == '\x12': # 1D little endian sequence shape = struct.unpack('<i', self.read(4)) endianness = 'little' elif seqtype == '\x13': # 1D big endian sequence shape = struct.unpack('>i', self.read(4)) endianness = 'big' elif seqtype == '\x14': # 2D little endian sequence shape = struct.unpack('<ii', self.read(8)) endianness = 'little' elif seqtype == '\x15': # 2D big endian sequence shape = struct.unpack('>ii', self.read(8)) endianness = 'big' else: raise TypeError( 'In binread_sequence, read invalid starting seqtype code char: ' + seqtype) seq = [] nelems = 1 for dim in shape: nelems *= dim if elemtype == '\xFF': # generic sequence for i in xrange(nelems): seq.append(self.binread()) if len(shape) > 1: raise TypeError( 'Reading generic 2D sequence not yet suppored.. Must implement turning this into a list of lists...' ) return seq try: atype = PL_TYPECODE_TO_ARRAYTYPE[elemtype] except KeyError: raise TypeError('Unsuppored array elemtype: ' + elemtype) elemsize = int(atype[-1]) data = self.read(nelems * elemsize) ar = numarray.fromstring(data, atype, shape) if sys.byteorder != endianness: ar.byteswap(True) if self.return_lists: ar = ar.tolist() return ar
def binread_sequence(self): self.skip_blanks_and_comments() seqtype = self.get() elemtype = self.get() if seqtype=='\x12': # 1D little endian sequence shape = struct.unpack('<i',self.read(4)) endianness = 'little' elif seqtype=='\x13': # 1D big endian sequence shape = struct.unpack('>i',self.read(4)) endianness = 'big' elif seqtype=='\x14': # 2D little endian sequence shape = struct.unpack('<ii',self.read(8)) endianness = 'little' elif seqtype=='\x15': # 2D big endian sequence shape = struct.unpack('>ii',self.read(8)) endianness = 'big' else: raise TypeError('In binread_sequence, read invalid starting seqtype code char: '+seqtype) seq = [] nelems = 1 for dim in shape: nelems *= dim if elemtype=='\xFF': # generic sequence for i in xrange(nelems): seq.append(self.binread()) if len(shape)>1: raise TypeError('Reading generic 2D sequence not yet suppored.. Must implement turning this into a list of lists...') return seq try: atype = PL_TYPECODE_TO_ARRAYTYPE[elemtype] except KeyError: raise TypeError('Unsuppored array elemtype: '+elemtype) elemsize = int(atype[-1]) data = self.read(nelems*elemsize) ar = numarray.fromstring(data, atype, shape) if sys.byteorder!=endianness: ar.byteswap(True) if self.return_lists: ar = ar.tolist() return ar
def main(): if len(sys.argv) > 1: im_name = sys.argv[1] else: import tkFileDialog, Tkinter im_name = tkFileDialog.askopenfilename( defaultextension=".png", filetypes=((_("Depth images"), ".gif .png .jpg"), (_("All files"), "*")) ) if not im_name: raise SystemExit Tkinter._default_root.destroy() Tkinter._default_root = None im = Image.open(im_name) size = im.size im = im.convert("L") # grayscale w, h = im.size nim = numarray.fromstring(im.tostring(), "UInt8", (h, w)).astype("Float32") options = ui(im, nim, im_name) step = options["pixelstep"] depth = options["depth"] if options["normalize"]: a = nim.min() b = nim.max() if a != b: nim = (nim - a) / (b - a) else: nim = nim / 255.0 maker = tool_makers[options["tool_type"]] tool_diameter = options["tool_diameter"] pixel_size = options["pixel_size"] tool = make_tool_shape(maker, tool_diameter, pixel_size) if options["expand"]: if options["expand"] == 1: pixel = 1 else: pixel = 0 w, h = nim.shape tw, th = tool.shape w1 = w + 2 * tw h1 = h + 2 * th nim1 = numarray.zeros((w1, h1), "Float32") + pixel nim1[tw : tw + w, th : th + h] = nim nim = nim1 w, h = w1, h1 nim = nim * depth if options["invert"]: nim = -nim else: nim = nim - depth rows = options["pattern"] != 1 columns = options["pattern"] != 0 columns_first = options["pattern"] == 3 spindle_speed = options["spindle_speed"] if rows: convert_rows = convert_makers[options["converter"]]() else: convert_rows = None if columns: convert_cols = convert_makers[options["converter"]]() else: convert_cols = None if options["bounded"] and rows and columns: slope = tan(options["contact_angle"] * pi / 180) if columns_first: convert_rows = Reduce_Scan_Lace(convert_rows, slope, step + 1) else: convert_cols = Reduce_Scan_Lace(convert_cols, slope, step + 1) if options["bounded"] > 1: if columns_first: convert_cols = Reduce_Scan_Lace(convert_cols, slope, step + 1) else: convert_rows = Reduce_Scan_Lace(convert_rows, slope, step + 1) units = unitcodes[options["units"]] convert( nim, units, tool, pixel_size, step, options["safety_height"], options["tolerance"], options["feed_rate"], convert_rows, convert_cols, columns_first, ArcEntryCut(options["plunge_feed_rate"], 0.125), spindle_speed, options["roughing_offset"], options["roughing_depth"], options["feed_rate"], )
def main(): if len(sys.argv) > 1: im_name = sys.argv[1] else: import tkFileDialog, Tkinter im_name = tkFileDialog.askopenfilename(defaultextension=".png", filetypes=((_("Depth images"), ".gif .png .jpg"), (_("All files"), "*"))) if not im_name: raise SystemExit Tkinter._default_root.destroy() Tkinter._default_root = None im = Image.open(im_name) size = im.size im = im.convert("L") #grayscale w, h = im.size nim = numarray.fromstring(im.tostring(), 'UInt8', (h, w)).astype('Float32') options = ui(im, nim, im_name) step = options['pixelstep'] depth = options['depth'] if options['normalize']: a = nim.min() b = nim.max() if a != b: nim = (nim - a) / (b - a) else: nim = nim / 255.0 maker = tool_makers[options['tool_type']] tool_diameter = options['tool_diameter'] pixel_size = options['pixel_size'] tool = make_tool_shape(maker, tool_diameter, pixel_size) if options['expand']: if options['expand'] == 1: pixel = 1 else: pixel = 0 w, h = nim.shape tw, th = tool.shape w1 = w + 2 * tw h1 = h + 2 * th nim1 = numarray.zeros((w1, h1), 'Float32') + pixel nim1[tw:tw + w, th:th + h] = nim nim = nim1 w, h = w1, h1 nim = nim * depth if options['invert']: nim = -nim else: nim = nim - depth rows = options['pattern'] != 1 columns = options['pattern'] != 0 columns_first = options['pattern'] == 3 spindle_speed = options['spindle_speed'] if rows: convert_rows = convert_makers[options['converter']]() else: convert_rows = None if columns: convert_cols = convert_makers[options['converter']]() else: convert_cols = None if options['bounded'] and rows and columns: slope = tan(options['contact_angle'] * pi / 180) if columns_first: convert_rows = Reduce_Scan_Lace(convert_rows, slope, step + 1) else: convert_cols = Reduce_Scan_Lace(convert_cols, slope, step + 1) if options['bounded'] > 1: if columns_first: convert_cols = Reduce_Scan_Lace(convert_cols, slope, step + 1) else: convert_rows = Reduce_Scan_Lace(convert_rows, slope, step + 1) units = unitcodes[options['units']] convert(nim, units, tool, pixel_size, step, options['safety_height'], options['tolerance'], options['feed_rate'], convert_rows, convert_cols, columns_first, ArcEntryCut(options['plunge_feed_rate'], .125), spindle_speed, options['roughing_offset'], options['roughing_depth'], options['feed_rate'])