Does anybody have a clue about this very weird color behavior

edited November 2016 in Questions about Code

Hello, so I have noticed a very weird behavior of colors, which will be better explained visually. First a small introduction to what I want to do : first, open a file, which is a 9x9 pixels JPEG image. Then I want to apply the color of each pixel in the first line to all following vertical pixels. Very simple indeed, there's how I did by code :

PImage loadedImgage;
String selectedPath = "";

void fileSelected(File selection) {
  selectedPath = selection.getAbsolutePath();
  loadedImgage = loadImage(selectedPath);
}

String getFileName(String completeFileName) {
  String[] fileNameParts = split(completeFileName, ".");
  fileNameParts = shorten(fileNameParts);
  return join(fileNameParts, ".");
}

void keyPressed() {
  if(key == 's') {
    save(getFileName(selectedPath) + "__.jpg");
    exit();
  }
}

void settings() {
  selectInput("Select a file to process:", "fileSelected");
  while (loadedImgage == null)  delay(100);
  size(loadedImgage.width, loadedImgage.height); 
}

void draw() {
  background(loadedImgage);

  for(int i = 0; i < loadedImgage.width; i++) {
    color imageColor = get(i, 0);

    noStroke();
    fill(imageColor);
    rect(i, 0, 1, 9);
  }

  noLoop();
}

Here are the visuals :

So as you can see first column of the modified file should have the same color as the first pixel of the original file (255;242;6), but strangely it doesn't, it's even a totally new color (255;225;75) which doesn't exist in any of the existing pixels !

So do any of you know how is this possible? Thanx a lot for your help

Answers

  • edited November 2016 Answer ✓

    First idea to debug:

    • Try loading from PNG and saving to JPEG.
    • Try loading from JPEG and saving to PNG.
    • Try loading from PNG and saving to PNG (recommended)!

    JPEGs are lossy compressed, and they are encoded with wavelets (cosine wave). On complex patches of close-together colors, saving and recovering specific values is unreliable and imprecise, depending on tiny details of how the waves (which represent components of multiple pixels) are stored or restored into a pixel grid. If you really are trying to save precise per-pixel data in 9x9 grids, and you want true color preservation, JPEG is a horrible format for doing that. Use PNG instead.

    Addendum: in additional to the problem that JPEG is unreliable in general, it is specifically unreliable on images such as yours which are smaller than a JPEG MCU block (Minimum Coded Unit) -- which is normally 16x16px. But, in general, just don't use JPEG if you need precise per-pixel values.

  • Hello Jeremy, thanx for your answer, it makes sense indeed, I'll try to do that, I think JPEG is probably the reason...

  • Hi back, loaded image as .jpg, and saved it as .tif. Everything works fine now, thank you very much, cheers!

  • @Matei -- glad to hear! -- good luck with the project.

Sign In or Register to comment.