Implement Canvas Size feature

This commit is contained in:
2024-05-20 23:53:16 +09:00
parent 9913ae8c35
commit a580a4e137
3 changed files with 168 additions and 3 deletions
+72 -1
View File
@@ -1,5 +1,5 @@
'''
Copyright (C) 2021 - 2023 Akaneyu
Copyright (C) 2021 - 2024 Akaneyu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -764,6 +764,77 @@ class IMAGE_EDITOR_PLUS_OT_scale(bpy.types.Operator):
return {'FINISHED'}
class IMAGE_EDITOR_PLUS_OT_change_canvas_size(bpy.types.Operator):
"""Apply settings"""
bl_idname = "image_editor_plus.change_canvas_size"
bl_label = "Change Canvas Size"
width: bpy.props.IntProperty()
height: bpy.props.IntProperty()
expand_from_center: bpy.props.BoolProperty()
use_background_color: bpy.props.BoolProperty()
def execute(self, context):
wm = context.window_manager
props = wm.imageeditorplus_properties
if self.use_background_color:
color = props.background_color[:] + (1.0,)
else:
color = (0, 0, 0, 0)
img = context.area.spaces.active.image
if not img:
return {'CANCELLED'}
width, height = img.size
pixels = utils.read_pixels_from_image(img)
if self.width > width or self.height > height:
expand_width = self.width - width if self.width > width else 0
expand_height = self.height - height if self.height > height else 0
if self.expand_from_center:
expand_left = int(expand_width / 2)
expand_top = int(expand_height / 2)
expand_right = expand_width - expand_left
expand_bottom = expand_height - expand_top
else:
expand_left, expand_top = 0, 0
expand_right = expand_width
expand_bottom = expand_height
new_pixels = np.pad(pixels, ((expand_bottom, expand_top),
(expand_left, expand_right), (0, 0)),
constant_values=np.array(((color, color), (color, color), (0, 0)),
dtype=object))
else:
new_pixels = pixels
if self.width < width or self.height < height:
shrink_width = width - self.width if self.width < width else 0
shrink_height = height - self.height if self.height < height else 0
if self.expand_from_center:
shrink_left = int(shrink_width / 2)
shrink_top = int(shrink_height / 2)
else:
shrink_left, shrink_top = 0, 0
if self.height < height:
new_pixels = new_pixels[height - self.height - shrink_top:height - shrink_top,
shrink_left:self.width + shrink_left]
else:
new_pixels = new_pixels[0:self.height,
shrink_left:self.width + shrink_left]
img.scale(self.width, self.height)
utils.write_pixels_to_image(img, new_pixels)
app.refresh_image(context)
return {'FINISHED'}
class IMAGE_EDITOR_PLUS_OT_flip_layer(bpy.types.Operator):
"""Flip the layer"""
bl_idname = "image_editor_plus.flip_layer"