MATLAB: Remove white padding from image

imageimage processingpadding

I want to remove white padding from an image (see attachment). The size of the padding is random.
So I was thinking of reading in the image and then some looping to check if the entire row/column contains the value 255. However, looping is most of the time not the best way to do stuff. So I'm wondering if there's an easier way to do this, because I've found a similar question where the rows/columns contain 0, but I don't know if this can also be used for this case (with some adjustments).
Thanks in advance.

Best Answer

I = imread('test-image.jpg');
W = all(I == 255, 3);
maskc = all(W, 1);
maskr = all(W, 2);
J = I;
J(maskr,:,:) = 0;
J(:,maskc,:) = 0;
image(J)