Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

It seems that ImageCrop allows one to specify the final size of the image. For example, ImageCrop[image, size, spec] crops image image to size size, and one can specify which side of the image the pixels should be taken from, using spec.

But, what if I don't know the final size size of the image? What if I just want to snip 10 pixels from the right side of the image? I might want to do this for a series of images, whose sizes are not known (at least immediately).

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Same basic solution using image take but wrapped up a little:

img = Import["ExampleData/rose.gif"];

Mathematica graphics

Crop[img_, {{dxl_, dxr_}, {dyl_, dyu_}}] := ImageTake[img, {dyl, -dyu}, {dxl, -dxr}]

Where dxl, dxr, dyl, dyu represent the x lext, x right, y lower and y upper sizes of crop.

Crop[img,{{10, 10}, {25, 25}}]

Removes 10 pixels from the left and right edges and 25 from the bottom and top.

Mathematica graphics

You can then apply the same trim a group of images held in a list, by the following

images={im1,im2,im3,im4,...,imn};

Crop[#,{{0,10},{5,0}}]&/@images
share|improve this answer
2  
When ImageTake is provided with negative arguments, it counts down from ImageDimensions, like Part –  belisarius Jul 24 '12 at 20:11
    
@belisarius Thanks for that, I didn't really pick that up from the docs. Which reduces this pretty much to the same as your solution :) –  image_doctor Jul 24 '12 at 22:15
    
From the help -n ... last n elements :) –  belisarius Jul 24 '12 at 23:16
    
@Belisarius It must be the way I read them :) I took, ImageTake[image,-n] gives an image consisting of the last n rows of image to mean you got rows N-n+1 to N, not, 0 to N-n. As in -3 gives you the last 3 rows, not everything but the last 3 rows. –  image_doctor Jul 24 '12 at 23:22
    
Well, you know: Mma docs have been written by 10^7 monkeys –  belisarius Jul 24 '12 at 23:28
show 1 more comment

Your code would be

ImageTake[i, All, {1, -11}]

Examples:

img = Import["ExampleData/rose.gif"];
ImageTake[img, {25, -25}, {10, -10}]

Mathematica graphics

ImageTake[ExampleData[{"TestImage", "Lena"}], {180, -180}, {180, -180}]

Mathematica graphics

share|improve this answer
    
Thanks! OK, I need to specify size as All. Cool! –  Andrew Jul 24 '12 at 19:37
    
@MatthiasOdisio OBOE :) –  belisarius Jul 27 '12 at 18:21
add comment

As an alternative to ImageTake, ImagePad will do the job using the syntax ImagePad[img, {{left, right}, {bottom, top}}]:

img = Import["ExampleData/rose.gif"];
In[10]:= ImagePad[img, {{0, -10}, {0, 0}}] == ImageTake[img, All, {1, -11}]
Out[10]= True

Note the difference between taking up to the last minus 10th column and removing 10 columns.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.