Exemplo n.º 1
0
        void RegenerateVideoThumbnail(string sourceFile, string destThumb)
        {
            DumpImageFromVideo(sourceFile, destThumb);

            var width  = 240;
            var height = 160;

            using (var wand = new MagickWand(destThumb))
            {
                float idealAspect  = (float)width / (float)height;
                float actualAspect = (float)wand.ImageWidth / (float)wand.ImageHeight;

                if (idealAspect >= actualAspect)
                {
                    width = (int)(actualAspect * (float)height);
                }
                else
                {
                    height = (int)((float)width / actualAspect);
                }

                wand.ScaleImage((uint)width, (uint)height);

                // sharpen after potentially resizing
                // http://www.imagemagick.org/Usage/resize/#resize_unsharp
                wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008);

                wand.WriteImage(destThumb, true);
            }
        }
Exemplo n.º 2
0
    void GenerateThumbnail(Ffmpeg ffmpeg, string localSourceFile, string localThumbnailFile, MovieMetadata mm)
    {
        ffmpeg.ExtractFrame(localSourceFile, localThumbnailFile, GetThumbnailSeconds(mm));

        using (var wand = new MagickWand(localThumbnailFile))
        {
            wand.GetLargestDimensionsKeepingAspectRatio(THUMB_WIDTH, THUMB_HEIGHT, out uint width, out uint height);
            wand.ScaleImage(width, height);

            // sharpen after potentially resizing
            // http://www.imagemagick.org/Usage/resize/#resize_unsharp
            wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008);

            wand.WriteImage(localThumbnailFile, true);

            mm.ThumbHeight = (int)height;
            mm.ThumbWidth  = (int)width;
        }
    }
Exemplo n.º 3
0
    void GenerateThumbSq(Ffmpeg ffmpeg, string localSourceFile, string localThumbSqFile, MovieMetadata mm)
    {
        ffmpeg.ExtractFrame(localSourceFile, localThumbSqFile, GetThumbnailSeconds(mm));

        using (var wand = new MagickWand(localThumbSqFile))
        {
            var width  = (double)wand.ImageWidth;
            var height = (double)wand.ImageHeight;
            var aspect = width / height;

            if (aspect >= THUMB_SQ_ASPECT)
            {
                var newWidth = (width / height) * THUMB_SQ_HEIGHT;

                // scale image to final height
                wand.ScaleImage((uint)newWidth, THUMB_SQ_HEIGHT);

                // crop sides as needed
                wand.CropImage(THUMB_SQ_WIDTH, THUMB_SQ_HEIGHT, (int)(newWidth - THUMB_SQ_WIDTH) / 2, 0);
            }
            else
            {
                var newHeight = THUMB_SQ_WIDTH / (width / height);

                // scale image to final width
                wand.ScaleImage(THUMB_SQ_WIDTH, (uint)newHeight);

                // crop top and bottom as needed
                wand.CropImage(THUMB_SQ_WIDTH, THUMB_SQ_HEIGHT, 0, (int)(newHeight - THUMB_SQ_HEIGHT) / 2);
            }

            // sharpen after potentially resizing
            // http://www.imagemagick.org/Usage/resize/#resize_unsharp
            wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008);

            wand.WriteImage(localThumbSqFile, true);

            mm.ThumbSqHeight = THUMB_SQ_HEIGHT;
            mm.ThumbSqWidth  = THUMB_SQ_WIDTH;
        }
    }
Exemplo n.º 4
0
        public void Generate()
        {
            using (var wand = new MagickWand(_sourcePath))
            {
                var width  = (double)wand.ImageWidth;
                var height = (double)wand.ImageHeight;
                var aspect = width / height;

                if (aspect >= Aspect)
                {
                    var newWidth = (width / height) * FinalHeight;

                    // scale image to final height
                    wand.ScaleImage((uint)newWidth, FinalHeight);

                    // crop sides as needed
                    wand.CropImage(FinalWidth, FinalHeight, (int)(newWidth - FinalWidth) / 2, 0);
                }
                else
                {
                    var newHeight = FinalWidth / (width / height);

                    // scale image to final width
                    wand.ScaleImage(FinalWidth, (uint)newHeight);

                    // crop top and bottom as needed
                    wand.CropImage(FinalWidth, FinalHeight, 0, (int)(newHeight - FinalHeight) / 2);
                }

                // sharpen after potentially resizing
                // http://www.imagemagick.org/Usage/resize/#resize_unsharp
                wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008);

                wand.WriteImage(_destPath, true);
            }

            ExecuteJpegOptimAsync(_destPath);
            ExecuteJpegTranAsync(_destPath);
        }
Exemplo n.º 5
0
 public void ComposedTest()
 {
     var contrastRatioThreshold = 0.5;
     var tooContrastyThreshold = 1.9;
     
     MagickWandEnvironment.Genesis();
     
     PrintStatsHeader();
     
     foreach(var file in _files)
     {
         using(var wand = new MagickWand(file))
         {
             double mean, stddev;
             
             wand.GetImageChannelMean(ChannelType.AllChannels, out mean, out stddev);
             
             PrintStats(file, wand);
             
             wand.AutoLevelImage();
             
             var contrastRatio = stddev / mean;
             var contrastyRatio = stddev / 10000;
             
             if(contrastRatio < contrastRatioThreshold)
             {
                 var saturationAmount = Convert.ToInt32((contrastRatioThreshold - contrastRatio) * 100) * 4;
                 
                 // limit the saturation adjustment to 20%
                 if(saturationAmount > 20)
                 {
                     saturationAmount = 20;
                 }
                 
                 saturationAmount += 100;
                 
                 Console.WriteLine("modulating by: " + saturationAmount);
                 
                 // 100 = don't adjust brightness
                 // 300 = don't rotate hue
                 wand.ModulateImage(100, saturationAmount, 300);
             }
             else if(contrastyRatio > tooContrastyThreshold)
             {
                 Console.WriteLine("attempting to reduce contrast");
                 wand.SigmoidalContrastImage(true, 2, 0);  // smooth brightness/contrast
             }
             
             wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008);  // sharpen
             
             WriteImage("composed", file, new string[] { }, wand);
         }
     }
     
     MagickWandEnvironment.Terminus();
 }