Problem of the Day
A new programming or logic puzzle every Mon-Fri

Pixel Color

Today's problem breaks out of the normal mode and into graphics mode. Your goal is to create a function that takes in an image and returns the color of the top left most pixel. For instance if you used this image your function should return #0000FE.

Permalink: http://problemotd.com/problem/pixel-color/

Comments:

  • Daniel - 10 years, 6 months ago

    C#. System.Drawing namespace makes this incredibly easy.

        public string GetPixelHexValue(string strImagePath)
        {
            if (System.IO.File.Exists(strImagePath))
            {
                Bitmap objBitmap = new Bitmap(strImagePath);
                Color objColor = objBitmap.GetPixel(1, 1);
                return ColorTranslator.ToHtml(objColor);
            }
            else
            {
                throw new System.IO.FileNotFoundException();
            }
        }
    

    reply permalink

  • Daniel - 10 years, 6 months ago

    To be lazier, you could replace

            Bitmap objBitmap = new Bitmap(strImagePath);
            Color objColor = objBitmap.GetPixel(1, 1);
            return ColorTranslator.ToHtml(objColor);
    

    with

            return ColorTranslator.ToHtml(new Bitmap(strImagePath).GetPixel(1, 1));
    

    reply permalink

  • Nick Krichevsky - 10 years, 6 months ago

    Python soloution with Pillow

    from PIL import Image
    im = Image.open('image.jpg').convert('RGB')
    pixel = im.getpixel((0,0))
    print ('#%02x%02x%02x' % pixel).upper()
    

    reply permalink

Content curated by @MaxBurstein