5

For a hobby gamedev project I'm looking for a way to automate the following workflow, can be in any photo/image editing tool.

  1. load a high resolution image
  2. make multiple selections
  3. give each selection a name
  4. export each selection to a separate PNG file (with the name)
  5. export coordinates of selections to textfile (name = x,y,h,w)
  6. save image with selections for later use or re-export

So in short, take a highres image, make multiple selections and then export each selection to a separate PNG file, while also exporting a list of the selections coordinates to a text file. See what I mean in the mock up image below:

save multiple selections to seperate images and a coordinates file

I'm doing all these steps by hand now and most the images have 70-80 parts. Whenever there is a mistake, image updates, changes in resolution etc. I have to redo large parts or even the whole thing, even though the selections are roughly the same.

Some photo editor programs support plug-ins and scripting, and I can do scripting (lua, python, javascript) no problem. But I don't know of any photo editor that supports saving multiple selections. If you know a good solution to this in any photo editor, like Painshop, PhotoShop, Coral Draw or an OpenSource program like Gimp or Paint Dot Net etc. please let me know.

  • Is this hobby of yours on github? because I find this very fascinating. I'd love to play an emulated version of the Russian nock off Game N Watch games – LateralTerminal Feb 21 '18 at 21:39
  • You'd probably find this interesting en.wikipedia.org/wiki/… – LateralTerminal Feb 21 '18 at 21:40
4

But I don't know of any photo editor that supports saving multiple selections.

A solution for Gimp:

  • In the script, do the equivalent of Select>To path (using the pdb.plug_in_sel2path(image, drawable) function). This will create a path made of several "strokes", each stroke being a closed curve that represents one of your selected items(*).
  • You can then iterate the strokes in the path, and for each stroke:
    • create a new path from it
    • make a selection from that path
    • create a new layer that contains only that selection
    • save that layer to file
    • copy that layer's offset to your data file (note that since the coordinates of the stroke points are readily available, you can also compute the centroid of the object)

For some sample Gimp scripts in Python, including a whole set that deals mostly with paths, see here and here.

(*) Some Gimp paths basics:

  • Just to be clear, I can select each shape, use selection to path to create a whole list of paths and rename each path to a useful name. But then, could a custom.py script be used to iterate through all these paths, convert each path back to selections and save it as a png file? Is that the approach that you mean? – BdR Mar 13 '17 at 23:36
  • Sort of. My suggestion is to select all your elements in one go, for instance by painting over them using the quickmask mode. But your approach is also valid, even if a bit more click-intensive. From the script point of view, either it iterates several paths with one single stroke (your method), or it iterates the strokes in a single path (my method). Not much difference. Either case would be under 100 lines of python. – xenoid Mar 14 '17 at 0:25
  • Okay thanks, but the problem with a single path is that you can't give the parts useful names (trampoline_1, baby_16, fire_3 etc.). Btw I've just found a script that seems to do almost exactly what I'm looking for; split an image using paths registry.gimp.org/node/27913 so I'll take a close look at that too – BdR Mar 14 '17 at 12:23
  • Well, if you need to give a name, then using a path for each makes sense and won't slow you much. Yes, Arakne's script can likely help (or is a good start for your own). – xenoid Mar 14 '17 at 14:05
  • 1
    FYI I've used the Python tips in this project :) github.com/BdR76/lcdgame.js/tree/master/utils – BdR Apr 19 '17 at 21:14
1

A (partial / not yet working) solution for GIMP, using the disjunct pieces ("strokes") of one selection:

  1. Put the GIMP script by Andrew Kvalheim into a file ~/.gimp-2.8/scripts/export-selected-regions.scm (or the equivalent place on Windows / Mac OS X).

  2. Start GIMP.

  3. Create your selection.

  4. Choose "Select → Export Selected Regions" from the menu. (This is the new menu item added by the script.)

Caveats:

  • As of GIMP 2.8, this script did not work, complaining about "unknown image format". So it will need an update, but then provide a solution. Please report in the comments when you get it to work.

  • Compared with a solution based on paths (in the other answers here), this solution needs less click-work to define the selections but is only applicable if (1) you do not need self-defined names for your output images and (2) none of your selection pieces overlap (as that would result in one merged "stroke" when defining the selection and consequently in one output image for that "stroke").

  • Thanks for answer. However, both the self-defined names for each the output image is very important, and a lot of the selections have an overlap. In the mean time I already had a workable solution, see the export_paths_to_png.py script here github.com/BdR76/lcdgame.js/tree/master/utils – BdR Feb 21 '18 at 16:38
1

A (partial / not yet working) solution for GIMP, using a fast semi-automated process of "make a selection, save it as PNG, repeat":

  1. Put the Save Area script into a file ~/.gimp-2.8/scripts/savearea.scm (or the equivalent place on Windows / Mac OS X).

  2. Start GIMP.

  3. Go to "Edit → Keyboard Shortcuts" and assign a keyboard shortcut to the "File → Save area" menu item (the one added by the script you installed). You can use a single key without modifier, for example /.

  4. To use the script:

    1. Make a selection.
    2. Press the shortcut you assigned to access the "File → Save area" feature, and save the selection to a file.
    3. Repeat for all selections you want to save.

Caveats: The script was last updated in 2007, so will probably not work in a current GIMP 2.8 (I did not test it though). But it's a partial solution that could be fixed. Also the installation instructions mention that if the script does not work, one should convert the source images to a different format. PNG with "Image → Mode → RGB" seems a good choice, as the script uses that format itself to save the selections.

  • 1
    The working solution I have is as follows. Select each of the shapes and save the selection as a path, and name the paths something like "mario_1", "mario_2", "mario_3", "crate_1" etc. You can then export all shapes to separate PNG files using the export_paths_to_png.py Python script for GIMP. The advantage of this is that the selections+names are stored in one file, which is nice for archiving so you can later make some changes and then simply re-export. Another advantage is that if you resize the entire image then the paths are also resized. – BdR Feb 21 '18 at 16:46
  • Right, yours is the best for sure. I added an answer for that as well now. Consider the other answers here only "for the sake of completeness" – somebody, some day, might need one of them instead :) Cheers! – tanius Feb 21 '18 at 21:34
1

A (fully working) solution as developed by the original poster @BdR and mentioned in this comment. It is the most advanced solution for GIMP so far, and fully works in current GIMP 2.8.

  1. Put the script export_paths_to_png.py into folder ~/.gimp-2.8/plug-ins/ (or the equivalent place on Windows / Mac OS X).

  2. Under Linux, make that script executable:

    chmod a+x ~/.gimp-2.8/plug-ins/export_paths_to_png.py
    
  3. Under Linux, make the generated files use / instead of \ as directory separators in the script (lines 29 and 68).

  4. Start GIMP.

  5. Create and name GIMP paths for each selection:

    1. Create the selection.

    2. In the "Paths" dialog, click "Selection to path".

    3. Optional: rename your path. The path name will be used as the filename when saving.

    4. Repeat for all your selections.

  6. Click "Filters → Paths → Export paths to PNG" (the menu item added by the script you installed), fill the dialog and click "OK".

Advantages:

  • Fully works with current GIMP 2.8 (I tested it).
  • You can save all paths together with your image by saving your image in the GIMP native format .xcf. This saves work when you need to make a few changes and re-export the images later.
  • When you resize the image, the paths are resized proportionally so they will still "fit".
  • Paths can overlap without issues, so that a pixel may be exported as part of multiple output images where that is needed.
  • The names, origin coordinates and sizes of all paths are also saved as a text file orgcoords.json alongside in your output directory, for use with your own scripts. If your paths are simple selections with the GIMP "Rectangle Select Tool", all information about them is contained in the .json file this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.