We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm hoping someone can help me with this. I want to create a function that takes a PImage, say 640x640 in size, and crops it to the non-alpha area bounds of the image. It would then return that image.
As an example, the 640x640 PImage it takes is fully transparent except for a circle of 50 pixels in diameter. The function would crop to that circle and return a PImage of 50x50 with the circle. It would also be useful to indicate a buffer area around the subject, i.e. the circle, and pad it with transparent pixels. Say you wanted a padding of 10 pixels per side, the fuction would return a PImage of 70x70 with the circle. Did I make that clear enough?
Thanks.
Answers
This can be easily done by:
You will end with two points that will define the square region you have to crop.
Thanks, I'll put something together with your suggestions. That makes sense.
Perhaps simpler, and faster: examine each row of pixels, starting from top (downward) and from bottom (upward). Just stop each search when you meet an opaque pixel, you found out minY and maxY.
Do the same for columns of pixels: from left (to the right) and from right (to the left). Gives minX and maxX.
Crop the image at these positions.
No need to explore the whole image.