Modify Image Algorithmically
function ensureInImage(coordinate, size){
//coordinate cannot be negative
if (coordinate < 0){
return 0;
}
//coordinate size must be between [0....size-1]
if (coordinate >= size){
return size-1;
}
else{
return coordinate;
}
}
function getPixelNearby(image , x, y, diameter){
var dx = Math.random() * diameter - diameter/2;
var dy = Math.random() * diameter - diameter/2;
var nx = ensureInImage(x + dx, image.getWidth());
var ny = ensureInImage(y + dy, image.getHeight());
return image.getPixel(nx, ny);
}
//Select An Image from the list
var image = new SimpleImage ("chapel.png");
//I'm gonna create a new image that is the same size as my starting image.
var output = new SimpleImage(image.getWidth() , image.getHeight() );
//for all the values of pixel of the image the values of x and y are taken and math.random is done since we dont want the new pixel more than the radius of some walue we enter 10 or any random number depending uopn the number we enter the blurrier the image
for (var pixel of image.values()){
var x = pixel.getX();
var y = pixel.getY();
if (Math.random() > 0.5) {
//we call the getPixelNearby(image , x, y, diameter) which inturn calls the ensureInImage(coordinate, size)
var other = getPixelNearby(image , x, y, 10);
output.setPixel(x, y, other);
}
else{
output.setPixel(x, y, pixel);
}
}
print (output);
print (image);
Comments
Post a Comment