Andy Warhol emulator


Andy Warhol was an American pop art artist (see Andy Warhol on Wikipedia ) and produced his typical kind of modern art images. Recently the idea came to me to create an image similar to an Andy Warhol image by a small C# application and soon I found my cute little Fiat cinquecento as a grateful object for such an image :-)



Warhol


With this picture I started to play.

First all the background had to be removed. This could be done in a regular paint shop program and looks like:


Warhol



Now the programming took place.

I converted the entire image into a grayscale image.


Warhol



In my C# application that’s a small function:



private Bitmap GrayScale(Bitmap bmpSource)
{
     int x, y;
     double R, G, B;
     Bitmap bmpDest = new Bitmap(pictureBox2.Image);
 
     for (x = 1; x < bmpSource.Width - 1; x++)
     {
         for (y = 1; y < bmpSource.Height - 1; y++)
         {
              Color pix1 = bmpSource.GetPixel(x, y);
              double grayscale = (pix1.R + pix1.G + pix1.B) / 3.0;
 
              R = grayscale;
              G = grayscale;
              B = grayscale;
 
              if (R < 0)
                   R = 0;
              else
              {
                   if (R > 255)
                        R = 255;
              }
              if (G < 0)
                   G = 0;
              else
              {
                   if (G > 255)
                        G = 255;
              }
              if (B < 0)
                   B = 0;
              else
              {
                   if (B > 255)
                        B = 255;
              }
 
              // set the corresponding pixel in the destination bitmap
              Color nPix = Color.FromArgb((int)(R), (int)(G), (int)(B));
              bmpDest.SetPixel(x, y, nPix);
         }
     }
     return bmpDest; 
}





It takes the mean of all 3 RGB colours of one pixel and puts this into the RGB values the same pixel. This is done in a loop that runs over all pixels of the image.

From there I replaced all the pixels brighter than a certain value by one color and all the other pixels by another color.

This is done in the following function:



private Bitmap ProcessImage(Bitmap bmpSource, int threshold)
{
     int x, y, i, j;
     double R, G, B;
     Bitmap bmpDest = new Bitmap(pictureBox2.Image);
     for (x = 1; x < bmpSource.Width - 1; x++)
     {
         for (y = 1; y < bmpSource.Height - 1; y++)
         {
              Color pix = bmpSource.GetPixel(x, y);
              R = pix.R;
              G = pix.G;
              B = pix.B;
 
              Color newPix;
 
              if (Math.Sqrt(R * R + G * G + B * B) > threshold)
              {
                   newPix = nPixBright;
              }
              else
              {
                   newPix = nPixDark;
              }
              bmpDest.SetPixel(x, y, newPix);
         }
     }
     return bmpDest;
}






This function gets the threshold and replaces all pixels brighter than it by the bright color and all pixels darker by the dark color.


Now the image must be loaded and both functions must be called to process the image:



int threshold = 180;
byte[] buffer;
 
FileStream fileStream = new FileStream(Application.StartupPath + "\\Fiat_500_blank.jpg", FileMode.Open, FileAccess.Read);
try
{
     int length = (int)fileStream.Length;
     buffer = new byte[length];
     int count;
     int sum = 0;                         
 
     // read until Read method returns 0 (end of the stream has been reached)
     while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
         sum += count;  // sum is a buffer offset for next reading
}
finally
{
     fileStream.Close();
}
MemoryStream imageStream = new MemoryStream(buffer, 0, buffer.Length);
string tempString = System.Text.Encoding.ASCII.GetString(buffer);
char[] bufferJpeg = new char[tempString.Length];
tempString.CopyTo(0, bufferJpeg, 0, tempString.Length);
imageStream.Position = 0;
Bitmap bmpSource = new Bitmap(imageStream);
Bitmap bmpDest1 = new Bitmap(pbDest.Image);
Bitmap bmpDest2 = new Bitmap(pbDest.Image);
bmpDest1 = GrayScale(bmpSource);
bmpDest2 = ProcessImage(bmpDest1, threshold);
pbDest.Image = bmpDest2;
pbDest.Image.Save(Application.StartupPath + "\\Fiat_gr.jpg");




With different colors that produces


Warhol


Or


Warhol


Or Andy Warhol like collected in one picture:


Warhol


Quite modern art :-)


The sample project consists of one main window:


Warhol


It shows the source image on the left side and the destination image on the right. The bright and the dark color can be chosen by two buttons that open a color selection dialogue. Pressing the button Process converts the source image to the selected colors and saves the destination image into the application directory. The source image is loaded from the application directory to.




C# Demo Project Andy Warhol emulator
  • Img_Warhol.zip