Hey, i’m simply trying to create an image and set every pixel to white. What am i doing wrong here?
PImage img;
void setup() {
img = createImage(500, 500, RGB);
size(1920, 1080);
}
void draw() {
img.loadPixels();
for (int x=0; x<500; x++) {
for (int y=0; y<500; y++) {
img.set(x,y,color(255));
}
}
image(img,0,0);
}
It looks the same here: https://processing.org/reference/PImage_set_.html
Only difference is that i dont want to edit a image file but create a new image instead.
thank you in advance!
1 Like
Invoke PImage::get() w/o any arguments in order to get a clone of it:
2 Likes
BtW, Arrays::fill():
Over the PImage::pixels[]:
Is much faster than any loop!
2 Likes
I don’t know why you are doing this, but if you want a more general interface to manipulating the pixels of an image, I might recommend PGraphics.
Now you can call beginDraw()
and then use Processing methods to do things on your PGraphics pg
. Want every pixel white? pg.background(255)
. Want to draw a line? pg.line(x,y,x2,y2)
. When you are done drawing, remember to call endDraw()
and then display your image with image(pg, x, y)
.
PGraphics pg;
void setup() {
size(1920, 1080);
pg = createGraphics(500, 500);
}
void draw() {
pg.beginDraw();
pg.background(255);
pg.endDraw();
image(pg, 0, 0);
}
1 Like
@Skranker When you modifiy your pixels, you need to call updatePixels()
. Also, as mentioned before, using the pixels array associated with the PImage instead of calling set()
img.pixels[y*img.width+x]=color(255);
Also in your for loops, instead of using 500, I encourage you to use image.width
and img.height
in their respective loops. Good habit to start working on.
Kf
1 Like