class Image(object): def __init__(self, image_id): super(Image, self).__init__() self.image = image_id self.drawable = pdb.gimp_image_active_drawable(self.image) pdb.gimp_context_set_brush("2. Hardness 100") pdb.gimp_context_set_sample_transparent(True) pdb.gimp_context_set_sample_merged(False) @timed def get_width(self): return self.drawable.width @timed def get_height(self): return self.drawable.height @timed def draw_brush_line(self, (red, green, blue, opacity, x1, y1, x2, y2, size)): change_foreground_color(self.from_normalized_color((red, green, blue))) change_size(self.from_normalized_size(size)) points = self.convert_points(Point(x1, y1), Point(x2, y2)) self.add_opacity_layer(opacity) pdb.gimp_paintbrush_default(self.drawable, len(points), points) self.merge_layers()
def scale_shape((x1, y1, x2, y2, size)): scaled_line = Line(Point(x1.value, y1.value), Point(x2.value, y2.value)).scale(scale) x1.value = scaled_line.start.x y1.value = scaled_line.start.y x2.value = scaled_line.end.x y2.value = scaled_line.end.y size.value = size.value * scale
def scale_shape((x1, y1, x2, y2, x3, y3)): scaled_triangle = Triangle(Point(x1.value, y1.value), Point(x2.value, y2.value), Point(x3.value, y3.value)).scale(scale) x1.value = scaled_triangle.a.x y1.value = scaled_triangle.a.y x2.value = scaled_triangle.b.x y2.value = scaled_triangle.b.y x3.value = scaled_triangle.c.x y3.value = scaled_triangle.c.y
def select_brush_line(self, x1, y1, x2, y2, size): point1 = self.__from_normalized_point(Point(x1, y1)) point2 = self.__from_normalized_point(Point(x2, y2)) drawable = pdb.gimp_image_active_drawable(self.image) size_to_change = max(1, max(drawable.height, drawable.width) * size) pdb.gimp_context_set_brush_size(size_to_change) points = [point1.x, point1.y, point2.x, point2.y] pdb.gimp_paintbrush_default(drawable, len(points), points) center = Point((point1.x + point2.x) / 2, (point1.y + point2.y) / 2) pdb.gimp_image_select_contiguous_color(self.image, CHANNEL_OP_REPLACE, drawable, center.x, center.y)
def __draw_triangle_edges(self, drawable, x1, x2, x3, y1, y2, y3): point1 = self.__from_normalized_point(Point(x1, y1)) point2 = self.__from_normalized_point(Point(x2, y2)) point3 = self.__from_normalized_point(Point(x3, y3)) first_line = [point1.x, point1.y, point2.x, point2.y] second_line = [point3.x, point3.y, point2.x, point2.y] third_line = [point1.x, point1.y, point3.x, point3.y] pdb.gimp_pencil(drawable, len(first_line), first_line) pdb.gimp_pencil(drawable, len(second_line), second_line) pdb.gimp_pencil(drawable, len(third_line), third_line) return point1, point2, point3
def select_triangle(self, x1, y1, x2, y2, x3, y3): drawable = pdb.gimp_image_active_drawable(self.image) pdb.gimp_context_set_brush_size(1) point1, point2, point3 = self.__draw_triangle_edges(drawable, x1, x2, x3, y1, y2, y3) center = Point((point1.x + point2.x + point3.x) / 3, (point1.y + point2.y + point3.y) / 3) pdb.gimp_image_select_contiguous_color(self.image, CHANNEL_OP_REPLACE, drawable, center.x, center.y)
def create_selection((x, y, width, height, angle)): Selection(image, Point(x.value, y.value), width.value, height.value).select_ellipse()
def __from_normalized_point(self, point): return Point(max(1, point.x * self.image.width), max(1, point.y * self.image.height))
def from_normalized_point(self, point): return Point(max(1, point.x * self.get_width()), max(1, point.y * self.get_height()))
start = self.from_normalized_point(start) end = self.from_normalized_point(end) points = [start.x, start.y, end.x, end.y] return points @timed def from_normalized_point(self, point): return Point(max(1, point.x * self.get_width()), max(1, point.y * self.get_height())) @timed def draw_rectangle(self, (red, green, blue, opacity, x, y, width, height, angle)): change_foreground_color(self.from_normalized_color((red, green, blue))) self.add_opacity_layer(opacity) selection = Selection(self.image, Point(x, y), width, height) selection.select_rectangle() self.fill_selection() self.rotate_and_merge(angle, opacity) @timed def draw_ellipse(self, (red, green, blue, opacity, x, y, width, height, angle)): change_foreground_color(self.from_normalized_color((red, green, blue))) opacity_layer = self.add_opacity_layer(opacity) selection = Selection(self.image, Point(x, y), width, height) selection.select_ellipse() self.fill_selection() self.rotate_and_merge(angle, opacity, opacity_layer) @timed