Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

Unable to export jpg after added a new layer

Mon Oct 04, 2021 10:50 am

I did the following

pdb.file_jpeg_save(image, drawable, filename, raw_filename, quality, smoothing, optimize, progressive, ...))
pdb.gimp_image_add_layer(image, imagLayer, 0)
pdb.file_jpeg_save(image, drawable, filename, raw_filename, quality, smoothing, optimize, progressive, ...))

The first save ran without any issue but after I added a layer, I am unable to save it again. I got a message:

Calling error for procedure 'file-jpeg-save':
Procedure 'file-jpeg-save' has been called with an invalid ID for argument 'drawable'. Most likely a plug-in is trying to work on a layer that doesn't exist any longer.

It sounds like I need a new drawable after added a new layer,

But how do I do that?

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 12:06 pm

The file-*-save procedure do not save the image, they save a layer. What happened in your case is likely that the initial layer is no more. If you merged things (gimp_image_flatten(), gimp_image_merge_down() and a few others) the result is a new layer, but the call returns the layer created. So you just have to keep that returned value.

If your image still has several layers at the end, instead of flattening you can also use gimp_layer_new_from_visible() and save the returned layer.

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 12:09 pm

You need to create a new layer first before you add or insert the layer.
Code:
    layer = pdb.gimp_layer_new(image, width, height, type, name, opacity, mode) #Give the layer another name other than drawable

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 12:55 pm

ofnuts wrote:The file-*-save procedure do not save the image, they save a layer. What happened in your case is likely that the initial layer is no more. If you merged things (gimp_image_flatten(), gimp_image_merge_down() and a few others) the result is a new layer, but the call returns the layer created. So you just have to keep that returned value.

If your image still has several layers at the end, instead of flattening you can also use gimp_layer_new_from_visible() and save the returned layer.


Thank you for the response.

I guessed that much. In fact, after the post, I did a pdb.gimp_image_merge_down to get a new layer but pdb.file_jpeg_save doesn't take layers as a parameter.

pdb.file_jpeg_save(image, drawable, filename, raw_filename, quality, smoothing, optimize, progressive, comment, subsmp, baseline, restart, dct)

I tried

pdb.gimp_image_set_active_layer(image, newLayer)

and that doesn't do anything.


So what do I do with the returned layer?

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 2:23 pm

to get the layer again that you merged. Are you using GIMP's PPB or PDB? The procedure Browser is your best friend!
Code:
layer = pdb.gimp_image_get_layer_by_name(image, name)

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 3:01 pm

Pocholo wrote:to get the layer again that you merged. Are you using GIMP's PPB or PDB? The procedure Browser is your best friend!
Code:
layer = pdb.gimp_image_get_layer_by_name(image, name)


I have the new_layer returned by several procedures I tried. That's not the problem.

What do I do with it? pdb.file_jpeg_save doesn't accept layer as a parameter.

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 3:53 pm

ecs1749 wrote:
Pocholo wrote:to get the layer again that you merged. Are you using GIMP's PPB or PDB? The procedure Browser is your best friend!
Code:
layer = pdb.gimp_image_get_layer_by_name(image, name)


I have the new_layer returned by several procedures I tried. That's not the problem.

What do I do with it? pdb.file_jpeg_save doesn't accept layer as a parameter.


Code:
pdb.gimp_image_flatten(image) #returns the layer object which is the result of the flattening and can be used for the save.

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 4:12 pm

Pocholo wrote:
ecs1749 wrote:
Pocholo wrote:to get the layer again that you merged. Are you using GIMP's PPB or PDB? The procedure Browser is your best friend!
Code:
layer = pdb.gimp_image_get_layer_by_name(image, name)


I have the new_layer returned by several procedures I tried. That's not the problem.

What do I do with it? pdb.file_jpeg_save doesn't accept layer as a parameter.


Code:
pdb.gimp_image_flatten(image) #returns the layer object which is the result of the flattening and can be used for the save.


I am having trouble communicating. We are going back and forth without progress. You said " can be used for the save". My point is "pdb.file_jpeg_save doesn't accept layer as a parameter". So, if it doesn't accept layer as a parameter, how does it "used for the save"? It accepts drawable as parameter but not layer.

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 6:59 pm

ecs1749 wrote:
ofnuts wrote:The file-*-save procedure do not save the image, they save a layer. What happened in your case is likely that the initial layer is no more. If you merged things (gimp_image_flatten(), gimp_image_merge_down() and a few others) the result is a new layer, but the call returns the layer created. So you just have to keep that returned value.

If your image still has several layers at the end, instead of flattening you can also use gimp_layer_new_from_visible() and save the returned layer.


Thank you for the response.

I guessed that much. In fact, after the post, I did a pdb.gimp_image_merge_down to get a new layer but pdb.file_jpeg_save doesn't take layers as a parameter.

pdb.file_jpeg_save(image, drawable, filename, raw_filename, quality, smoothing, optimize, progressive, comment, subsmp, baseline, restart, dct)

I tried

pdb.gimp_image_set_active_layer(image, newLayer)

and that doesn't do anything.


So what do I do with the returned layer?


Of course it does. A layer is a drawable (a drawable is a layer, a channel or a mask), so the "drawable" parameter should be the layer returned by pdb.gimp_image_merge_down().

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 7:17 pm

I think you should read the GIMP Python Documentation

Re: Unable to export jpg after added a new layer

Mon Oct 04, 2021 7:49 pm

ofnuts wrote:
ecs1749 wrote:
ofnuts wrote:The file-*-save procedure do not save the image, they save a layer. What happened in your case is likely that the initial layer is no more. If you merged things (gimp_image_flatten(), gimp_image_merge_down() and a few others) the result is a new layer, but the call returns the layer created. So you just have to keep that returned value.

If your image still has several layers at the end, instead of flattening you can also use gimp_layer_new_from_visible() and save the returned layer.


Thank you for the response.

I guessed that much. In fact, after the post, I did a pdb.gimp_image_merge_down to get a new layer but pdb.file_jpeg_save doesn't take layers as a parameter.

pdb.file_jpeg_save(image, drawable, filename, raw_filename, quality, smoothing, optimize, progressive, comment, subsmp, baseline, restart, dct)

I tried

pdb.gimp_image_set_active_layer(image, newLayer)

and that doesn't do anything.


So what do I do with the returned layer?


Of course it does. A layer is a drawable (a drawable is a layer, a channel or a mask), so the "drawable" parameter should be the layer returned by pdb.gimp_image_merge_down().


LOFL! Is it that obvious? Great help! I completed my first plugin. I am very comfortable with Python. I just need to get familiar with GIMP specific details.

Thanks for all the help, folks.

Re: Unable to export jpg after added a new layer

Tue Oct 05, 2021 2:24 am

ecs1749 wrote:I am very comfortable with Python.


Then you should have found this:

>>> layer=gimp.image_list()[0].active_layer
>>> layer.__class__.__mro__
(<type 'gimp.Layer'>, <type 'gimp.Drawable'>, <type 'gimp.Item'>, <type 'object'>)

Re: Unable to export jpg after added a new layer

Tue Oct 05, 2021 2:54 pm

ofnuts wrote:
ecs1749 wrote:I am very comfortable with Python.


Then you should have found this:

>>> layer=gimp.image_list()[0].active_layer
>>> layer.__class__.__mro__
(<type 'gimp.Layer'>, <type 'gimp.Drawable'>, <type 'gimp.Item'>, <type 'object'>)


Alright. You got me stumped. I know syntactically what these statements are but how would they have helped me realize "Drawable" means I can pass the "layer" to it?

For me as a newbie, I thought it's a typo for "Drawtable"!!!

Re: Unable to export jpg after added a new layer

Tue Oct 05, 2021 3:59 pm

When I started scripting and got stuck on the 'eccentricities' of Gimp I found that looking at other people's code was a big help.

Re: Unable to export jpg after added a new layer

Wed Oct 06, 2021 6:50 am

ecs1749 wrote:
ofnuts wrote:
ecs1749 wrote:I am very comfortable with Python.


Then you should have found this:

>>> layer=gimp.image_list()[0].active_layer
>>> layer.__class__.__mro__
(<type 'gimp.Layer'>, <type 'gimp.Drawable'>, <type 'gimp.Item'>, <type 'object'>)


Alright. You got me stumped. I know syntactically what these statements are but how would they have helped me realize "Drawable" means I can pass the "layer" to it?

For me as a newbie, I thought it's a typo for "Drawtable"!!!


Well, if you know Python, you know some OOP. Types within types within types. If I tell you something takes a Collection, but you have a Set, you'll wonder if the Set is a Collection. In any case this isn't civil engineering, the cost of trying something is nearly zero. And since you already had case that worked, did you try to check the type of what was passed in that call?
Post a reply