trying to get() jpg image pixels, but only getting -1

Here is my code:

    PImage img = get(101, 11, 29, 39);

    image(img, 150, 0);
    image(ace, 200, 0);

    println(img.pixels[0]);
    println(ace.pixels[0]);
    println(get(151,1));
    println(get(200,1));

'ace' is a jpg image. However I try to get any pixel's value from the 'ace' image, it returns -1. 'img' returns the correct value of the pixel. Any ideas? Something with loadPixels() maybe?

Thanks, ~Mikeshiny

Tagged:

Answers

  • Yes, you need to load the pixels (and possibly update) before you can reliably do anything with them. Also, I don't see in your code where you declare ace as a PImage.

  • I just have the segment in question. I declare the image and everything else properly. I have never used loadPixels() before. Can you give me a little bit more of an idea where to put it? I will try myself to see if that is the problem

  • Hi, some thing like this, can't explain why (please someone) but I didn't need to loadPIxels(); Any way you just call it before you need the array containing the pixels. You can call to the window displayed loadPixels() or to an image image.loadPixels().

    PImage i ;

    void setup() {
      i =loadImage("http://www.wired.com/wiredscience/wp-content/gallery/vulcans-view-14/sakurajima_oli_2013231.jpg");
      size(i.width, i.height); 
      image(i, 0, 0);
      noLoop();
    }
    
    void draw() {
    }
    
    void mouseMoved() {
      int noGoodColorData = get(mouseX, mouseY);
      int loc = mouseY*width+mouseX;
      int sameUsingPixels = i.pixels[loc];
      int r = i.pixels[loc]  >> 16 & 0xFF; //faster red(color)
      int g = i.pixels[loc]  >> 8 & 0xFF;  //faster green(color)
      int b = i.pixels[loc]  & 0xFF;  //faster green(color)
      println(noGoodColorData + " = " + sameUsingPixels + " and the components r,g,b  " + r + " ," + g + " ,"+ b +" :)" );
    }
    
  • _vk_vk
    edited October 2013 Answer ✓

    I guess PImage should be a array of pixels under the hood, so probably when we loadImage() we load it's array. With the display window, you need to use it before trying to access it. See below, we can read "above" or "below" the wgite square. Also the -1 is returned instead of the big int in the format ARGB -> AAAARRRRGGGGBBBB so it makes no sense as an int... see this link for a better understanding.

    http://wiki.processing.org/w/What_is_a_color_in_Processing%3F

    and the code modified:

     PImage i ;
    
    void setup() {
      i =loadImage("http://www.wired.com/wiredscience/wp-content/gallery/vulcans-view-14/sakurajima_oli_2013231.jpg");
      size(i.width, i.height);
    
      image(i, 0, 0);
      rect(width/2, height/2, 100, 100);
      noLoop();
    }
    
    void draw() {
    }
    
    void mouseMoved() {
    
      // accessing image direct
      int noGoodColorData = i.get(mouseX, mouseY);
      int loc = mouseY*width+mouseX;
      int sameUsingPixels = i.pixels[loc];
      int r = i.pixels[loc]  >> 16 & 0xFF; //faster red(color)
      int g = i.pixels[loc]  >> 8 & 0xFF;  //faster green(color)
      int b = i.pixels[loc]  & 0xFF;  //faster green(color)
    
      println(noGoodColorData + " = " + sameUsingPixels + " and the components r,g,b  " + r + " ," + g + " ,"+ b );
    
      // accessing display window (over rect)
      int noGoodColorData2 = get(mouseX, mouseY);
    
      //needed
      loadPixels();
      int sameUsingPixels2 = pixels[loc];
      int r2 = pixels[loc]  >> 16 & 0xFF; //faster red(color)
      int g2 = pixels[loc]  >> 8 & 0xFF;  //faster green(color)
      int b2 = pixels[loc]  & 0xFF;  //faster green(color)
      println(hex(noGoodColorData2));//<--- see link above
      println(noGoodColorData2 + " = " + sameUsingPixels2 + " and the components r,g,b  " + r2 + " ," + g2 + " ,"+ b2 +" <- display" );
    }
    
  • Upon further investigation into this, my problem is actually somewhere else. The -1 I was getting was correct.

    My problem is that I am copying pixels from one place to another while enlarging the image using the copy() function. The copy function somehow changes the value you get from the get() function. I guess because I am enlarging the image. Here is code that shows my problem:

    void setup()
    {
      size(200, 200);
    }
    
    void draw()
    {
      background(255);
      println(get(100, 100));
      copy(1, 1, 16, 31, 100, 100, 45, 90);
      println(get(100,100));
    }
    

    When I use the copy function but do not enlarge the image at the same time, the correct value comes out. Thanks for the help everyone!

  • Only the alpha part was reset. The RGB part is still FF FF FF: (~~)

    // forum.processing.org/two/discussion/803/
    // trying-to-get-jpg-image-pixels-but-only-getting-1
    
    void setup() {
      size(200, 200);
      noLoop();
    }
    
    void draw() {
      background(-1);
      println( hex(get(100, 100), 8) );
    
      copy(1, 1, 16, 31, 100, 100, 45, 90);
      println( hex(get(100, 100), 8) );
    }
    
Sign In or Register to comment.