Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cropping and slicing a path
#1
I made a plugin to crop a path, similar to cropping an image or a layer. I don't know any such tool in Gimp previously, but what I know about Gimp is very limited, so please enlighten me if I am wrong.

Actually I made four plugins, two for cropping, two for slicing:
  1. Crop path by convex polygon
  2. Crop path by rectangle
  3. Slice path by guides
  4. Slice path by lines
Numbers 2 and 3 are the main plugins, that is, I expect them to be the most used. The other two came as by-products.

To get the plugins, go to Cropping and slicing a path, scroll to the bottom, and click the download button. You get a zip file. Unzip it and place the one file it contains (crop_path.py) in your user's plug-ins folder. Then (re)start Gimp.

To invoke the plugins, go to the Paths tab, right-click some path and follow the link
Tools > Cropping and slicing > ...

I explain now the plugins. They are very simple to use (quite an achievement from me).

Crop path by rectangle takes as input a path (the right-clicked one) and it allows four choices for the cropping rectangle:
  1. a rectangular selection
  2. four guides (two horizontal, two vertical)
  3. image (canvas)
  4. the active layer.
The first option (selection) allows in fact any selection, not only rectangular, but only its bounding box is used. In this sense it works just in the same way as the usual tool Image > Crop to selection works for images.

The second option (guides) requires that no other guides exist.

   

Crop path by convex polygon works similarly except that instead of a rectangle the cropping region is a convex polygon. The polygon is inputted as a path: closed, with straight edges, enclosing a convex region. ("Convex" means that all corners jut outwards.) Of the polygon path only anchors are taken into account; all curviness is ignored.

   

Then the two slicing plugins:

Slice path by lines takes as input two paths. One is the source path to be sliced (the right-clicked one), and the other is supposed to be a set of straight line segments, one line segment per stroke. Of each stroke the first two anchors are read and used to determine a line. You can think that each stroke is supposed to be a 2-anchor path, a straight line segment. Note that the line segments may be short, but the plugin sees and uses them as full infinite lines.

If the slicing path has only one stroke (only one slicing line), the source path is split into two parts. This is the situation in the following picture. For this picture, I moved the two halves of the result slightly apart just so that you can see what happened.

   

What happens here is more precisely the following. The path is splitted into two parts. Two new paths are created: if the original path has name 'path', the new ones will probably be 'path|slice' and 'path|slice #1'. If either of them is empty, it is discarded (not inserted in Gimp). But in some cases an additional new path is created. Namely, if the original path contains straight line segments which lie flat on the slicing line, there is no way to tell to which side they belong, so they are collected as a separate additional new path.

In the next picture the slicing path consists of three strokes (three slicing lines). Again I moved the sliced parts a little just to show the effect.

   

Slice path by guides works quite similarly except that the slicing lines are guides. You have the choice to use all guides in the image, or only horizontal guides, or only vertical guides.

A word of warning: The running time grows fast with the number of slicing lines (or guides). That is why I prevented the usage of more than 8 lines. Another advantage of this is that the number of created new paths also grows fast, and it is good to have some limit. If you want to change that number 8, find and edit in the code the line

MAX_SLICE_LINES = 8 # Maximum number of slicing lines allowed

But please, do not go over 10.

If you find some case where the plugins act strangely, please report. The code is a little messy, I admit, and I may have missed something.
Reply


Messages In This Thread
Cropping and slicing a path - by Ottia Tuota - 10-09-2020, 11:31 AM
RE: Cropping and slicing a path - by Ofnuts - 10-09-2020, 08:09 PM
RE: Cropping and slicing a path - by Ottia Tuota - 10-09-2020, 08:55 PM
RE: Cropping and slicing a path - by Krikor - 10-09-2020, 10:51 PM
RE: Cropping and slicing a path - by Ottia Tuota - 10-10-2020, 04:27 AM
RE: Cropping and slicing a path - by Ottia Tuota - 01-01-2021, 11:08 AM
RE: Cropping and slicing a path - by Ottia Tuota - 01-17-2021, 04:08 PM
RE: Cropping and slicing a path - by denzjos - 01-19-2021, 08:51 AM
RE: Cropping and slicing a path - by Krikor - 01-19-2021, 01:22 PM
RE: Cropping and slicing a path - by Ottia Tuota - 01-19-2021, 02:08 PM
RE: Cropping and slicing a path - by Krikor - 01-19-2021, 02:37 PM
RE: Cropping and slicing a path - by Ottia Tuota - 01-21-2021, 12:30 PM
RE: Cropping and slicing a path - by Krikor - 01-21-2021, 04:10 PM
RE: Cropping and slicing a path - by denzjos - 01-21-2021, 02:30 PM
RE: Cropping and slicing a path - by Ottia Tuota - 03-30-2021, 01:38 PM
RE: Cropping and slicing a path - by Ofnuts - 03-30-2021, 03:07 PM
RE: Cropping and slicing a path - by Ottia Tuota - 03-30-2021, 04:49 PM
RE: Cropping and slicing a path - by Krikor - 03-30-2021, 06:45 PM
RE: Cropping and slicing a path - by Ottia Tuota - 03-30-2021, 08:32 PM
RE: Cropping and slicing a path - by Krikor - 03-30-2021, 09:34 PM
RE: Cropping and slicing a path - by Ottia Tuota - 03-31-2021, 07:03 AM
RE: Cropping and slicing a path - by Ottia Tuota - 03-31-2021, 12:28 PM
RE: Cropping and slicing a path - by denzjos - 03-31-2021, 03:27 PM
RE: Cropping and slicing a path - by Ofnuts - 03-31-2021, 04:00 PM
RE: Cropping and slicing a path - by denzjos - 03-31-2021, 04:16 PM
RE: Cropping and slicing a path - by Krikor - 03-31-2021, 05:41 PM
RE: Cropping and slicing a path - by Krikor - 04-01-2021, 12:33 AM
RE: Cropping and slicing a path - by Ottia Tuota - 04-01-2021, 06:23 AM
RE: Cropping and slicing a path - by Krikor - 04-01-2021, 08:47 AM
RE: Cropping and slicing a path - by Ofnuts - 04-01-2021, 11:54 AM

Forum Jump: