pschroeter Posted July 9, 2015 Share Posted July 9, 2015 I want to crop black and white images down to the smallest rectangular size that contains the black part. The way I do it now is I select the white background, invert the selections than use Crop to Selection. Doing it this way changes the canvas size the way I want, but also removes the white from inside the image, which I than have to fill back in. I just wonder if there is a simpler way to do this, either natively or with a plugin. I have been creating a bunch of mathematical formulae that are pictures and I want to make them as small as possible for the flashcard program I am using. Quote Link to comment Share on other sites More sharing options...
racerx Posted July 9, 2015 Share Posted July 9, 2015 Not sure I understand your question, but I think the smallest crop would be 1x1 pixel square. Quote Link to comment Share on other sites More sharing options...
Ego Eram Reputo Posted July 10, 2015 Share Posted July 10, 2015 @Racerx - pschroeter want to crop to the smallest bounding rectangle which contains all of the black elements. Plugins cannot crop an image. So that option is out. Following the method you use above, how about adding a new layer (Ctrl+Shift+N), move it below the B & W layer. Select all of it (Ctrl+A) and fill it with the default Secondary color (Shift + Backspace). Flatten and save. Quote ebook: Mastering Paint.NET | resources: Plugin Index | Stereogram Tut | proud supporter of Codelab plugins: EER's Plugin Pack | Planetoid | StickMan | WhichSymbol+ | Dr Scott's Markup Renderer | CSV Filetype | dwarf horde plugins: Plugin Browser | ShapeMaker Link to comment Share on other sites More sharing options...
MJW Posted July 11, 2015 Share Posted July 11, 2015 (edited) Here is a semi-sensible plugin that might help. I call it Unselected Rectangle Keeper, because (believe it or not) that was the best name I could come up with. If you select a rectangular region with an unselected area inside it, then run Effects>Selection>Unselected Rectangle Keeper, it will erase everything outside the bounding rectangle for the unselected area. Then you can select the transparent area, invert the selection, and crop. Here is the code: Hidden Content: // Author: MJW // Name: Unselected Rectangle Keeper // Title: Unselected Rectangle Keeper // Submenu: Selection // Desc: Erase the area outside the bounding rectangle of the unselected interior. // Keywords: erase unselected region rectangle #region UICode #endregion private volatile bool firstTime = true; private object myLock = new object(); int left, right, top, bottom; void Render(Surface dst, Surface src, Rectangle rect) { PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds); Rectangle selection = selectionRegion.GetBoundsInt(); if (firstTime) lock (myLock) { if (firstTime) { int l = int.MaxValue, r = -1, t = int.MaxValue, b = -1; for (int y = selection.Top; y < selection.Bottom; y++) { for (int x = selection.Left; x < selection.Right; x++) { if (!selectionRegion.IsVisible(x, y)) { if (x < l) l = x; if (x > r) r = x; if (t == int.MaxValue) t = y; b = y; } } } right = r; left = l; top = t; bottom = b; firstTime = false; } } // Erase everything outside the bounding rectangle for the unselected region. for (int y = rect.Top; y < rect.Bottom; y++) { for (int x = rect.Left; x < rect.Right; x++) { ColorBgra pixel; if ((y < top) || (y > bottom) || (x < left) || (x > right)) pixel = ColorBgra.Transparent; else pixel = src[x, y]; dst[x, y] = pixel; } } } Here is the icon: Here is the plugin: UnselectedRectangleKeeper .zip Edited July 11, 2015 by MJW 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.