def scaleImgTForms(oldT, scale): newT = Transform() newT.dim = oldT.dim newxCoefs = [] newyCoefs = [] if oldT.dim in range(4,7): #Poly. transform for newCoefs, oldCoefs in [(newxCoefs, oldT.xcoef), (newyCoefs, oldT.ycoef)]: newCoefs.append( oldCoefs[0]*(scale) ) newCoefs.append( oldCoefs[1] ) newCoefs.append( oldCoefs[2] ) newCoefs.append( oldCoefs[3]/scale ) newCoefs.append( oldCoefs[4]/scale ) newCoefs.append( oldCoefs[5]/scale ) newT.xcoef = newxCoefs newT.ycoef = newyCoefs else: # Affine transform for newCoefs, oldCoefs in [(newxCoefs, oldT.xcoef), (newyCoefs, oldT.ycoef)]: newCoefs.append( oldCoefs[0]*(scale) ) newCoefs.append( oldCoefs[1] ) newCoefs.append( oldCoefs[2] ) newCoefs.append( oldCoefs[3] ) newCoefs.append( oldCoefs[4] ) newCoefs.append( oldCoefs[5] ) newT.xcoef = newxCoefs newT.ycoef = newyCoefs newT.tform() return newT
def test_transform_to_xml(self): xml_in = etree.parse(os.path.join(DATA_LOC, "_transform.xml")).getroot() transform = Transform( **reconstruct_reader.extract_transform_attributes(xml_in)) xml_out = reconstruct_writer.transform_to_xml(transform) self.assertEqual( xml_element_to_dict(xml_in), xml_element_to_dict(xml_out), )
def test_shape_point(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) point_contour = Contour( closed=False, points=[(13.5904, 16.6472)], transform=transform, ) self.assertEqual(point_contour.shape.type, "Point")
def process_section_file(path): """Return a Section object from a Section XML file.""" tree = etree.parse(path) root = tree.getroot() # Create Section and populate with metadata data = extract_section_attributes(root) data["name"] = os.path.basename(path) data["_path"] = os.path.dirname(path) section = Section(**data) # Process Images, Contours, Transforms for node in root: # make Transform object transform_data = extract_transform_attributes(node) transform = Transform(**transform_data) children = [child for child in node] # Image node images = [child for child in children if child.tag == "Image"] if len(images) > 1: raise Exception("No support for Sections with more than one Image.") elif images: image_data = extract_image_attributes(images[0]) image_data["_path"] = section._path image_data["transform"] = transform # Image contours image_contours = [child for child in children if child.tag == "Contour"] if len(image_contours) > 1: raise Exception( "No support for Images with more than one Contour.") elif not image_contours: raise Exception("No support for Images with out a Contour.") else: image_contour_data = extract_section_contour_attributes( image_contours[0]) image_data.update(image_contour_data) image = Image(**image_data) section.images.append(image) # Non-Image Node else: for child in children: if child.tag == "Contour": contour_data = extract_section_contour_attributes(child) contour_data["transform"] = transform contour = Contour(**contour_data) section.contours.append(contour) return section
def test_shape_polygon(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) poly_contour = Contour( closed=True, points=[ (19.2342, 15.115), (19.2826, 15.115), (19.2584, 15.1593), ], transform=transform, ) self.assertEqual(poly_contour.shape.type, "Polygon")
def test_shape_weird_line(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) line_contour = Contour( closed=False, points=[ (15.9352, 10.8615), (15.9332, 10.8508), (15.9352, 10.8615), ], transform=transform, ) self.assertEqual(line_contour.shape.type, "LineString")
def test_shape_line(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) line_contour = Contour( closed=False, points=[ (24.6589, 17.3004), (24.7018, 17.3489), (24.7634, 17.3917), (24.8174, 17.4197), ], transform=transform, ) self.assertEqual(line_contour.shape.type, "LineString")
def processSectionFile(tree): '''Returns attribute dictionary, image object, and contour list associated with a Section's XML <tree>''' # Process attributes root = tree.getroot() attributes = sectionAttributes(root) # Process images and contours images = [] contours = None for transform in root: # make Transform object tForm = Transform(transformAttributes(transform)) children = [child for child in transform] # Image transform node img = [child for child in children if child.tag == 'Image'] if len(img) > 0: img = img.pop() img = Image(imageAttributes(img)) imgContour = [ child for child in children if child.tag == 'Contour' ] if len(imgContour) > 0: imgContour = imgContour.pop() contour = Contour(contourAttributes(imgContour), tForm) contour.image = img # set contour's image to the image img.contour = contour # set image's contour to the contour images.append(img) # Non-Image Transform Node else: for child in children: if child.tag == 'Contour': cont = Contour(contourAttributes(child), tForm) if contours == None: contours = [] contours.append(cont) return attributes, images, contours
def process_section_file(path, data_check=False): """Return a Section object from a Section XML file.""" tree = etree.parse(path) root = tree.getroot() # Create Section and populate with metadata data = extract_section_attributes(root) data["name"] = os.path.basename(path) data["_path"] = os.path.dirname(path) section = Section(**data) # Process Images, Contours, Transforms for node in root: # make Transform object transform_data = extract_transform_attributes(node) transform = Transform(**transform_data) children = [child for child in node] # Image node images = [child for child in children if child.tag == "Image"] if images: image_data = extract_image_attributes(images[0]) image_data["_path"] = section._path image_data["transform"] = transform # Image contours image_contours = [ child for child in children if child.tag == "Contour" ] if len(image_contours) > 1: raise Exception( "No support for Images with more than one Contour.") elif not image_contours: raise Exception("No support for Images with out a Contour.") else: image_contour_data = extract_section_contour_attributes( image_contours[0]) image_data.update(image_contour_data) image = Image(**image_data) if data_check: # Check if ref exists image_path = os.path.join(image._path, image.src) if not os.path.isfile(image_path): print( "WARNING: Could not find referenced image: {}".format( image_path)) section.images.append(image) # Non-Image Node else: for child in children: if child.tag == "Contour": contour_data = extract_section_contour_attributes(child) contour_data["transform"] = transform contour = Contour(**contour_data) section.contours.append(contour) if data_check: if not section.images: print("WARNING: section {} is missing an Image.".format( section.index)) elif len(section.images) > 1: print("WARNING: section {} contains more than one Image.".format( section.index)) return section