How to autoslice image's alpha background in sprite?

Godot Version

4.2

Question

I wonder if godot has a feature that can auto-slice the excess transparent background without using code, as Cocos Creator has one.
If not, can texture rec does that? Or is there any other tool that can achieve this easily? I mean there’s a lot of image.

If you know how to do it in code, you can make it into a plugin.

1 Like

seems reasonable :+1:

The Image class has a method.

Rect2i get_used_rect ( ) const

Returns a Rect2i enclosing the visible portion of the image, considering each pixel with a non-zero alpha channel as visible.

Thanks to @wyattbiker , An app that can convert those images can be easily done. Here are the codes that I have figured out.

var image_path = OS.get_executable_path().get_base_dir() + "/" + file_name //e.g.  file_name=pic_01.png
var image:Image = Image.new()
image.load(image_path)
var used_rect:Rect2i = image.get_used_rect()
image = image.get_region(used_rect)
image.save_png(image_path)

Also, if you want to use it in TextureRect or Sprite or some stuff like these, you can use these codes.

var image_texture:ImageTexture = ImageTexture.new()
image_texture.create_from_image(image)	
image_texture.set_image(image)
$ImageDisplay.texture=image_texture
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.