This site requires JavaScript, please enable it in your browser!
Greenfoot back
seikan
seikan wrote ...

2013/12/7

Requesting help for changing color of a series of pixels please!

1
2
seikan seikan

2013/12/7

#
Hi, I'm in an intro to computer science class and am struggling to figure out how to select a series of pixels from an image that has thousands of pixels, and subsequently change their color. I am using a while loop, and am assigning the variable to the increment. I think I'm doing a few things wrong. So far my failed attempt look like this- public void changeColor() { { int getPixel=1000; while ( getPixel<=1110) { Pixel myPix=getPixel(1000); myPix.setColor(0, 0, 0); } getPixel=getPixel++; } } Would any kind soul mind offering me a few hints or how to select big groups of pixels using while loops? I think I'm creating an infinite loop on accident, or incrementing the wrong variable. I've had success in previous project but this one is stumping me because there are hundreds of thousands of pixels. I know I can't create variable for them all, so I need to group them with loops. I'm pretty new at this. Any help or guidance to understand the mistakes or big picture would be greatly appreciated. Thanks so much and best wishes, John
seikan seikan

2013/12/8

#
So I figured out part of what I was doing wrong, can't believe I was making this mistake. I needed to assign the counter variable to the parameter of getPixel within the while loop. I was just working on it too long to realize the silly mistake. And I was incrementing in the wrong place to where it was stuck in an infinite loop. But I still can't seem to change colors of sets of pixels. I also incremented wrong, bah. It was late yesterday and i was just overlooking simple mistakes. Could anyone give me a further hint or correction on the code I fixed below? public void changeColor() { int i=1000; getPixel=getPixel+1; while ( i<=1110) { Pixel myPix=getPixel(i); myPix.setColor(0, 0, 0); } }
danpost danpost

2013/12/8

#
Your first coding attempt looks better than the second, but the increment of the variable needs to be within the loop (not outside of it). The second attempt was slightly improved because of the fact that you used the variable instead of the hard-coded value of '1000' within the loop to get the pixel.
seikan seikan

2013/12/8

#
Thanks very much for your fast reply and the helpful information. Will implement that and get this working and understand it better.
seikan seikan

2013/12/8

#
Say I have a method getPixels() that returns all the pixels in a picture that has several hundred thousand pixels, how would I isolate a 200x200 square in the top left? Just figure out where they lie with trail and error then call them with loops? I also have getWidth() and getHeight() that return the respective world dimensions which I think are the same as the pixel number so I can use those to calculate the total number of pixels in the image. Will I be able to create a loop that includes half the pixels without getting a memory error/ needing to debug? I hope it's not too broad of a question, but what question should I ask myself or think about to figure out how to get chunks of the picture, invert colors, or flip the image some? edit: I can choose pixels using getPixel(int index position) or getPixel(x, y) so I'm using the index position, for the 200x200 square I'm going to try using getPixel=0 and increment it up to a certain number and see what that returns to figure out how many index position yield a 200x200 area.
danpost danpost

2013/12/8

#
From what I understand about what you want to do is this: you want to isolate a 200x200 pixel area from the background of the world. However, I am unsure about whether you are trying to isolate a specific area or a randomly selected area. Please, correct me if I am wrong and clarify. Thanks.
seikan seikan

2013/12/8

#
Ah, yes thanks for pointing that out. I want to isolate the 200x200 pixel area in a specific area in the very top left corner of the world. I'm trying to manipulate an existing image. Just a picture of a snowy backyard. I just tried this, and it gave me a skinny diagonal line. So what I'm seeing from the example below is I should stick with the index position integer rather than the x,y position. And maybe set an if statement within to make it not select all the pixels across the x axis and restrict it to the corner. Trying trial and error to figure this out... /** * This method will create a solid colored 200X200 square in the top left hand corner of the world. */ public void topLeftSquare() { { int x=1; int y=1; while(x<=200 && y <=200) { Pixel sqPix=getPixel(x,y); sqPix.setColor(Color.YELLOW); x++; y++; } }
danpost danpost

2013/12/8

#
Are you required to use 'while' loops? The reason I ask is because you could just use the 'drawImage' method of the background image of the world:
GreenfootImage snowyImage = new GreenfootImage(/* image filename */);
getBackground().drawImage(snowyImage, 0, 0);
This is presuming that the size of the 'snowy backyard' image is 200x200.
seikan seikan

2013/12/8

#
The dimensions of the whole image are 635x385. So around 240,000 pixels. No I'm not required to use while loops for everything, no. It's just kind of frustrating. We are required to change the RGB color value of each existing pixel in the photo by using 4 methods we create to manipulate the pixel coloration. One method is supposed to increase the red value of all pixels by 30%. The next method should create a 200x200 solid colored pixel square in the top left. And the last two methods are up to us to be creative, so I thought flipping half the image, or inverting the colors might be easy. I'm just at a loss on how to understand or limit which pixels I'm selecting using the index position thing. I think I may be overcomplicating it but I'm just struggling to realize my logical errors I think.
danpost danpost

2013/12/8

#
Alright, to iterate through the pixels of an image, best would be to use a double 'for' loop. One to iterate across and the other to iterate down. Limit them to 'getWidth' and 'getHeight' (the dimensions of the world background. You could do the same with limits of '200' and '200' to do the top-left corner thing afterwards. Another way to do the top-left corner thing is to create a new GreenfootImage of size 200x200 and 'fill' it will any color using 'setColor' and 'fill' on the image and then draw that image onto the image of the background as I had coded above.
MatheMagician MatheMagician

2013/12/8

#
Your code is mainly good, except that it only alters 200 pixels:( Think of it this way: every while loop, you are adding one to x and one to y, so at first: x = 0, y = 0, then x = 1, y = 1, then x = 2, y = 2, etc. What you have to do is split your while loop in two Like this:
/**
     * This method will create a solid colored 200X200 square in the top left hand corner of the world.
     */
    public void topLeftSquare()
    {
        {
         int x=1;
         int y=1;
while(x<=200)
{
        x++;
        while(y<=200)
        {
             Pixel sqPix=getPixel(x,y);
             sqPix.setColor(Color.YELLOW);
             y++;
            
        }
}
    }
seikan seikan

2013/12/8

#
Thanks so much, that's very helpful. I appreciate your help and time. What would a small exmaple of the double 'for' loop look like for iterating down and across? I made the method for getWidth and getHeight but I don't think I know how to connect them to a variable or limit them. I think I'm getting closer to understanding this.
danpost danpost

2013/12/8

#
seikan wrote...
I made the method for getWidth and getHeight...
You do not have to create these methods; they are already supplied from within the World class. The double 'for' would be like this:
for (int h=0; h<getHeight(); h++)
{
    for (int w=0; w<getWidth(); w++)
    {
        // use 'setColorAt' to change pixel using 'h' and 'w'
    }
}
seikan seikan

2013/12/8

#
Ah ok MatheMagician, thank you so much for illustrating that! I see I do get a different result from your code, it gives me a colored skinny vertical line on the leftmost corner. How do I select a square area of pixels instead of just a line? Like a 200x200 box out of a 635x385 image.
seikan seikan

2013/12/8

#
danpost wrote...
seikan wrote...
I made the method for getWidth and getHeight...
You do not have to create these methods; they are already supplied from within the World class. The double 'for' would be like this:
for (int h=0; h<getHeight(); h++)
{
    for (int w=0; w<getWidth(); w++)
    {
        // use 'setColorAt' to change pixel using 'h' and 'w'
    }
}
Oh I'm sorry, you're right. I conveyed what I meant incorrectly. I meant I was attempting to make a method using those two pre existing methods that would give me the total pixels in the world but I know I'm not doing it right. I think I need to multiply the two, do I set them to a variable and then multiply that? They are supposed to return an int but I don't see how to get it when I call the methods individually. /** * This method will return an integer that represents the number of pixels in the world. */ public void getPixelCount() { theWorld.getHeight(); theWorld.getWidth(); }
There are more replies on the next page.
1
2