Jump to content

crop black and white images down to the smallest rectangular size that contains the black part


Recommended Posts

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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: post-53337-0-43043300-1436585023.png

 

Here is the plugin: UnselectedRectangleKeeper .zip

Edited by MJW
  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...