public void Resize() { var file = "test3.jpg"; MagickWandEnvironment.Genesis(); using(var mw = new MagickWand()) { mw.ReadImage("test.jpg"); mw.ResizeImage(120, 100, FilterTypes.LanczosFilter, 1); mw.WriteImage(file, true); Assert.True(File.Exists(file), "scaled image not created"); } using(var mw = new MagickWand(file)) { Assert.True(mw.ImageWidth == 120, "width does not match the expected size"); Assert.True(mw.ImageHeight == 100, "height does not match the expected size"); } File.Delete(file); MagickWandEnvironment.Terminus(); }
public async Task<ProcessingResult> ProcessPhotoAsync(string filename) { using(var wand = new MagickWand()) { var srcFile = _pathHelper.GetSourceFilePath(filename); if(_rawConverter.IsRawFile(srcFile)) { var conversionResult = await _rawConverter.ConvertAsync(srcFile); wand.ReadImage(conversionResult.OutputFile); File.Delete(conversionResult.OutputFile); } else { wand.ReadImage(srcFile); } wand.AutoOrientImage(); wand.AutoLevelImage(); wand.StripImage(); var path = Path.Combine(Path.GetDirectoryName(srcFile), "review", $"{Path.GetFileNameWithoutExtension(filename)}.jpg"); wand.WriteImage(path, true); } return null; }
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); } }
public void SaveResizedImage(int newWidth, string outPath) { CreateDirectoryForFile(outPath); using (MagickWand wand = new MagickWand()) { wand.ReadImage(filename); wand.TransformImage("", newWidth.ToString()); wand.WriteImage (outPath); } }
public void SaveResizedImage(int newWidth, int newHeight, string outPath) { CreateDirectoryForFile(outPath); using (MagickWand wand = new MagickWand()) { wand.ReadImage(filename); if (width > newWidth && height > newHeight) { wand.ResizeImage((uint)newWidth, (uint)newHeight); } else { // XXX: Make a new image that's newwidth by newheight // and center old image in it } wand.WriteImage (outPath); } }
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; } }
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; } }
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); }
public uint GetOptimalQuality(MagickWand wand) { var tmp = $"{Path.GetTempFileName()}.jpg"; try { using(var tmpWand = wand.Clone()) { tmpWand.CompressionQuality = MAX_QUALITY; wand.WriteImage(tmp, true); } var opts = new ImgminOptions { ErrorThreshold = 0.08 }; var imgmin = new Imgmin(opts); var result = imgmin.Minify(tmp, tmp); if(!_quiet) { Console.WriteLine(result.StandardOutput); } // the following has not been reliable, so figure out the // quality based on opening the tmp file. //return Convert.ToUInt32(result.StatsAfter.Quality); using(var qualWand = new MagickWand(tmp)) { return qualWand.ImageCompressionQuality; } } finally { File.Delete(tmp); } }
void WriteImage(string test, string filename, string[] args, MagickWand wand) { var dir = Path.Combine("contrast_tests", test); var file = $"{Path.GetFileNameWithoutExtension(filename)}_{string.Join("_", args)}{Path.GetExtension(filename)}"; var fullPath = Path.Combine(dir, file); Directory.CreateDirectory(dir); wand.WriteImage(fullPath, true); }
void Write(MagickWand wand, string file) { if(KEEP_FILES) { wand.WriteImage("wrapper_out" + Path.DirectorySeparatorChar + file, true); } }
public void StreamTest() { MagickWandEnvironment.Genesis(); using(var wand = new MagickWand(TestHelper.TEST_FILE)) using(var ms = new MemoryStream()) { wand.WriteImage(ms); var streamfile = "streamtest.jpg"; using(var fs = new FileStream(streamfile, FileMode.CreateNew)) { ms.CopyTo(fs); fs.Flush(); } Assert.True(File.Exists(streamfile)); using(var newWand = new MagickWand(streamfile)) { Assert.True(wand.ImageHeight == newWand.ImageHeight); Assert.True(wand.ImageWidth == newWand.ImageWidth); } File.Delete(streamfile); } MagickWandEnvironment.Terminus(); }