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

2021/12/28

How to draw a colorfull image after converting it into grayscale image?

Roshan123 Roshan123

2021/12/28

#
The following code is to convert a pic into grayscale image
GreenfootImage img=dan.getImage();
for(int i=0; i<=img.getWidth(); i++)
{
  for(int j=0; j<=img.getHeight(); j++)
  {
     Color col=img.getColorAt(i,j);
     int r=col.getRed();
     int g=col.getGreen();
     int b=col.getBlue();
     int avg=(r+g+b)/3;
     img setColorAt(i, j, new Color(avg, avg, avg);
  }
  dan.setImage(img);
}
So how am I supposed to draw back a normal image after turning it into grayscale image? I used a way in which it will save the normal image before converting it into grayscale image....and after convertion, i press a button which will set back the previous normal image which was saved earlier. But due to some reason I don't want to use this method....i only want to draw back a normal image
danpost danpost

2021/12/28

#
Roshan123 wrote...
The following code is to convert a pic into grayscale image << Code Omitted >> So how am I supposed to draw back a normal image after turning it into grayscale image? I used a way in which it will save the normal image before converting it into grayscale image....and after convertion, i press a button which will set back the previous normal image which was saved earlier. But due to some reason I don't want to use this method....i only want to draw back a normal image
You cannot with any accuracy put color back into a gray-scaled image. Look at line 10 of the code given to gray-scale the image. There is no one-to-one correspondence between the average and the 3 values that make it up. That is, there are multiple ways to come up with the same average value and there is no way to determine which set of r, g and b made up the average, except, of course, for the average of 255. Even a zero average may come from 10 possible sets of values for r, g and b: (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0) and (2, 0, 0)
Roshan123 Roshan123

2021/12/28

#
So is their any easy way to solve the problem?
danpost danpost

2021/12/28

#
Roshan123 wrote...
So is their any easy way to solve the problem?
Nothing easy. And anything that would be half usable would limit the pallet of colors used in the re-colored image.
You need to login to post a reply.