[ACCEPTED]-How is a sepia tone created?-imagemagick

Accepted answer
Score: 26

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

The algorithm 6 comes from this page, each input pixel color is transformed 5 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 4 values is greater than 255, you simply set 3 it to 255. These specific values are the 2 values for sepia tone that are recommended 1 by Microsoft.

Score: 4

This is in C#, however, the basic concepts 2 are the same. You will likely be able to 1 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);

}
Score: 1

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

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

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

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

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

...and no reference 1 to any Sepia method.

Score: 1

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

The 3 basics seem to be

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

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

More Related questions