Wednesday 29 June 2011

Remove background and make it transparent

/*
removes a black background from the vase picture and replaces it with translucent pixels, so the image behind the vase is revealed
*/
PImage img, img3, destination;


void setup() {
size(300, 300);
img = loadImage("vase.jpg");
destination = createImage(img.width, img.height, RGB);


img3 = loadImage("star.jpg");
}

float threshold = 12;
void draw() {
//noLoop();
background(255);

img.loadPixels();
destination.loadPixels();
color cc =color(255, 255, 255, 0); //with transluscent alpha channel
//could just write 255, 0
color cc2 =color(0, 0, 255);

for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++ ) {
int loc = x + y*img.width;

if (brightness(img.pixels[loc])>=threshold) {
destination.pixels[loc] = img.pixels[loc];// the vase , could be a color fill too i.e =cc2;
}
else if(brightness(img.pixels[loc])<=threshold) {

destination.pixels[loc] = cc;
}
}
}
destination.format = ARGB; //this is the crucial line
destination.updatePixels();


image(img3, -20, -20);
image(destination,0,0);
}

1 comment:

  1. """Background removal easy, cheap and fast! Say bye to the Background on your Images say hello to higher conversion rates - www.Byeground.com the leading background remover offers affordable services such as: photoshop masking, keep clipping path, background removal, knock out mask and image cut out that's fast and affordable within 24 hours - best of all prices starts at $0.85 per image at Byeground.com."
    www.byeground.com

    ReplyDelete