16

What are the basic operations needed to create a sepia tone? My reference point is the perl imagemagick library, so I can easily use any basic operation. I've tried to quantize (making it grayscale), colorize, and then enhance the image but it's still a bit blurry.

4 Answers 4

27

Sample code of a sepia converter in C# is available in my answer here: What is wrong with this sepia tone conversion algorithm?

The algorithm comes from this page, each input pixel color is transformed in the following way:

outputRed = (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189)
outputGreen = (inputRed * .349) + (inputGreen *.686) + (inputBlue * .168)
outputBlue = (inputRed * .272) + (inputGreen *.534) + (inputBlue * .131)

If any of these output values is greater than 255, you simply set it to 255. These specific values are the values for sepia tone that are recommended by Microsoft.

2
  • You will need to use Math.Min likely. I tried doing the check for 255 after those three lines and an error will occur. I was facing the same problem earlier today when i was trying to make a sepia tone for my program..
    – BigBug
    Feb 26, 2012 at 6:34
  • But what if i want something different to change the filter then how can i get to these values ? like my question is how we came to know about these values , do we need to just put different values again and again ?
    – AHF
    Mar 23, 2014 at 15:20
4

This is in C#, however, the basic concepts are the same. You will likely be able to convert this into perl.

  private void SepiaBitmap(Bitmap bmp)
{
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        System.Drawing.Imaging.PixelFormat.Format32bppRgb);

    IntPtr ptr = bmpData.Scan0;

    int numPixels = bmpData.Width * bmp.Height;
    int numBytes = numPixels * 4;
    byte[] rgbValues = new byte[numBytes];

    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, numBytes);
    for (int i = 0; i < rgbValues.Length; i += 4)
    {
        rgbValues[i + 2] = (byte)Math.Min((.393 * red) + (.769 * green) + (.189 * (blue)), 255.0); //red
        rgbValues[i + 1] = (byte)Math.Min((.349 * red) + (.686 * green) + (.168 * (blue)), 255.0); //green
        rgbValues[i + 0] = (byte)Math.Min((.272 * red) + (.534 * green) + (.131 * (blue)), 255.0); //blue
        if ((rgbValues[i + 2]) > 255)
        {
            rgbValues[i + 2] = 255; 
        }

        if ((rgbValues[i + 1]) > 255)
        {
            rgbValues[i + 1] = 255;
        }
        if ((rgbValues[i + 0]) > 255)
        {
            rgbValues[i + 0] = 255;
        }
    }

    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes);
    this.Invalidate();
    bmp.UnlockBits(bmpData);

}
2
  • 1
    what is the point of using math.min and using the if to check if is bigger of 255? May 8, 2017 at 7:04
  • 1
    The Math.Min is necessary to clamp the computed values to byte range. Those if statements that follow are completely pointless. rgbValues is a byte array, so none of its values can possibly be > 255 May 17, 2017 at 23:46
1

Take a look at how it's implemented in the AForge.NET library, the C# code is here.

The basics seem to be

  • transform it to the YIQ color space
  • modify it
  • transform back to RGB

The full alrogithm is in the source code, plus the RGB -> YIQ and YIQ -> RGB transformations are explained.

1
  • thanks, using this was giving bright images, so resorted to converting to grayscale first and them calling sepia
    – codejammer
    Dec 20, 2012 at 8:11
0

It's easy if you use the imagemagic command line.

http://www.imagemagick.org/script/convert.php

Use the "-sepia-tone threshold" argument when converting.

Strangely enough, the PerlMagick API doesn't seem to include a method for doing this directly:

http://www.imagemagick.org/script/perl-magick.php

...and no reference to any Sepia method.

1
  • Since I have an imagemagick object in perl this isn't the most direct way but it could work... but it doesn't seem to have an option to use other colors for the effect
    – user83358
    Jul 1, 2009 at 21:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.