Randomly Generated Pixels

Discussion of Automation, Image Workflow and Raw Image Workflow

Moderators: Tom, Kukurykus

Squis

Randomly Generated Pixels

Post by Squis »

Hi!

I want to create a script that creates random (or near random) values for every single pixel of a document, similar to the "Add Noise..." filter, but with more control, such as "only b/w", "only grey", "all RGB" and "all RGB with alpha" and maybe even control over the probability distribution. Any idea how this could be tackled? Selecting every single pixel and applying a random color seems like something that would take a script hours...

Why do I need this?
I've started creating some filters in Pixel Bender (http://en.wikipedia.org/wiki/Adobe_Pixel_Bender). Since Pixel Bender doesn't really have any random generator (and workarounds are limited) I'm planning on passing on the random numbers through random pixel values. I'm well aware that this can only be used for filters in which Pixel Bender creates images from scratch, but that's the plan.

Thanks!
txuku

Randomly Generated Pixels

Post by txuku »

Hello Squis

Maybe something like this - with a dialog box :
Code: Select alldocRef = activeDocument;

//docRef.changeMode(ChangeMode.GRAYSCALE);  // only grey

//docRef.channels[1].duplicate();          // Copy Green channel

var docH = activeDocument.width;
var docV = activeDocument.height;

for(j=0;j<docH ; j++ )
{
  for(i=0;i< docV ;i++ )
  {
      // selextion 1 px
    TOP = i
    LEFT= j
    BOTTOM = TOP + 1;
    RIGHT = LEFT + 1
    var selRef = [
                  [LEFT,TOP],
                  [LEFT,BOTTOM],
                  [RIGHT,BOTTOM],
                  [RIGHT,TOP]
                  ]

    docRef.selection.select( selRef);
    //rempliCoul();                     // RGB
    rempliCoulBW();                     // B&W
  }
}

function rempliCoul()
{
    var Colour = new SolidColor;
    Colour.rgb.red = Math.ceil(255*Math.random());
    Colour.rgb.green = Math.ceil(255*Math.random());
    Colour.rgb.blue = Math.ceil(255*Math.random());
    docRef.selection.fill(Colour);
}   

function rempliCoulBW()
{
    var Colour = new SolidColor;
    Colour.rgb.red = Math.ceil(255*Math.random());
    Colour.rgb.green = Colour.rgb.red;
    Colour.rgb.blue = Colour.rgb.red;
    docRef.selection.fill(Colour);
}   

But it will be very very long if the size of the image is large !
Squis

Randomly Generated Pixels

Post by Squis »

Wow! Thanks for the code! I've been trying to keep away from single pixel editing, since it would take quite long. But I should just go ahead and try it to see how long it really takes.
txuku

Randomly Generated Pixels

Post by txuku »

I've tried 100x100: manageable!

3504x2336 .... hard !
jacobolus

Randomly Generated Pixels

Post by jacobolus »

My recommendation would be to use the add noise filter, and combine it with other tools to change the output. For example, you can change the probability distribution by applying curves to the uniform noise or making two noise layers and combining them with adjustable blend mode and transparency. For black/white make grayscale noise and then use the Threshhold adjustment. You can make the noise apply to particular channels by unchecking the channel visibility of the others in the Advanced Blending options. You can apply noise to the alpha by way of a layer mask. And so forth.

If that doesn't work, then my recommendation would be to create your noise in an external program. If I was doing it, I would use Python and Numpy, in which creating whatever kind of noise you want is trivial.