Gimp-Forum.net
GIF frames deleting script. - 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)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: GIF frames deleting script. (/Thread-GIF-frames-deleting-script)



GIF frames deleting script. - rey - 04-02-2023

I understand it's not polite to bluntly ask for anybody to make a script that i need, so hope asomebody can give hints on commands i must use to try to achieve my goal.

The goal is a a script that makes automatic deletion of GIF frames(layers) based on 2 parameters, and then exports file in original location adding some "copy"/"-01" to name. The deletion logic should work like "delete X frames after each Y frames".
For example:
 = GIF with 22 frames, parameters are X=2 Y=3, after processing there will be: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 = GIF with 22 frames, parameters are X=1 Y=4, after processing there will be: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
[crossed frames are deleted]

Hope for your help.


RE: GIF frames deleting script. - rey - 04-03-2023

I've got this script as start:

Code:
image = gimp.image_list()[0]
for (index,layer) in enumerate(image.layers):
 if not index%3:
   image.remove_layer(layer)

but it only does like 30% of goal.


RE: GIF frames deleting script. - Ofnuts - 04-03-2023

Pretty close. Something like this:

1. Depends if you work top down of bottom up
Code:
layers=image.layers[:] # Make a copy of the list
layers.reverse() # if working bottom up

2. Select layers
Code:
keep=3
delete=2
stride=keep+delete
deleted_layers=[layer for i in range(keep,len(layers),stride) for layer in layers[i:i+delete]]

At that point you can inspect the contents of delete_layers to make sure they are the ones

3. Delete them
Code:
for l in deleted_layers:
   image.remove_layer(l)

Coded in slo-mo (so to speak) for better readability, a tattooed Python coder would have done a one-liner Cool


RE: GIF frames deleting script. - rey - 04-03-2023

@Ofnuts thank you for code. Py scripts need some special variable setting?(data type or smth)

Code:
Error: eval: unbound variable: layers=image.layers[:]



RE: GIF frames deleting script. - Ofnuts - 04-03-2023

(04-03-2023, 05:18 PM)rey Wrote: @Ofnuts thank you for code. Py scripts need some special variable setting?(data type or smth)

Code:
Error: eval: unbound variable: layers=image.layers[:]

No, but it needs your image = gimp.image_list()[0] first :-)


RE: GIF frames deleting script. - teapot - 04-04-2023

Hi Ofnuts, I'm confused by this line of your suggested code:

(04-03-2023, 02:41 PM)Ofnuts Wrote:
Code:
layers=image.layers[:] # Make a copy of the list

I get that it makes a copy of a list but could it just be:

Code:
layers=image.layers

I thought the ‘layers’ attribute of an image or layer group builds a new Python list and copies into it the IDs of the layers so that gives the plugin it's own private list anyway?


RE: GIF frames deleting script. - Ofnuts - 04-04-2023

(04-04-2023, 03:30 AM)teapot Wrote: Hi Ofnuts, I'm confused by this line of your suggested code:

(04-03-2023, 02:41 PM)Ofnuts Wrote:
Code:
layers=image.layers[:] # Make a copy of the list

I get that it makes a copy of a list but could it just be:

Code:
layers=image.layers

I thought the ‘layers’ attribute of an image or layer group builds a new Python list and copies into it the IDs of the layers so that gives the plugin it's own private list anyway?

Maybe, and maybe not. The indexation operator [] is just a call to a __getitem__() method, so image.layers could be live-wired into the image layers, retrieving layers on the fly when __getitem__() is called. In other words it's indexable but not necessarily a list. And in that case iterating the collection while deleting stuff in it is not going to be pretty.

Of course, in this particular case, it is a list, so yes, the copy isn't entirely necessary.


RE: GIF frames deleting script. - teapot - 04-05-2023

(04-04-2023, 07:26 AM)Ofnuts Wrote:
(04-04-2023, 03:30 AM)teapot Wrote: Hi Ofnuts, I'm confused by this line of your suggested code:

(04-03-2023, 02:41 PM)Ofnuts Wrote:
Code:
layers=image.layers[:] # Make a copy of the list

I get that it makes a copy of a list but could it just be:

Code:
layers=image.layers

I thought the ‘layers’ attribute of an image or layer group builds a new Python list and copies into it the IDs of the layers so that gives the plugin it's own private list anyway?

Maybe, and maybe not. The indexation operator [] is just a call to a __getitem__() method, so image.layers could be live-wired into the image layers, retrieving layers on the fly when __getitem__() is called. In other words it's indexable but not necessarily a list. And in that case iterating the collection while deleting stuff in it is not going to be pretty.

Of course, in this particular case, it is a list, so yes, the copy isn't entirely necessary.

Thank you for your explanation.