Ejemplo n.º 1
0
        /// <summary>
        /// Performs the EntropyCrop effect from the ImageProcessor library.
        /// </summary>
        /// <param name="image">The image with which to apply the effect.</param>
        /// <param name="threshold">The value used to determine where the crop will occur.</param>
        /// <returns>A new image with the EntropyCrop effect applied.</returns>
        public Image EntropyCrop(Image image, byte threshold = 128)
        {
            using var factory = new ImageFactory();

            factory.Load(image);
            factory.EntropyCrop(threshold);

            var result = factory.Image;

            return(new Bitmap(result));
        }
Ejemplo n.º 2
0
        void IModule.Install(ModuleManager manager)
        {
            _manager = manager;
            _client  = manager.Client;
            _http    = _client.GetService <HttpService>();

            manager.CreateCommands("image", group =>
            {
                group.CreateCommand("caption")
                .Parameter("uri", ParameterType.Required)
                .Parameter("parameters", ParameterType.Unparsed)
                .Description("Adds text to an Image.\n<`uri`> ⦗`color`⦘ ⦗`alpha`⦘ ⦗`position`⦘ ⦗`fontsize`⦘ ⦗`dropshadow`⦘\nExample usage:\n⦗`cap http://myimage.png red 0.5 center arial 12 1 hello world`⦘\n⦗`cap http://myimage.png hello world`⦘")
                .Alias("cap")
                .Do(async e =>
                {
                    if (e.Args.Any())
                    {
                        var args   = getArgs(e);
                        string uri = e.Args[0];

                        if (args.Any())
                        {
                            if (await isImage(uri))
                            {
                                string file = "img_cap_tmp" + Guid.NewGuid().ToString() + await getImageExtension(uri);

                                try
                                {
                                    await DownloadImage(uri, file);
                                }
                                catch (WebException ex)
                                {
                                    await _client.ReplyError(e, ex.Message);
                                    _client.Log.Error("captions", ex);

                                    if (File.Exists(file))
                                    {
                                        File.Delete(file);
                                    }
                                    return;
                                }

                                if (File.Exists(file))
                                {
                                    byte[] pb = File.ReadAllBytes(file);

                                    var asd        = new TextLayer();
                                    asd.Text       = args?["text"];
                                    asd.FontColor  = args.ContainsKey("color") ? System.Drawing.Color.FromName(args["color"]) : System.Drawing.Color.White;
                                    asd.FontFamily = args.ContainsKey("font") ? FontFamily.Families.Where(x => x.Name == args["font"]).FirstOrDefault() : FontFamily.GenericSerif;
                                    asd.DropShadow = args.ContainsKey("dropshadow") ? bool.Parse(args["dropshadow"]) : false;
                                    // asd.Position = Point.Empty;
                                    asd.Opacity  = args.ContainsKey("opacity") ? int.Parse(args["opacity"]) : 100;
                                    asd.Style    = args.ContainsKey("style") ? (FontStyle)Enum.Parse(typeof(FontStyle), args["style"], true) : FontStyle.Regular;
                                    asd.FontSize = args.ContainsKey("size") ? int.Parse(args["size"]) : 20;

                                    ISupportedImageFormat format = new PngFormat {
                                        Quality = 100
                                    };

                                    using (MemoryStream ins = new MemoryStream(pb))
                                        using (MemoryStream outs = new MemoryStream())
                                            using (ImageFactory iff = new ImageFactory(true))
                                            {
                                                iff.Load(ins)
                                                .Watermark(asd)
                                                .Format(format)
                                                .Save(outs);
                                                await e.Channel.SendFile("output.png", outs);
                                            }
                                    File.Delete(file);
                                }
                                else
                                {
                                    await _client.ReplyError(e, "Couldn't find your image on my end. Bug @Techbot about it.");
                                    return;
                                }
                            }
                            else
                            {
                                await _client.ReplyError(e, "That doesn't seem to be an image...");
                            }
                        }
                        else
                        {
                            await _client.ReplyError(e, "No Parameters provided, aborting...");
                            return;
                        }
                    }
                    else
                    {
                        await _client.Reply(e, "Usage: `cap [link] <text>`");
                    }
                });
                group.CreateCommand("edit")
                .Parameter("uri", ParameterType.Required)
                .Parameter("parameters", ParameterType.Unparsed)
                .Description("transforms an image.\nSupported Parameters:\n\n\nTint: `tint=red`\nCensor: `censor=x;y;w;h`\n`Scaling: `w=num or h=num`\n`blur=num`\n`rot=num`\n`filp=true/false`\n`crop=true + (top=num or bottom=num or left=num or right=num)`")
                .Alias("e")
                .Do(async e =>
                {
                    if (e.Args.Any())
                    {
                        var args   = getArgs(e);
                        string uri = e.Args[0];

                        if (await isImage(uri))
                        {
                            string file = "img_e_tmp" + Guid.NewGuid().ToString() + await getImageExtension(uri);
                            try
                            {
                                await DownloadImage(uri, file);
                            }
                            catch (WebException ex)
                            {
                                await _client.ReplyError(e, ex.Message);
                                _client.Log.Error("captions", ex);

                                if (File.Exists(file))
                                {
                                    File.Delete(file);
                                }
                                return;
                            }
                            if (File.Exists(file))
                            {
                                byte[] pb = File.ReadAllBytes(file);

                                ISupportedImageFormat format = new PngFormat {
                                    Quality = 100
                                };

                                using (MemoryStream ins = new MemoryStream(pb))
                                    using (MemoryStream outs = new MemoryStream())
                                        using (ImageFactory iff = new ImageFactory(true))
                                        {
                                            iff.Load(ins);
                                            if (args.ContainsKey("rot"))
                                            {
                                                iff.Rotate(int.Parse(args["rot"]));
                                            }
                                            if (args.ContainsKey("flip"))
                                            {
                                                iff.Flip(bool.Parse(args["flip"]));
                                            }
                                            if (args.ContainsKey("blur"))
                                            {
                                                iff.GaussianBlur(int.Parse(args["blur"]));
                                            }
                                            if (args.ContainsKey("ecrop"))
                                            {
                                                iff.EntropyCrop(byte.Parse(args["ecrop"]));
                                            }
                                            if (args.ContainsKey("pixelate"))
                                            {
                                                iff.Pixelate(int.Parse(args["pixelate"]));
                                            }
                                            if (args.ContainsKey("tint"))
                                            {
                                                iff.Tint(System.Drawing.Color.FromName(args["tint"]));
                                            }
                                            if (args.ContainsKey("replacecolor"))
                                            {
                                                string[] rcargs          = args["replacecolor"].Split(';');
                                                System.Drawing.Color tar = System.Drawing.Color.FromName(rcargs[0]);
                                                System.Drawing.Color rep = System.Drawing.Color.FromName(rcargs[1]);

                                                if (rcargs.Length > 2 && int.Parse(rcargs[2]) >= 128 || int.Parse(rcargs[2]) < 0)
                                                {
                                                    await _client.ReplyError(e, "Fuzzines mix and max values are 0 - 128");
                                                    File.Delete(file);
                                                    return;
                                                }
                                                int fuzz = rcargs.Length > 2 ? int.Parse(rcargs[2]) : 128;

                                                iff.ReplaceColor(tar, rep, fuzz);
                                            }
                                            if (args.ContainsKey("censor"))
                                            {
                                                string[] cargs = args["censor"].Split(';');
                                                var pixels     = int.Parse(cargs[4]);
                                                var x          = int.Parse(cargs[0]);
                                                var y          = -int.Parse(cargs[1]);
                                                var w          = int.Parse(cargs[2]);
                                                var h          = int.Parse(cargs[3]);

                                                iff.Pixelate(pixels, new Rectangle(new Point(x, y), new Size(w, h)));
                                            }
                                            if (args.ContainsKey("w") || args.ContainsKey("h"))
                                            {
                                                int width = 0, height = 0;

                                                if (args.ContainsKey("w"))
                                                {
                                                    width = int.Parse(args["w"]);
                                                }

                                                if (args.ContainsKey("h"))
                                                {
                                                    height = int.Parse(args["h"]);
                                                }
                                                iff.Resize(new ResizeLayer(new Size(width, height), ResizeMode.Stretch, AnchorPosition.Center, true, null, new Size(5000, 5000)));
                                            }
                                            if (args.ContainsKey("crop"))
                                            {
                                                int top = 0, bottom = 0, left = 0, right = 0;

                                                // is there a better way to do this?

                                                if (args.ContainsKey("top"))
                                                {
                                                    top = int.Parse(args["top"]);
                                                }
                                                if (args.ContainsKey("bottom"))
                                                {
                                                    bottom = int.Parse(args["bottom"]);
                                                }
                                                if (args.ContainsKey("left"))
                                                {
                                                    left = int.Parse(args["left"]);
                                                }
                                                if (args.ContainsKey("right"))
                                                {
                                                    right = int.Parse(args["right"]);
                                                }

                                                iff.Crop(new CropLayer(
                                                             top,
                                                             bottom,
                                                             left,
                                                             right,
                                                             CropMode.Percentage));
                                            }

                                            iff
                                            .Format(format)
                                            .Save(outs);
                                            await e.Channel.SendFile("output.png", outs);
                                        }
                                File.Delete(file);
                            }
                            else
                            {
                                await _client.ReplyError(e, "Couldn't find your image on my end. Bug @Techbot about it.");
                                return;
                            }
                        }
                        else
                        {
                            await _client.ReplyError(e, "That doesn't seem to be an image...");
                        }
                    }
                });
            });
        }
Ejemplo n.º 3
0
 private void entropyCropToolStripMenuItem_Click(object sender, EventArgs e)
 {
     imageFactory.EntropyCrop(128);
     pictureBox.Image = imageFactory.Image;
 }
Ejemplo n.º 4
0
 public IImageTransformer EntropyCrop(byte threshold = 128)
 {
     _image.EntropyCrop(threshold);
     return(this);
 }
Ejemplo n.º 5
0
 public override void Apply(ImageFactory fs)
 {
     fs = fs.EntropyCrop(threshold);
 }