how to crop image

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mohsenisaei

Hi, I am using Godot version 3.4.3.
I am making a 2D game, in this game it is necessary that everyone can cut his own head from his own photo to use on the main character. And the image of his head is placed instead of the character’s head.
It cannot be done with the “crop” command. Because it is cut in a rectangular shape.
While I need the shape of a human head, or finally cut into an oval shape.

What is your suggestion?

:bust_in_silhouette: Reply From: exuin

The image will be a rectangle no matter what, but you can turn all the pixels that are not in the oval transparent. So just use the formula for calculating if a point is inside an ellipse to see if so.

if pow(pixel.x - center.x, 2) / pow(radius.x, 2) + pow(pixel.y - center.y, 2) / pow(radius.y, 2) > 1:
    image.set_pixel(pixel, Color(0, 0, 0, 0))

Did not Work

mohsenisaei | 2022-08-02 14:24

Please stop posting multiple questions about this, or you will be blocked from using this website.

Calinou | 2022-08-14 18:33