def get_bbox(self, real=True): """ Get BBox(x1, y1, x2, y2) namedtuple with mask bounding box. :param real: When False, ignore real flags. """ if real and self.has_real(): return BBox(self.mask_data.real_left, self.mask_data.real_top, self.mask_data.real_right, self.mask_data.real_bottom) else: return BBox(self.mask_data.left, self.mask_data.top, self.mask_data.right, self.mask_data.bottom)
def get_layers_from_psd(decoded_data, psd_layers, bl_layers_data): '''psd data to bl image bl_layers_data''' for layer in psd_layers: if layer.is_group: lm_layer = bl_layers_data.layers.add() lm_layer.type = "GROUP_START" lm_layer.is_visible = layer.visible lm_layer.name = layer.name lm_layer.group_expand = not layer.closed ImportLayersImage.get_layers_from_psd(decoded_data, layer.layers, bl_layers_data) lm_layer = bl_layers_data.layers.add() lm_layer.type = "GROUP_END" else: # is real layer header = decoded_data.header lm_layer = bl_layers_data.layers.add() lm_layer.type = "LAYER" lm_layer.is_visible = layer.visible lm_layer.name = layer.name bbox = layer.bbox lm_layer["position"] = bl_support.psd_xy_to_bl( header.height, bbox.x1, bbox.y1, bbox.x2, bbox.y2) layers_rd = decoded_data.layer_and_mask_data.layers layer_rd = layers_rd.layer_records[layer._index] layer_bbox = BBox(layer_rd.left, layer_rd.top, layer_rd.right, layer_rd.bottom) bl_channels = bl_support.psd_layer_to_bl_rgba( layers_rd.channel_image_data[layer._index], bl_support._get_layer_channel_ids(layer_rd), header.depth, layer_bbox, target_has_alpha=True ) lm_layer["channels"] = bl_channels.tolist() return
def layers_mix(self): ''' update layer image from its layers data ''' image = self.image layers = self.layers_in_image() # filter layer layers = [layer for layer in layers if layer["position"][3] - layer["position"][1] > 0 or layer["position"][2] - layer["position"][0] > 0] base_bbox = BBox(0, 0, image.size[0], image.size[1]) base_channels = numpy.ones((4, base_bbox.height, base_bbox.width)) base_channels[3] = 0 for layer in reversed(layers): layer_bbox = BBox(layer["position"][0], layer["position"][1], layer["position"][2], layer["position"][3]) layer_channels = numpy.array(layer["channels"]) bl_support.bl_layers_nor_mix( base_channels, base_bbox, layer_channels, layer_bbox) image.pixels[:] = bl_support.bl_rgba_mix(base_channels)
def new_image_from_layers(self, start_idx, end_idx): layers = self.image.layers_data.layers real_layers = LayersImageManager.real_layers_in_layers( layers, start_idx, end_idx, only_visible=False) c_pos = LayersImageManager.combined_position(real_layers) c_bbox = BBox(c_pos[0], c_pos[1], c_pos[2], c_pos[3]) image_name = layers[start_idx].name new_image = bpy.data.images.new( image_name, c_bbox.width, c_bbox.height, alpha=True) new_layers = new_image.layers_data.layers for layer in layers[start_idx:end_idx + 1]: new_layer = new_layers.add() new_layer.name = layer.name new_layer.group_expand = layer.group_expand new_layer.type = layer.type new_layer.is_visible = layer.is_visible if layer.type == "LAYER": new_layer["channels"] = layer["channels"] new_layer["position"] = LayersImageManager.relative_position(c_pos, layer["position"]) LayersImageManager(new_image).layers_mix() return new_image
def test_userapi_placed_layers(): img = PSDImage(decode_psd("placedLayer.psd")) bg = img.layers[3] assert bg.kind == 'pixel' assert not hasattr(bg, 'object_bbox') assert not hasattr(bg, 'placed_bbox') layer0 = img.layers[0] assert layer0.kind == 'smartobject' assert layer0.object_bbox == BBox(0, 0, 64, 64) assert layer0.placed_bbox == BBox(x1=96.0, y1=96.0, x2=160.0, y2=160.0) layer1 = img.layers[1] assert layer1.kind == 'smartobject' assert layer1.object_bbox == BBox(0, 0, 101, 55) assert layer1.object_bbox.width == 101 assert layer1.placed_bbox == BBox(x1=27.0, y1=73.0, x2=229.0, y2=183.0) layer2 = img.layers[2] assert layer2.kind == 'smartobject' assert layer2.object_bbox == BBox(0, 0, 64, 64) assert layer2.placed_bbox == BBox(x1=96.0, y1=96.0, x2=160.0, y2=160.0)
('hidden-layer.psd', (100, 150)), ('history.psd', (100, 150)), ('mask.psd', (100, 150)), ('note.psd', (300, 300)), ('pen-text.psd', (300, 300)), ('smart-object-slice.psd', (100, 100)), ('transparentbg.psd', (100, 150)), ('transparentbg-gimp.psd', (40, 40)), ('vector-mask.psd', (100, 150)), ('gray0.psd', (400, 359)), ('gray1.psd', (1800, 1200)), ('empty-layer.psd', (100, 150)), )) BBOXES = ( ('1layer.psd', 0, BBox(0, 0, 101, 55)), ('2layers.psd', 0, BBox(8, 4, 93, 50)), ('2layers.psd', 1, BBox(0, 0, 101, 55)), ('group.psd', 0, BBox(25, 24, 66, 98)), ('empty-layer.psd', 0, BBox(37, 58, 51, 72)), ('empty-layer.psd', 1, BBox(0, 0, 100, 150)), ) RESOLUTIONS = ( ('1layer.psd', ResolutionInfo( h_res=72.0, h_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH, v_res=72.0, v_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH, width_unit=DimensionUnit.INCH, height_unit=DimensionUnit.INCH)), ('group.psd', ResolutionInfo( h_res=72.0, h_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH, v_res=72.0, v_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH,
('hidden-layer.psd', (100, 150)), ('history.psd', (100, 150)), ('mask.psd', (100, 150)), ('note.psd', (300, 300)), ('pen-text.psd', (300, 300)), ('smart-object-slice.psd', (100, 100)), ('transparentbg.psd', (100, 150)), ('transparentbg-gimp.psd', (40, 40)), ('vector-mask.psd', (100, 150)), ('gray0.psd', (400, 359)), ('gray1.psd', (1800, 1200)), ('empty-layer.psd', (100, 150)), )) BBOXES = ( ('1layer.psd', 0, BBox(0, 0, 101, 55)), ('2layers.psd', 0, BBox(8, 4, 93, 50)), ('2layers.psd', 1, BBox(0, 0, 101, 55)), ('group.psd', 0, BBox(25, 24, 66, 98)), ('empty-layer.psd', 0, BBox(36, 57, 52, 73)), ('empty-layer.psd', 1, BBox(0, 0, 100, 150)), ) RESOLUTIONS = ( ('1layer.psd', ResolutionInfo( h_res=72.0, h_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH, v_res=72.0, v_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH, width_unit=DimensionUnit.INCH, height_unit=DimensionUnit.INCH)), ('group.psd', ResolutionInfo( h_res=72.0, h_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH, v_res=72.0, v_res_unit=DisplayResolutionUnit.PIXELS_PER_INCH,