File Exchange

image thumbnail

crop whitespace from an image

version (2.4 KB) by Andrew Bliss
CROP gets rid of whitespace around an image


Updated 01 Jun 2012

View Version History

View License

CROP(FILENAME,APPEND,MARGIN) is the full calling form. APPEND and MARGIN are optional inputs.

CROP('filename.ext') crops the image in the file and saves it using the original filename, overwriting the old image. The extension (ext) can be anything IMREAD supports.

CROP(directory) crops all images in a directory.

If APPEND is 1, CROP saves the cropped image as 'filename_crop.ext' in the same directory as the original.

MARGIN sets the margin width in pixels (default is 10).

Example: crop('C:\MATLAB7\toolbox\matlab\demos\html\cruller_01.png',1)

Requirements: your FIND must allow the 'last' option (version 7+?)

Cite As

Andrew Bliss (2021). crop whitespace from an image (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (33)


Sorry to say but it did not work. Turned the image B&W and left margins as is.

Jessica Kern

Igor Gayday

Fereshteh Nazari

Anirudh Thatipelli

de souza alex

Marta Vallejo

Works very well. Thanks a lot


it is amazing! very useful!

Jesus Hinojosa


Siebren Wytzes


Amazing. Thanks so much for this


reza vahid

I have numerical result from PDE in include (U,x,y)
that (x,y)=[-1,1]*[-1,1] mesh! and U is answer PDE, how can use this function !
Please any example or tip ! let me know !!


Love this script. Use it all the time, brilliant.


Can I nominate this for script of the year? This is amazing. I wish I knew about this script earlier.


Just Perfect, thanks!


where is the file???? anybody send me on my email id?
thanks in advance..

Andrew Bliss

Johan- it shouldn't change the colors. Can you give more details or an example file?


Very helpful but does anybody know why it changes the colours of the picture ?


Very useful. Works like a charm.



Nice job, thanks very much.


I tried savefig but it doesn't crop, this one works great! Keeps me from manually cropping every time with another software

Mohsen Jamali

Works very nice, thanks.


Thank you, thank you, thank you!

even coil

very helpful, thank you

curran eggertson

I wish I had found this earlier. This could have saved me manually cropping about 300 images already, but atleast still saves the next 700.

Mike Hoff

Wow! This just saved me a wealth of time, Thank you so much!!! No more opening each image one by one in Picture Manager and manually cropping

Maider Marin

very usefull, Thanks

Clayton Stanley

Great little *.m file!
Modified it a bit, but more-or-less ready to go.
Thanks a bunch!

Tom Close

Saved me pulling my hair out trying to work this out!

MATLAB Release Compatibility
Created with R14SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!