Пример #1
0
        public static void CropPadding(string path, int pixMin, int pixMax, bool cut)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            PreProcessing(path);

            Random rand = new Random();
            int    pix  = rand.Next(pixMin, pixMax + 1);
            int    w    = img.Width;
            int    h    = img.Height;

            if (!cut)
            {
                w = img.Width + pix * 2;
                h = img.Height + pix * 2;
            }
            else
            {
                w = img.Width - pix;
                h = img.Height - pix;
            }
            img.BackgroundColor = new MagickColor("#" + Config.Get("backgroundColor"));
            MagickGeometry geom = new MagickGeometry(w + "x" + h + "!");

            img.Extent(geom, Gravity.Center);

            img.Write(path);
            PostProcessing(img, path);
        }
Пример #2
0
        public static async Task ConvertToPng(string path, int q = 50, bool delSrc = false)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Png;
            if (await Config.GetInt("pngColorDepth") == 1)
            {
                img.Format = MagickFormat.Png24;
            }
            if (await Config.GetInt("pngColorDepth") == 2)
            {
                img.Format = MagickFormat.Png32;
            }
            img.Quality = q;
            string outPath = Path.ChangeExtension(path, null) + ".png";

            if (path == outPath)
            {
                File.Delete(path);
            }

            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc);
        }
Пример #3
0
        public static void AddNoise(string path, List <NoiseType> noiseTypes, double attenMin, double attenMax, bool monoChrome)
        {
            if (noiseTypes.Count < 1)
            {
                return;
            }
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            NoiseType chosenNoiseType = GetRandomNoiseType(noiseTypes);

            PreProcessing(path, "- Noise Type: " + chosenNoiseType.ToString());
            Random rand = new Random();
            double att  = (double)rand.Next((int)attenMin, (int)attenMax + 1);

            Program.Print("-> Using attenuate factor " + att);
            if (monoChrome)
            {
                MagickImage noiseImg = new MagickImage(MagickColors.White, img.Width, img.Height);
                noiseImg.AddNoise(chosenNoiseType, att);
                noiseImg.ColorSpace = ColorSpace.LinearGray;
                noiseImg.Write(Path.Combine(IOUtils.GetAppDataDir(), "lastnoiseimg.png"));
                img.Composite(noiseImg, CompositeOperator.Multiply);
            }
            else
            {
                img.AddNoise(chosenNoiseType, att);
            }
            img.Write(path);
            PostProcessing(img, path, path);
        }
Пример #4
0
        public static void CropDivisible(string path, int divisibleBy, Gravity grav, bool expand)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }

            int divisbleWidth   = img.Width;
            int divisibleHeight = img.Height;

            if (!expand) // Crop
            {
                while (divisbleWidth % divisibleBy != 0)
                {
                    divisbleWidth--;
                }
                while (divisibleHeight % divisibleBy != 0)
                {
                    divisibleHeight--;
                }
            }
            else // Expand
            {
                while (divisbleWidth % divisibleBy != 0)
                {
                    divisbleWidth++;
                }
                while (divisibleHeight % divisibleBy != 0)
                {
                    divisibleHeight++;
                }
                img.BackgroundColor = new MagickColor("#" + Config.Get("backgroundColor"));
            }

            if (divisbleWidth == img.Width && divisibleHeight == img.Height)
            {
                Program.Print("-> Skipping " + Path.GetFileName(path) + " as its resolution is already divisible by " + divisibleBy);
            }
            else
            {
                Program.Print("-> Divisible resolution: " + divisbleWidth + "x" + divisibleHeight);
                if (!expand) // Crop
                {
                    img.Crop(divisbleWidth, divisibleHeight, grav);
                }
                else // Expand
                {
                    img.Extent(divisbleWidth, divisibleHeight, grav);
                }
                img.RePage();
                img.Write(path);
            }
        }
Пример #5
0
        public static async Task <string> ConvertToTempPng(string inPath)
        {
            string      outPath = Path.ChangeExtension(inPath, null) + ".temp.png";
            MagickImage img     = IOUtils.ReadImage(inPath);

            img.Format  = MagickFormat.Png00;
            img.Quality = 0;    // Disable PNG compression for speed
            img.Write(outPath);
            //Logger.Log("Input is not a PNG - Converted temporarily to PNG for compatibility", true);
            return(outPath);
        }
Пример #6
0
        static string ConvertToPng(string inpath)
        {
            string      newPath = Path.ChangeExtension(inpath, "png");
            MagickImage img     = IOUtils.ReadImage(inpath);

            img.Format  = MagickFormat.Png00;
            img.Quality = 0;    // Disable PNG compression for speed
            img.Write(newPath);
            Program.Print("-> Input is not a PNG - Converted temporarily to PNG for compatibility");
            return(newPath);
        }
Пример #7
0
        public static void AutoLevel(string path)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            string fname = Path.ChangeExtension(path, null);

            Print("-> " + fname + "\n");
            img.AutoLevel();
            img.Write(path);
        }
Пример #8
0
        public static void Scale(string path, float minScale, float maxScale, int randFilterMode, string filterName)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            FT     filter      = GetFilter(randFilterMode, filterName);
            Random rand        = new Random();
            float  targetScale = (float)rand.NextDouble(minScale, maxScale);

            img.FilterType = filter;

            bool heightLonger = img.Height > img.Width;
            bool widthLonger  = img.Width > img.Height;
            bool square       = (img.Height == img.Width);

            if ((square && currMode != SM.Percentage) || currMode == SM.Height || (currMode == SM.LongerSide && heightLonger) || (currMode == SM.ShorterSide && widthLonger))
            {
                if (onlyDownscale && (img.Height <= targetScale))
                {
                    return;
                }
                Program.Print("-> Scaling to " + targetScale + "px height with filter " + filter + "...");
                MagickGeometry geom = new MagickGeometry("x" + targetScale);
                img.Resize(geom);
            }
            if (currMode == SM.Width || (currMode == SM.LongerSide && widthLonger) || (currMode == SM.ShorterSide && heightLonger))
            {
                if (onlyDownscale && (img.Width <= targetScale))
                {
                    return;
                }
                Program.Print("-> Scaling to " + targetScale + "px width with filter " + filter + "...");
                MagickGeometry geom = new MagickGeometry(targetScale + "x");
                img.Resize(geom);
            }
            if (currMode == SM.Percentage)
            {
                Program.Print("-> Scaling to " + targetScale + "% with filter " + filter + "...");
                MagickGeometry geom = new MagickGeometry(Math.Round(img.Width * targetScale / 100f) + "x" + Math.Round(img.Height * targetScale));
                img.Resize(geom);
            }
            PreProcessing(path);
            Write(img, filter);
            PostProcessing(img, path);
            Program.Print("-> Done");
        }
Пример #9
0
        public static void DeleteSmallImages(string path, SM mode, Op op, int minPixels)
        {
            MagickImage img = IOUtils.ReadImage(path, false);

            if (img == null)
            {
                return;
            }
            bool heightLonger = img.Height > img.Width;
            bool widthLonger  = img.Width > img.Height;
            bool square       = (img.Height == img.Width);

            if (mode == SM.EitherSide)
            {
                if (HeightValid(img, op, minPixels) && WidthValid(img, op, minPixels))
                {
                    return;
                }
            }
            else
            if (mode == SM.BothSides)
            {
                if (HeightValid(img, op, minPixels) || WidthValid(img, op, minPixels))
                {
                    return;
                }
            }

            if (square || mode == SM.Height || (mode == SM.LongerSide && heightLonger) || (mode == SM.ShorterSide && widthLonger))
            {
                if (HeightValid(img, op, minPixels))
                {
                    return;
                }
            }
            else
            if (mode == SM.Width || (mode == SM.LongerSide && widthLonger) || (mode == SM.ShorterSide && heightLonger))
            {
                if (WidthValid(img, op, minPixels))
                {
                    return;
                }
            }

            string fname = Path.GetFileName(path);

            Logger.Log("-> Deleted " + fname + " (" + img.Width + "x" + img.Height + ")");
            File.Delete(img.FileName);
        }
Пример #10
0
        public static async Task ConvertToFlifMagick(string path, int q, bool delSrc)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format  = MagickFormat.Flif;
            img.Quality = q;
            img.Settings.SetDefine(MagickFormat.Flif, "lossless", true);
            string outPath = Path.ChangeExtension(path, null) + ".flif";

            IOUtils.SaveImage(img, outPath);
        }
Пример #11
0
        public static async Task ConvertToBmp(string path, bool delSrc)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Bmp;
            string outPath = Path.ChangeExtension(path, null) + ".bmp";

            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc);
        }
Пример #12
0
        public static void ConvertToTga(string path, bool delSource = false)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Tga;
            string outPath = Path.ChangeExtension(path, null) + ".tga";

            PreProcessing(path);
            img.Write(outPath);
            PostProcessing(img, path, outPath, delSource);
        }
Пример #13
0
        public static void ConvertToFlif(string path, int q, bool delSrc)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format  = MagickFormat.Flif;
            img.Quality = q;
            string outPath = Path.ChangeExtension(path, null) + ".flif";

            PreProcessing(path);
            img.Write(outPath);
            PostProcessing(img, path, outPath, delSrc);
        }
Пример #14
0
        public static void CropAbsolute(string path, int newWidth, int newHeight, Gravity grav)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            PreProcessing(path);

            img.BackgroundColor = new MagickColor("#" + Config.Get("backgroundColor"));
            img.Extent(newWidth, newHeight, grav);

            img.Write(path);
            PostProcessing(img, path);
        }
Пример #15
0
        public static async Task ConvertToJpeg2000(string path, int q, bool delSrc = false)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Jp2;
            string outPath = Path.ChangeExtension(path, null) + ".jp2";

            img.Quality = q;
            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc);
        }
Пример #16
0
        public static void ConvertToJpeg2000(string path, int q, bool delSource = false)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Jp2;
            string outPath = Path.ChangeExtension(path, null) + ".jp2";

            img.Quality = q;
            PreProcessing(path);
            img.Write(outPath);
            PostProcessing(img, path, outPath, delSource);
        }
Пример #17
0
        public static async Task Scale(string path, float minScale, float maxScale, int randFilterMode, string filterName)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            FT     filter      = GetFilter(randFilterMode, filterName);
            Random rand        = new Random();
            float  targetScale = (float)rand.NextDouble(minScale, maxScale);

            img.FilterType = filter;

            bool heightLonger = img.Height > img.Width;
            bool widthLonger  = img.Width > img.Height;
            bool square       = (img.Height == img.Width);

            if ((square && currMode != SM.Percentage) || currMode == SM.Height || (currMode == SM.LongerSide && heightLonger) || (currMode == SM.ShorterSide && widthLonger))
            {
                if (onlyDownscale && (img.Height <= targetScale))
                {
                    return;
                }
                MagickGeometry geom = new MagickGeometry("x" + targetScale);
                img.Resize(geom);
            }
            if (currMode == SM.Width || (currMode == SM.LongerSide && widthLonger) || (currMode == SM.ShorterSide && heightLonger))
            {
                if (onlyDownscale && (img.Width <= targetScale))
                {
                    return;
                }
                MagickGeometry geom = new MagickGeometry(targetScale + "x");
                img.Resize(geom);
            }
            if (currMode == SM.Percentage)
            {
                MagickGeometry geom = new MagickGeometry(Math.Round(img.Width * targetScale / 100f) + "x" + Math.Round(img.Height * targetScale));
                img.Resize(geom);
            }

            string outPath = await Write(img, filter);

            ConvertUtils.PostProcessing(path, outPath, bytesSrc, false, $"Scaled to {targetScale}% with {filter}");
        }
Пример #18
0
        public void PreviewImage(string imgPath)
        {
            MagickImage tempImg = IOUtils.ReadImage(imgPath);

            if (tempImg == null)
            {
                return;
            }
            tempImg.Format = MagickFormat.Png;
            string tempImgPath = Path.Combine(IOUtils.GetAppDataDir(), "previewImg.png");

            tempImg.Write(tempImgPath);
            Program.previewImgPath = tempImgPath;
            var imgForm = new ImagePreviewPopup();

            imgForm.Show();
        }
Пример #19
0
        public static async Task ConvertToJxl(string path, int qMin, int qMax, bool delSrc = false)
        {
            long        bytesSrc = new FileInfo(path).Length;
            Random      rand     = new Random();
            int         q        = rand.Next(qMin, qMax + 1);
            string      outPath  = Path.ChangeExtension(path, null) + ".jxl";
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format  = MagickFormat.Jxl;
            img.Quality = q;
            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc, $"JPEG XL Quality: {q}");
        }
Пример #20
0
        public static async Task ConvertToJpeg(string path, int qMin, int qMax, bool delSrc = false)
        {
            long   bytesSrc = new FileInfo(path).Length;
            Random rand     = new Random();
            int    q        = rand.Next(qMin, qMax + 1);
            string outPath  = Path.ChangeExtension(path, null) + ".jpg";

            if (await Config.GetInt("jpegEnc") == 0) // Native Magick JPEG Encoder
            {
                MagickImage img = IOUtils.ReadImage(path);
                if (img == null)
                {
                    return;
                }
                img.Format  = MagickFormat.Jpeg;
                img.Quality = q;
                img         = await SetJpegChromaSubsampling(img);

                IOUtils.SaveImage(img, outPath);
            }
            else // MozJPEG Encoder
            {
                bool convert = !IsPng(path);
                if (convert)
                {
                    path = await ConvertToTempPng(path);
                }

                switch (await Config.GetInt("jpegChromaSubsampling"))
                {
                case 0: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma420); break;

                case 1: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma422); break;

                case 2: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma444); break;
                }

                if (convert)
                {
                    IOUtils.TryDeleteIfExists(path);
                }
            }

            PostProcessing(path, outPath, bytesSrc, delSrc, $"JPEG Quality: {q}");
        }
Пример #21
0
        public static async void PrintImageInfoDir()
        {
            int counter = 1;

            FileInfo[] files = IOUtils.GetFiles(Config.GetBool("fileOperationsNoFilter"));
            Program.PreProcessing(true, false);
            foreach (FileInfo file in files)
            {
                MagickImage img        = IOUtils.ReadImage(file.FullName);
                string      fnameNoExt = Path.GetFileNameWithoutExtension(file.Name);
                Program.ShowProgress("", counter, files.Length);
                counter++;
                if (counter % 10 == 0)
                {
                    await Program.PutTaskDelay();
                }
            }
            Program.PostProcessing(true, false);
        }
Пример #22
0
        public static async Task Tile(string path, int tileW, int tileH, bool useTileAmount, bool delSrc)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            PreProcessing(path);

            string pathNoExt = Path.ChangeExtension(path, null);
            string ext       = Path.GetExtension(path);

            if (useTileAmount)
            {
                tileW = (int)Math.Round(img.Width / (float)tileW);
                tileH = (int)Math.Round(img.Height / (float)tileH);
            }

            int i = 1;

            Program.Print("-> Creating tiles...");
            var tiles = img.CropToTiles(tileW, tileH);

            foreach (MagickImage tile in tiles)
            {
                tile.Write(pathNoExt + "-tile" + i + ext);
                Program.Print("-> Saved tile " + i + "/" + tiles.Count(), true);
                i++;
                if (i % 2 == 0)
                {
                    await Program.PutTaskDelay();
                }
            }
            await Program.PutTaskDelay();

            PostProcessing(img, path);
            if (delSrc)
            {
                DelSource(path);
            }
        }
Пример #23
0
        public static void CropRelative(string path, int minSize, int maxSize, SizeMode sizeMode, Gravity grav)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            PreProcessing(path);

            Random         rand       = new Random();
            int            targetSize = rand.Next(minSize, maxSize + 1);
            MagickGeometry geom       = null;

            bool heightLonger = img.Height > img.Width;
            bool widthLonger  = img.Width > img.Height;
            bool square       = (img.Height == img.Width);

            if (square || sizeMode == SizeMode.Height || (sizeMode == SizeMode.Longer && heightLonger) || (sizeMode == SizeMode.Shorter && widthLonger))
            {
                Program.Print("-> Resizing to " + targetSize + "px height...");
                int w = (int)Math.Round(img.Width * ((targetSize / (float)img.Height)));
                geom = new MagickGeometry(w + "x" + targetSize);
            }
            if (sizeMode == SizeMode.Width || (sizeMode == SizeMode.Longer && widthLonger) || (sizeMode == SizeMode.Shorter && heightLonger))
            {
                Program.Print("-> Resizing to " + targetSize + "px width...");
                int h = (int)Math.Round(img.Height * ((targetSize / (float)img.Width)));
                geom = new MagickGeometry(targetSize + "x" + h);
            }
            if (sizeMode == SizeMode.Percentage)
            {
                int w = (int)Math.Round(img.Width * targetSize / 100f);
                int h = (int)Math.Round(img.Height * targetSize / 100f);
                Program.Print("-> Resizing to " + targetSize + "% (" + w + "x" + h + ")...");
                geom = new MagickGeometry(w + "x" + h);
            }
            img.BackgroundColor = new MagickColor("#" + Config.Get("backgroundColor"));
            img.Extent(geom, grav);
            img.Write(path);
            PostProcessing(img, path);
        }
Пример #24
0
        public static void ConvertToWebp(string path, int qMin, int qMax, bool delSource = false)
        {
            MagickImage img = IOUtils.ReadImage(path); if (img == null)

            {
                return;
            }

            img.Format = MagickFormat.WebP;
            string outPath = Path.ChangeExtension(path, null) + ".webp";
            Random rand    = new Random();

            img.Quality = rand.Next(qMin, qMax + 1);
            if (img.Quality >= 100)
            {
                img.Settings.SetDefine(MagickFormat.WebP, "lossless", true);
            }
            PreProcessing(path, " [WEBP Quality: " + img.Quality.ToString().Replace("100", "Lossless") + "]");
            img.Write(outPath);
            PostProcessing(img, path, outPath, delSource);
        }
Пример #25
0
        public static async Task RandomResample(string path, float minScale, float maxScale, int downFilterMode, string downFilterName, int upFilterMode, string upFilterName)
        {
            long        bytesSrc    = new FileInfo(path).Length;
            MagickImage img         = IOUtils.ReadImage(path);
            FT          filter      = GetFilter(downFilterMode, downFilterName);
            int         srcWidth    = img.Width;
            int         srcHeight   = img.Height;
            Random      rand        = new Random();
            float       targetScale = (float)rand.NextDouble(minScale, maxScale + 1);

            img.FilterType = filter;
            img.Resize(new Percentage(targetScale));
            MagickGeometry upscaleGeom = new MagickGeometry(srcWidth + "x" + srcHeight + "!");

            img.FilterType = GetFilter(upFilterMode, upFilterName);
            img.Resize(upscaleGeom);
            PreProcessing(path);
            string outPath = await Write(img, filter);

            ConvertUtils.PostProcessing(path, outPath, bytesSrc, false, $"Scaled to {targetScale.ToString("0.00")}% with {filter}");
        }
Пример #26
0
        public static void RandomResample(string path, float minScale, float maxScale, int downFilterMode, string downFilterName, int upFilterMode, string upFilterName)
        {
            MagickImage img         = IOUtils.ReadImage(path);
            FT          filter      = GetFilter(downFilterMode, downFilterName);
            int         srcWidth    = img.Width;
            int         srcHeight   = img.Height;
            Random      rand        = new Random();
            float       targetScale = (float)rand.NextDouble(minScale, maxScale + 1);

            Program.Print("-> Scaling down to " + targetScale + "% with filter " + filter + "...");
            img.FilterType = filter;
            img.Resize(new Percentage(targetScale));
            MagickGeometry upscaleGeom = new MagickGeometry(srcWidth + "x" + srcHeight + "!");

            img.FilterType = GetFilter(upFilterMode, upFilterName);
            Program.Print("-> Scaling back up...\n");
            img.Resize(upscaleGeom);
            PreProcessing(path);
            Write(img, filter);
            PostProcessing(img, path);
        }
Пример #27
0
        public static void PreviewImage(string imgPath, bool run = false)
        {
            MagickImage tempImg = IOUtils.ReadImage(imgPath);

            if (tempImg == null)
            {
                return;
            }
            tempImg.Format = MagickFormat.Png;
            string tempImgPath = Path.Combine(Paths.GetDataPath(), "previewImg.png");

            tempImg.Write(tempImgPath);
            tempImg.Dispose();
            previewImgPath = tempImgPath;
            if (run)
            {
                Application.Run(new ImagePreviewPopup());
            }
            else
            {
                new ImagePreviewPopup().Show();
            }
        }