Making a program that crops images to become squares. But having an issue
in
Programming Questions
•
11 months ago
Hi everyone, I'm making a code that take an images and crops it into a square. For example, 400*500 becomes 400*400.
It working great when the width is greater than the height, but when the height is greater than the width then it doesn't crop into a perfect square for some reason. The height is still a tiny bit bigger that the width. Help?
Here is my code:
PImage img; //Create image
void setup() {
img = loadImage("tall.jpg"); //Initialize the picture
size(img.width, img.height);
}
void draw() {
background(0);
squareImg(img);
}
PImage squareImg(PImage img) {
boolean wBigger = false;
boolean hBigger = false;
if (img.width>img.height) {
copy(img, 0, 0, img.height, img.height, 0, 0, img.height, img.height); //Crops. This works perfectly.
}
else if (img.width<img.height) {
copy(img, 0, 0, img.width, img.width, 0, 0, img.width, img.width); //Crops. Doesn't become a perfect square.
}
else{
copy(img, 0, 0, img.width, img.height, 0, 0, img.width, img.height); //Don't change it
}
return img;
}
1