Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Blurring a picture
- var inputImage = new SimpleImage("duvall.jpg");
- var outputImage = new SimpleImage(inputImage.getWidth(), inputImage.getHeight());
- for(var pixel of inputImage.values()){
- var x = pixel.getX();
- var y = pixel.getY();
- if(Math.random() > 0.5){
- //Changing half of the pixels only to blur the given picture.
- var nearbyPixel = getNearbyPixel(inputImage, x, y, 10);
- outputImage.setPixel(x, y, nearbyPixel);
- }else{
- outputImage.setPixel(x, y, pixel);
- }
- }
- print(outputImage);
- function getNearbyPixel(inputImage, x, y, diameter){
- //Generating a random number between 0 and 10 (diameter = 10 in this case) but < 10
- var dx = Math.random() * diameter;
- //To cover both the sides we want to shift half of the window to the negative side
- //so we subtract half of diameter. So we get a number between -5 and +5 instead of between 0 and 9.9
- dx = dx - diameter/2;
- var dy = Math.random() * diameter;
- dy = dy - diameter/2;
- //Generating the new nearyby pixel
- nearbyX = ensureInsideImage(x + dx, inputImage.getWidth());
- nearbyY = ensureInsideImage(y + dy, inputImage.getHeight());
- return inputImage.getPixel(nearbyX, nearbyY);
- }
- function ensureInsideImage(coordinate, size){
- //coordinate can not be negative
- if(coordinate < 0){
- return 0;
- }
- //coordinate must be in the range [0....size-1]
- if(coordinate >= size){
- return size - 1;
- }
- return coordinate;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement