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

Unable to load an image as layer

Sat Oct 02, 2021 1:02 am

This must be simple. I am simply trying to load my signature.png file to a new layer.

filename=r"signature.jpg"
layer = pdb.gimp_file_load_layer(image, filename)

and I tried:

filename=r"signature.png"
image = pdb.file_png_load(filename, filename)

and neither of them work. The new layer doesn't show up.

Re: Unable to load an image as layer

Sat Oct 02, 2021 1:39 am

I go to file and then open as layers and then navigate to the folder that holds the file and select the image file. Is that what you are doing?

Re: Unable to load an image as layer

Sat Oct 02, 2021 1:53 am

ecs1749 wrote:This must be simple. I am simply trying to load my signature.png file to a new layer.

filename=r"signature.jpg"
layer = pdb.gimp_file_load_layer(image, filename)

and I tried:

filename=r"signature.png"
image = pdb.file_png_load(filename, filename)

and neither of them work. The new layer doesn't show up.


Unless the file is in Gimp's current directory(*), there must be a complete path in the file name.

(*) If you start Gimp by clicking an icon this is very unlikely

Re: Unable to load an image as layer

Sat Oct 02, 2021 4:01 am

ecs1749 wrote:This must be simple. I am simply trying to load my signature.png file to a new layer.

filename=r"signature.jpg"
layer = pdb.gimp_file_load_layer(image, filename)

and I tried:

filename=r"signature.png"
image = pdb.file_png_load(filename, filename)

and neither of them work. The new layer doesn't show up.

Dont understand what you are trying to do
Have you tried this?
Image

Re: Unable to load an image as layer

Sat Oct 02, 2021 4:36 am

@ ecs1749

Keep going developing your plugin, just throwing a what-if into the hat.

Importing as a layer will plant the layer centered in the base image. That might be ok and work with your work flow. Might not be so good with diferent size images or if the sig is needed in a specific location. More code in your plugin.

For any single image an easy alternative is make your sig file into a greyscale brush. And use it as a stamp.

sig.jpg
sig.jpg (158.21 KiB) Viewed 1193 times


If the intention is for batch purposes, then there are already scripts to do this or use the Gimp plugin BIMP.

Re: Unable to load an image as layer

Sat Oct 02, 2021 10:59 am

Assuming this is what you need, this will create a user dialog and you can find the image using a directory option. The image is place in the center of the canvas


Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *

def open_an_image(filename):   

    img = pdb.gimp_image_new(1920, 1080, RGB)
    imagLayer = pdb.gimp_file_load_layer(img, filename)
    pdb.gimp_image_add_layer(img, imagLayer, 0)
   
    #Center the image to the canvas
    imagLayer.set_offsets((img.width - imagLayer.width) / 2, (img.height - imagLayer.height) / 2)
   
    pdb.gimp_display_new(img)
   
register(
    "open_an_image",
    "Open any image",           #Short description
    "Open any type of image",   #Long description
    "Your name",
    "Your name",
    "2021",
    "Open my favorite image",  #This is how you want it to appear in GIMP menu
    "",
    [   
    (PF_FILENAME, "filename", "Image:", None),  #Directory option
    ],
    [],
    open_an_image, menu="<Image>/Python-Fu/Open my favorite image"), #This is how you want it to appear in GIMP menu
main()





Or you can center the image and make the image as high of the canvas

Code:
#!/usr/bin/env python     
# -*- coding: utf-8 -*-   

#Above codings
#1. Python Source Code Encodings
#2. Most popular Python Source Code Encodings (Use both)

from gimpfu import *     #This willgives you all of the constants, the procedural database (pdb), and gimp (Everything that belong to GIMP)

def open_an_image(filename):  #Name of main function and variables

    img = pdb.gimp_image_new(1920, 1080, RGB)      #This function opens a new image with the size given and the image mode colors
    imagLayer = pdb.gimp_file_load_layer(img, filename)    #This will open or load image from directory
    pdb.gimp_image_add_layer(img, imagLayer, 0)            #Add or insert the layer for the image that it will open       
    #Center the image and make it big
    factor = min (float(img.width) / imagLayer.width, float(img.height) / imagLayer.height)
    imagLayer.scale(int(imagLayer.width * factor), int(imagLayer.height * factor))
    imagLayer.set_offsets((img.width - imagLayer.width) / 2, (img.height - imagLayer.height) / 2)
     
    pdb.gimp_display_new(img)
   
register(
    "open_an_image",
    "Open any image",           #Short description
    "Open any type of image",   #Long description
    "Your name",
    "Your name",
    "2021",
    "Open my favorite image",  #This is how you want it to appear in GIMP menu
    "",
    [   
    (PF_FILENAME, "filename", "Image:", None),  #Directory option
    ],
    [],
    open_an_image, menu="<Image>/Python-Fu/Open my favorite image"), #This is how you want it to appear in GIMP menu
main()

Re: Unable to load an image as layer

Sat Oct 02, 2021 8:47 pm

Wow! So many people offered to help. Appreciate it. I feel bad I wasn't clearer about what my question is. My bad.

What I need to do really is very simple. Yes, I am trying to do with plug-in script the exact same thing as "File -> Open as layers" from the GIMP UI. When I do that, you are prompted for the file, and the png file gets loaded to a new layer, and your current layer becomes that newly loaded png. Very simple. Not so with the Python script. I can do color adjustment, sharping, and a bunch of other plug-ins - but I am stumped on this one.

filename=r"c:\Users\xxxx\Pictures\signature.png"
image = pdb.file_png_load(filename, filename)

I think the file is loaded because if I do this immediately after

name=image.name
width=image.width
height=image.height
pdb.gimp_message("%s: width=%d, height=%d\n"%(name,width,height))

I see the proper information displayed at the error console. But there is no new layer. I don't see any new layer like I normally get from the GIMP UI.

I wish there is a easy way for me to post a screen shot. Sallyanne, was that a screen shot you posted? How do you do that?

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:01 pm

I am going to try Pocholo's method:

imagLayer = pdb.gimp_file_load_layer(img, filename) #This will open or load image from directory
pdb.gimp_image_add_layer(img, imagLayer, 0) #Add or insert the layer for the image that it will open

Will let you know.

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:08 pm

For screen shots in Gimp first I print screen from the keyboard. Alt print screen can also be used. Then I go to edit and paste as and choose new image.

ecs1749 wrote:Wow! Sallyanne, was that a screen shot you posted? How do you do that?

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:10 pm

Yes!!! That works. So, I have to explicitly add the new layer.

Thanks, everyone.

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:14 pm

zeuspaul wrote:For screen shots in Gimp first I print screen from the keyboard. Alt print screen can also be used. Then I go to edit and paste as and choose new image.

ecs1749 wrote:Wow! Sallyanne, was that a screen shot you posted? How do you do that?


I know how to capture the screen but where do I paste it to? "edit and paste as"???

I try Ctrl-V and that doesn't work

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:21 pm

ecs1749 wrote:I wish there is a easy way for me to post a screen shot. Sallyanne, was that a screen shot you posted? How do you do that?

There is a way to do it in gimp, file > create > screenshot But I always do it this way - I find it quicker.
Print the screen and paste in gimp as a new image then crop to what you want to show. If you are wanting to show a drop down menu you need to click over it and hold your cursor over the area while you take your screen shot.
You may have to have an image open as some things are 'unavailable/greyed out' when there is no image.

rich did a screen shot too and probably with a better answer. Theme of his gimp is just a different colour.

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:25 pm

sallyanne wrote:
ecs1749 wrote:I wish there is a easy way for me to post a screen shot. Sallyanne, was that a screen shot you posted? How do you do that?

There is a way to do it in gimp, file > create > screenshot But I always do it this way - I find it quicker.
Print the screen and paste in gimp as a new image then crop to what you want to show. If you are wanting to show a drop down menu you need to click over it and hold your cursor over the area while you take your screen shot.
You may have to have an image open as some things are 'unavailable/greyed out' when there is no image.


That's all good but how do I paste it to this place? Do I have to create an image file and upload the image file? I hope not.

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:29 pm

After you have it you export it as an image and then post it as an image. By the way the red square I did on it I did after I had the picture in gimp - Just used a rectangle select and stroked that selection. you can add anything you need to on your image before you export it, then post.

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:47 pm

Sorry ecs. After you have the screenshot in your clipboard, To paste that into gimp first you go to "Menu in Gimp"
Edit> Paste As> New Image
Image
To do with keys it has the shortcul there as well
Also, Have a look at this viewtopic.php?f=23&t=7695

I'm guessing you haven't posted a picture on the message boards yet? If you want to, click on the icon in between the speech icon and the web icon, hold your cursor over it and it will say upload file. Click on it and you will get another window where you have to browse for your file and then add it. - It needs to upload and then you submit it. It will only give a web address for now until you have posted your post. Then you will see your image. By the way the maximum size file is 5242880 bytes.

Re: Unable to load an image as layer

Sat Oct 02, 2021 9:53 pm

ecs1749 wrote:Yes!!! That works. So, I have to explicitly add the new layer.

Thanks, everyone.

I'm glad I was be able to help! Keep learning, don't give up.
Make sure you add to the thread title "Solved".

Re: Unable to load an image as layer

Sat Oct 02, 2021 10:10 pm

sallyanne wrote:Sorry ecs. After you have the screenshot in your clipboard, To paste that into gimp first you go to "Menu in Gimp"
Edit> Paste As> New Image
[ Image ]
To do with keys it has the shortcul there as well
Also, Have a look at this viewtopic.php?f=23&t=7695

I'm guessing you haven't posted a picture on the message boards yet? If you want to, click on the icon in between the speech icon and the web icon, hold your cursor over it and it will say upload file. Click on it and you will get another window where you have to browse for your file and then add it. - It needs to upload and then you submit it. It will only give a web address for now until you have posted your post. Then you will see your image. By the way the maximum size file is 5242880 bytes.


So, you do have to go through a file. No clipboard. Ok. Thanks,
Post a reply