We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
First idea to debug:
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.