Searching for python script islands to layers - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +--- Thread: Searching for python script islands to layers (/Thread-Searching-for-python-script-islands-to-layers) |
Searching for python script islands to layers - mich_lloid - 01-22-2018 Hi, I'm searching for a plugin that converts a layer's non-transparent items/islands to multiple layers with each layer an item/island. Please help, I am non-programmer and tried very simple python script but am very novice. Actions/steps should be: 1. make sure tranparency is already mask layer, else make mask layer with right-click Layer > "Add Layer Mask..." > "Layer's alpha channel" 2. make duplicate of current layer as backup named temp_layer. 3. select temp_layer 4. go to every pixel in every row in temp_layer, if pixel's transparency is 0 (mask layer pixel is black) than do nothing if transparency is not 0 (mask layer pixel is not black) select "fuzzy select tool" on position of that pixel with settings "Threshhold" at 254.9 and checked off "antialiasing", "feather edges", "select transparent areas" and "sample merged". 5. duplicate temp_layer and call it item### or island### for example island001. 6. crop to selection in island001 layer (Layer > Crop to Selection). 7. go back to temp_layer and make selection fully transparent in mask layer by filling selection with black color 8. update temp_layer and repeat from step 3 or 4 to 8 untill there are no more islands in temp layer and temp layer is fully transparent (mask layer is fully black). As I said I'm no programmer so I hope someone will make this or point me to script that already does this, I haven't found one on the internet, only for photoshop but I don't have photoshop. Here's link for photoshop version: https://graphicdesign.stackexchange.com/questions/82043/turning-one-layer-with-many-separate-items-into-multiple-layers-batch Thanks in advance RE: Searching for python script islands to layers - Espermaschine - 01-22-2018 I think there is a G'MIC filter for that. Investigating now. EDIT: yes its under Arrays & Tiles -> Extract Objects RE: Searching for python script islands to layers - mich_lloid - 01-22-2018 Thank you, I didn't know this existed in G'MIC, but it doesn't work with masked layers, which is very important to me. As a python-fu exercise, how would you do the steps in the first post? RE: Searching for python script islands to layers - Ofnuts - 01-22-2018 See ofn-extract-objects here: https://sourceforge.net/projects/gimp-tools/files/scripts/ Basically it makes an alpha-selection, and converts it to a path. That path has exactly as many strokes as there are "islands" in the selection, so the script breaks the multi-stroke path into as many single-stroke paths, and iterates them to make a selection restricted to one islands, and copies it to a new layer. For a crash course on paths: https://www.gimp-forum.net/Thread-Paths-Basics RE: Searching for python script islands to layers - rich2005 - 01-22-2018 (01-22-2018, 03:56 PM)mich_lloid Wrote: Thank you, I didn't know this existed in G'MIC, but it doesn't work with masked layers, which is very important to me. Why is a layer mask so important? Is it just because it is part of that long complicated PS script? What is the desired end result other than each item/island on its own layer? Do you want to keep as a stack of layers or do you eventually want each as a separate image. I can think of other ways for each of those options RE: Searching for python script islands to layers - mich_lloid - 01-22-2018 (01-22-2018, 04:26 PM)rich2005 Wrote:(01-22-2018, 03:56 PM)mich_lloid Wrote: Thank you, I didn't know this existed in G'MIC, but it doesn't work with masked layers, which is very important to me. Because G'MIC's version destroys colors of transparent regions while Ofnuts's version does not. I paint transparent regions around objects/islands or use G'MIC's solidify to give neighboring transparent pixels of the objects/islands the same color as the border of the islands which is very important so that sprites don't "bleed" or create artifacts in 2d/3d applications for html games or godot engine games or any other game engine. RE: Searching for python script islands to layers - Ofnuts - 01-22-2018 (01-22-2018, 05:14 PM)mich_lloid Wrote: Because G'MIC's version destroys colors of transparent regions while Ofnuts's version does not. Should I take this as a hint that the plugin worked for your purpose? RE: Searching for python script islands to layers - mich_lloid - 01-22-2018 Not 100%, there's a lot of seemingly empty layers, probably because sprites have some holes in them and are counted as objects/islands. Anyways, this is my first python script ever so bear with me, in python-fu console: Code: def crop_selection_to_new_layer(image): I select a sprite, then run last line, then select next sprite, then run last line again etc... This gives accurate results but tedious and time consumming so I need help with: -step 4 of first post: how do I go over each pixel and select pixel with non-zero mask layer with "fuzzy select tool" with said parameters. -step 7 and 8 of first post RE: Searching for python script islands to layers - Ofnuts - 01-22-2018 4) "num_channels, pixel = pdb.gimp_drawable_get_pixel(drawable, x_coord, y_coord)" will give you all four channels (so includes alpha). You can get all the pixels in an array using pixels regions. See this for an example (no, you don't need to use numpy). 7) The mask is a drawable and you can get it with "mask = pdb.gimp_layer_get_mask(layer)". Once you have it, you can fill the selection with "pdb.gimp_edit_fill(drawable, fill_type)" 8) "mean, std_dev, median, pixels, count, percentile = pdb.gimp_histogram(drawable, channel, start_range, end_range)" will tell you a lot of things about the contents of the selection. However:
RE: Searching for python script islands to layers - mich_lloid - 01-22-2018 Wow, that's a lot to take in, I'm really a novice at this, a lot of studying to do... Meanwhile I made a small script that works by going every 10 pixels, use color picker, if alpha is bigger than 0, use fuzzy select tool etc. I also have to apply the mask layer for this script to work, thankfully the color data of the transparent pixels is preserved. Code: img = gimp.image_list()[0] It works better than your version as it takes into account holes in sprites, but the downside the script is kinda slow. Further questions: - -How do I rename copied layers to something like <original_layer>### for example layer001, layer002, etc. |