6

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).

Andrew
  • 10,569
  • 5
  • 51
  • 104

3 Answers3

12

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

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
4

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
image_doctor
  • 10,234
  • 23
  • 40
1

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.

Matthias Odisio
  • 1,246
  • 9
  • 11