We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Say for example I did pic = loadImage("pic.jpg") and I did pic2 = createImage(width,height,RGB). Would I be able to store pixels of pic into pic2?
If I found the right indices could I just do pic2.pixels[location] = color(r , g ,b ), where r g and b are found from the first image like: r = red(pic.pixels[location])?
thank you
Answers
If 2 PImage objects got the same width & height dimensions, a simple arrayCopy() would suffice:
arrayCopy(pic1.pixels, pic2.pixels);
: https://Processing.org/reference/arrayCopy_.htmlAn example using arrayCopy(): https://forum.Processing.org/two/discussion/14835/how-to-change-main-sketch-display#latest
If they're different dimensions, you're gonna need copy() instead:
https://Processing.org/reference/PImage_copy_.html
An example using copy(): https://forum.Processing.org/two/discussion/comment/61996/#Comment_61996
Would arrayCopy() or copy be the only way to do it? I'm not sure if I am allowed to use it unless that is the only way. Would using a loop and doing as I described above work? I tried it, but it seems to not be working. I'm not sure if it is just not possible or I have a bug somewhere.
Both arrayCopy() & copy() are for full copy transfer.
If you need to modify the transferred values, you're gonna need a regular loop.
If you've got an attempt, why haven't you posted it already?
Don't forget to format it for the forum though, please: https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
I was thinking:
Ir seems you're not altering the values. Therefore arrayCopy() or copy() are the best options.
And don't forget to learn how to post formatted code in this forum:
https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
I can't seem to get the tabs correct for the formatted code.
Just highlight posted code and hit CTRL+O.
if I have an image, if i want to loop through its pixels would it be like this:
Slight mistake in line 4
You can make it clearer by using different variable names
Thanks quark! So the variable that is used to compare against the height will always be the one that gets multiplied to the width? Do you think you can explain the logic behind this to me. Thanks!
Assume you have an image 8 pixels wide and 4 high. That is 32 pixels so the pixel array elements go from [0] to [31] and their position in the image is like this
Now if we take the image pixel at [x=3, y=2] the position in the pixel array is 19 which is calculated by 2 * 8 + 3 in other words
y * image_width + x
BTW you can reverse the process.
Given an index position in the pixel array
idx
you can calculate the pixel coordinates withwhere
%
is the modulo operator i.e. gives the remainder after a division.Just an addendum: For the
/
operator to work correctly, neither of its operands can be of any fractional type, such asfloat
&double
for example. :-SSThat's especially true for JavaScript for example, given the fact its
number
type is fractional.Therefore, we need to truncate the result by using
| 0
:y = idx / image_width | 0;