Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GIF frames deleting script.
#1
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.
Reply
#2
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.
Reply
#3
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
Reply
#4
@Ofnuts thank you for code. Py scripts need some special variable setting?(data type or smth)

Code:
Error: eval: unbound variable: layers=image.layers[:]
Reply
#5
(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 :-)
Reply
#6
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?
Reply
#7
(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.
Reply
#8
(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.
Reply
#9
Ofnuts1. 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


Hi, I'm very new to scripting in Gimp and this is my first question, so I hope the age of the thread isn't an issue. Now, on to the question:

When I enter this code in the Python-Fu console window I do not see any list. Is the list that is referred to here supposed to be visible to the user? If so where or how can I see it?
Reply
#10
(12-16-2024, 01:31 PM)Qhuenta Wrote: Ofnuts1. 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


Hi, I'm very new to scripting in Gimp and this is my first question, so I hope the age of the thread isn't an issue. Now, on to the question:

When I enter this code in the Python-Fu console window I do not see any list. Is the list that is referred to here supposed to be visible to the user? If so where or how can I see it?

In the Python console window you have to obtain the image. If there is only one image loaded in Gimp:
Code:
➤> image=gimp.image_list()[0]
If there are several images, you can find the image ID in the title bar (usually followed by .0, for instance 11.0 (the 0 is a view ID, not relevant here))
Code:
➤> image=[img for img in gimp.image_list() if img.ID==11][0]
In slow-mo: [img for img in gimp.image_list() if img.ID==11] constructs  a list of all images with the required id (only one...), and the [0] extracts the first and only image form the list.

Do not use this technique in your scripts, it is just a useful cop-out in the Python console. In scripts you are normally given the image to work on.

Then with the image, image.layers is the list of the top-level layers and groups:

Code:
➤> image.layers
[<gimp.Layer 'Layer copy'>, <gimp.Layer 'Layer'>, <gimp.Layer 'Pasted Layer'>]
and:
Code:
➤> image.layers[:]   # copy of the list
[<gimp.Layer 'Layer copy'>, <gimp.Layer 'Layer'>, <gimp.Layer 'Pasted Layer'>]
➤> image.layers[::-1]   # reversed copy of the list
[<gimp.Layer 'Pasted Layer'>, <gimp.Layer 'Layer'>, <gimp.Layer 'Layer copy'>]
Reply


Forum Jump: