-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddImageBorder.js
34 lines (31 loc) · 1.09 KB
/
addImageBorder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function setBlack(pixel) {
pixel.setRed(0);
pixel.setBlue(0);
pixel.setGreen(0);
return pixel;
}
function addBorder(image,thickness) {
var newWidth = image.getWidth()+2*thickness;
var newHeight = image.getHeight()+2*thickness;
var resImg = new SimpleImage(newWidth,newHeight);
for(var pix of resImg.values()){
if (pix.getX() <= thickness || pix.getX() > (newWidth-thickness)) {
pix = setBlack(pix);
}
if (pix.getY() <= thickness || pix.getY ()> (newHeight-thickness)) {
pix = setBlack(pix);
}
if (pix.getX() > thickness && pix.getX() < (newWidth-thickness) && pix.getY() > thickness && pix.getY() < (newHeight-thickness)){
var x = pix.getX() - thickness;
var y = pix.getY() - thickness;
resImg.setPixel(pix.getX(),pix.getY(),image.getPixel(x,y));
}
}
return resImg;
}
var img = new SimpleImage("smallpanda.png");
var newW = img.getWidth()+20;
var newH = img.getHeight()+20;
var newImg = new SimpleImage(newW,newH);
newImg = addBorder(img,10);
print(newImg);