随机删除部分图像/像素

时间:2014-11-07 01:09:15

标签: photoshop

我想知道是否有人知道如何随机删除/删除图像的某些部分。是否有某种过滤器,我可以指定我想要删除的图片的百分比? (我在谷歌搜索方面表现不佳。)

1 个答案:

答案 0 :(得分:0)

你可以这样: 效率不高!因为它循环选择然后删除那些像素。一遍又一遍。

我用1 MB图像尝试了它,50%删除的设置花了几分钟。所以你被警告了。

// call the source document
var srcDoc = app.activeDocument;

// Get original width and height
var imageWidth = srcDoc.width.value;
var imageHeight = srcDoc.height.value;

// Selection size for deleting pixels
// You could make these random
var sizeX = 20;
var sizeY = 20;

// Define the number of parts you want to delete
// Area of image = imageWidth * imageHeight
// Example image 1600 x 1200, area = 1920000 pixels
// To delete 50% would be the same as selecting
// an area that's (sizeX * sizeY) *2400 times

var pcent = 0.1; //10% of image

var area = imageWidth * imageHeight;
var selArea = sizeX * sizeY; //selection area
var numParts = pcent * (area/ selArea);
numParts = parseInt(numParts);


for (var i = 0; i < numParts; i++)
{

  var randX = Math.floor(Math.random() * (imageWidth-sizeX));
  var randY = Math.floor(Math.random() * (imageHeight-sizeY));
  // deselect EVERYTHING first
  srcDoc.selection.deselect();

  // select random position of selection
  selectRectangle(randY, randX, randY+sizeY, randX+sizeX);

  // Delete those pixels
  srcDoc.selection.clear();

  // deselect EVERYTHING first
  srcDoc.selection.deselect();
}

// function SELECT RECTANGLE(top, left, bottom, right)
//
// Note: co-ordinates are same as script listener
// and not so human-friendly as t,l,r,b.
// --------------------------------------------------------
function selectRectangle(top, left, bottom, right)
{
  // =======================================================
  var id1 = charIDToTypeID( "setd" );
  var desc1 = new ActionDescriptor();
  var id2 = charIDToTypeID( "null" );
  var ref1 = new ActionReference();
  var id3 = charIDToTypeID( "Chnl" );
  var id4 = charIDToTypeID( "fsel" );
  ref1.putProperty( id3, id4 );
  desc1.putReference( id2, ref1 );
  var id5 = charIDToTypeID( "T   " );
  var desc2 = new ActionDescriptor();
  var id6 = charIDToTypeID( "Top " );
  var id7 = charIDToTypeID( "#Pxl" );
  desc2.putUnitDouble( id6, id7, top );
  var id8 = charIDToTypeID( "Left" );
  var id9 = charIDToTypeID( "#Pxl" );
  desc2.putUnitDouble( id8, id9, left );
  var id10 = charIDToTypeID( "Btom" );
  var id11 = charIDToTypeID( "#Pxl" );
  desc2.putUnitDouble( id10, id11, bottom );
  var id12 = charIDToTypeID( "Rght" );
  var id13 = charIDToTypeID( "#Pxl" );
  desc2.putUnitDouble( id12, id13, right );
  var id16 = charIDToTypeID( "Rctn" );
  desc1.putObject( id5, id16, desc2 );

   executeAction( id1, desc1, DialogModes.NO );

}