Function Repository Resource:

ArrayCropPad

Source Notebook

Crop uniform values from the edges of an array

Contributed by: Wolfram Staff

ResourceFunction["ArrayCropPad"][array]

crops uniform values from the edges of the specified array.

ResourceFunction["ArrayCropPad"][array,m]

gives array with exactly m zeros of padding on each side.

ResourceFunction["ArrayCropPad"][array,m,form]

uses the specified form of padding.

Details and Options

ResourceFunction["ArrayCropPad"][array] currently works only on arrays that can be represented as Image[array].
In ResourceFunction["ArrayCropPad"][array,padding,form], the padding specification and the form specification are the same as in ArrayPad.
In ResourceFunction["ArrayCropPad"][array,padding,] the array is cropped before padding is added.

Examples

Basic Examples (3) 

Create a two-dimensional array and crop zeros at edges:

In[1]:=
array = CellularAutomaton[30, CenterArray[{1}, 20], 6]
Out[1]=
In[2]:=
ResourceFunction["ArrayCropPad"][array]
Out[2]=

Plot the original and the cropped arrays:

In[3]:=
GraphicsRow[ArrayPlot /@ {%%, %}]
Out[3]=

Add some "white space" around the cropped array:

In[4]:=
ArrayPlot[
 ResourceFunction["ArrayCropPad"][
  CellularAutomaton[30, CenterArray[{1}, 20], 6], 1]]
Out[4]=

Add the specified border value:

In[5]:=
ResourceFunction["ArrayCropPad"][
 CellularAutomaton[30, CenterArray[{1}, 20], 6], 1, .5]
Out[5]=
In[6]:=
ArrayPlot[%]
Out[6]=

Applications (2) 

Without ArrayCropPad, this leaves lots of "white space":

In[7]:=
ArrayPlot[Last /@ TuringMachine[2506, {{1, 15}, Table[0, 30]}, 30]]
Out[7]=

ArrayCropPad crops in on the structure of interest, leaving specified padding:

In[8]:=
ArrayPlot[
 ResourceFunction["ArrayCropPad"][
  Last /@ TuringMachine[2506, {{1, 15}, Table[0, 30]}, 30], 2]]
Out[8]=

Version History

  • 1.0.0 – 28 August 2020

Related Resources

Author Notes

The use of ImageCrop is a hack. For example, this will crop in y as well as x.

License Information