We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I'm trying to pixelate an image, so that when the mouseX moves from 0 towards right, the image gets more and more pixelated. I've tried to do this using the map() function and I don't understand what's wrong with my code. At the moment it only shows a grey window. Also, would it be possible to get the pixel effect on the whole sketch (with multiple videos, images etc.), not just one image? Here's my code, would be really grateful for your help!
// Image variable
PImage img;
void setup() {
size(500, 500);
// Load the file from the data folder
img = loadImage("sunflower.jpg");
}
void draw() {
// Call loadPixels before we deal with pixels
loadPixels();
float a = map(mouseX,0,width,0,10);
// Loop through all pixels, skip every 10 pixels along the y-axis
for (int y = 0; y < img.height; y+=a) {
// skip every 5 pixels along the x-axis
for (int x = 0; x < img.width; x+=a) {
// Calculate the 1D location from a 2D grid
int loc = x + y*img.width;
// Color according to the image
noStroke();
fill(img.pixels[loc]);
rect(x,y, 10,10);
}
}
}
Answers
big error in line 14
img. ....
Yes, you can get it for videos as well. In regards to your previous code, you need to call
image(img,0,0);
and since you are modifying your pixel array, you need to callimg.updatePixels();
after you are done modifying your pixels.Lastly, line 26 in this case is preferred to be called in setup.
Kf
(And
noStroke()
can be put in setup instead of inside the nested loop in draw)Thank you for your help! I tried again with your suggestions, but I still get a grey image. The problem seems to be with the "a" variable as my original code does work when I replace the variable with an integer. However it does not work with line 16 (float a=...) and variable "a" embedded in code (lines 19 & 21). Any ideas on this?
Line 16 should be
float a = map(mouseX,0,width,1,10);
avoid having 0 as you can end up in infinite loops.Also line 28 should be
rect(x,y,a,a);
and to make things better in this case, callrectMode(CENTER)
in your setup() function.Please also show your latest code reflecting the implemented suggested changes from previous posts.
Kf
This sounds like it could be done in a shader.
pixelate.frag needs to be in data folder
Possibly using filter()
but I couldn't get it to work,EDIT: got it working now
@prince_polka
Really cool. BTW, you missed a final curly bracket in your frag code.
Kf
[deleted] nothing important, see post below
@hello_processing
I made an example with just an ellipse following the mouse and getting pixlier closer to the right edge of screen. Will probably work with images, text and video as well, haven't tested though.
@cansik
I'we just "figured out" how to use filter ;)
It seems filter works opposite of shader() in three ways.
It seems if you set() a Pimage as a sampler you shouldn't call it texture, should flip it upside down, and call shader() before you draw.
And with filter() it's the opposite, you should call the sampler texture, shouldn't flip the screen, and call filter() after you'we drawn.
This pixelate effect isn't super fancy but maybe something to consider adding to your PostFX library, if you're still working on it.
**Sketch **
pixelate.glsl
Here is a concise explanation of this contrast:
filter()
is a post-processing method, likeblend()
-- it acts on Processing's built-inpixels[]
array. Filter after data is added to the pixels array (e.g. after callingimage
). Everything uses the normal Processing coordinate system (0,0 in the upper left).shader()
is a configuration method, liketint()
-- it changes Processing's rendering pipeline. Shade before you draw, changing how subsequent drawing operations will affect the canvas. Shaders are written in OpenGL Shader Language (GLSL), which uses the OpenGL coordinate system (0,0 in the lower left) -- hence the need to vertically "flip" coordinates when interacting with GLSL.@jeremydouglass That explains the performance increase when using
filter()
. However, to clarify, both methods (technically, only in P2D/P3D) make use of the GPU.Thank you so much for your help everyone!
@prince_polka I tried to add your shader to my library, but I can not compile the shader. You are using glsl commands which are not support by MacOS at the moment (only GLSL 120 is supported I think).
ERROR: 0:24: '%' does not operate on 'ivec2' and 'int' ERROR: 0:27: Invalid call of undeclared identifier 'texelFetch' ERROR: 0:28: Invalid call of undeclared identifier 'texelFetch' ERROR: 0:29: Invalid call of undeclared identifier 'texelFetch' ERROR: 0:30: Invalid call of undeclared identifier 'texelFetch'
I'm going to try to rewrite the shader to work everywhere :)
Added a new
imod
function and implemented my own texelFetch method, but it is not working as expected. There is no pixelate effect on the image.Full code here: old shader/pixelateFrag.glsl
So I've implemented it with a simpler version, but I don't know if it has the same result as your code. My code can be found here:
new shader/pixelateFrag.glsl
@cansik

I don't use mac and didn-t know you couldnt use % or texelFetch,
With your code I get dots and line artifacts