Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load RGB fits image with GIMP Python API
#1
Hello everyone!

I'm struggling with python API to open a FITS image.

When I open a FITS picture with the menu "file->open" I have a prompt regarding picture composition : "none" or "NAXIS=3, NAXIS3=2,...,4". And when I select "NAXIS..." the image is correctly loaded into GIMP in RGB.

But when I use python pdb.file_fits_load() command, I have no options in order to specify none or NAXIS. thus my image is always in gray scale.

Also, with python command, I do not have multiple images as I have when I select "none" on the HMI (=> 3 images, one per color).

What should I do to have the image in RGB loaded by python command?

Thank you for your help!
Best regards,
Damien.
Reply
#2
As a work around you can try to call file_fits_load with the named parameter run_mode=RUN_INTERACTIVE this may make it show the dialog where you can select parameters manually.
Reply
#3
I played around with this and notice the function file_fits_load returns one image but it creates 3 in memory which you can use compose them as RGB.
So i got this to work


def afittest(image,layer,file):
    image = pdb.file_fits_load(file,file)
    #images set in memory but not returned because return only one but we have 3 new images
    l = gimp.image_list()
    redImage = l[len(l)-2]
    greenImage = l[len(l)-3]
    blueImage = l[len(l)-4]
    #compose them
    new_image = pdb.plug_in_compose(redImage,layer,greenImage,blueImage,redImage,"RGB")
    pdb.gimp_display_new(new_image)
Reply
#4
(10-08-2024, 07:29 PM)ttt Wrote: I played around with this and notice the function file_fits_load returns one image but it creates 3 in memory which you can use compose them as RGB.
So i got this to work


def afittest(image,layer,file):
    image = pdb.file_fits_load(file,file)
    #images set in memory but not returned because return only one but we have 3 new images
    l = gimp.image_list()
    redImage = l[len(l)-2]
    greenImage = l[len(l)-3]
    blueImage = l[len(l)-4]
    #compose them
    new_image = pdb.plug_in_compose(redImage,layer,greenImage,blueImage,redImage,"RGB")
    pdb.gimp_display_new(new_image)

In Python you can use negative indexes to access list elements from the end: l[-1] is the last element, for instance. You can even use slices, so for instance: imgR,imgG,imgB=gimp.image_list()[-4:-1].

Otherwise, yes, the Images dockable dialog is your best friend, because it shows all images, even those that have no associated view/display, and there is even a button to create a view for the latter. It is also a nice way to check for lingering images after a script.
Reply
#5
thanks, more info by the master hehe.
Reply
#6
(10-08-2024, 07:47 PM)Ofnuts Wrote:
(10-08-2024, 07:29 PM)ttt Wrote: I played around with this and notice the function file_fits_load returns one image but it creates 3 in memory which you can use compose them as RGB.
So i got this to work


def afittest(image,layer,file):
    image = pdb.file_fits_load(file,file)
    #images set in memory but not returned because return only one but we have 3 new images
    l = gimp.image_list()
    redImage = l[len(l)-2]
    greenImage = l[len(l)-3]
    blueImage = l[len(l)-4]
    #compose them
    new_image = pdb.plug_in_compose(redImage,layer,greenImage,blueImage,redImage,"RGB")
    pdb.gimp_display_new(new_image)

In Python you can use negative indexes to access list elements from the end: l[-1] is the last element, for instance. You can even use slices, so for instance: imgR,imgG,imgB=gimp.image_list()[-4:-1].

Otherwise, yes, the Images dockable dialog is your best friend, because it shows all images, even those that have no associated view/display, and there is even a button to create a view for the latter. It is also a nice way to check for lingering images after a script.

Thank you very much ttt and Ofnuts!!

I was not able to use slices, I have an error:
➤> imgR,imgG,imgB=gimp.image_list()[-4:-1]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: need more than 2 values to unpack

But using gimp.image_list()[-1] , gimp.image_list()[-2] and gimp.image_list()[-3] works like a charm.

With the example of ttt, I now have my RGB picture!

Thanks again!
Best regads,
Damien.
Reply
#7
(10-08-2024, 09:09 PM)damien.f Wrote:
(10-08-2024, 07:47 PM)Ofnuts Wrote:
(10-08-2024, 07:29 PM)ttt Wrote: I played around with this and notice the function file_fits_load returns one image but it creates 3 in memory which you can use compose them as RGB.
So i got this to work


def afittest(image,layer,file):
    image = pdb.file_fits_load(file,file)
    #images set in memory but not returned because return only one but we have 3 new images
    l = gimp.image_list()
    redImage = l[len(l)-2]
    greenImage = l[len(l)-3]
    blueImage = l[len(l)-4]
    #compose them
    new_image = pdb.plug_in_compose(redImage,layer,greenImage,blueImage,redImage,"RGB")
    pdb.gimp_display_new(new_image)

In Python you can use negative indexes to access list elements from the end: l[-1] is the last element, for instance. You can even use slices, so for instance: imgR,imgG,imgB=gimp.image_list()[-4:-1].

Otherwise, yes, the Images dockable dialog is your best friend, because it shows all images, even those that have no associated view/display, and there is even a button to create a view for the latter. It is also a nice way to check for lingering images after a script.

Thank you very much ttt and Ofnuts!!

I was not able to use slices, I have an error:
➤> imgR,imgG,imgB=gimp.image_list()[-4:-1]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: need more than 2 values to unpack

But using gimp.image_list()[-1] , gimp.image_list()[-2] and gimp.image_list()[-3] works like a charm.

With the example of ttt, I now have my RGB picture!

Thanks again!
Best regads,
Damien.

This is because as used in ttt's example, your images are indices -4 to -2 (which implies there are at least 4 images in the list). If you want the last three the slice is [-3:] (from -3 to whatever the end is).
Reply
#8
(10-09-2024, 09:02 AM)Ofnuts Wrote: This is because as used in ttt's example, your images are indices -4 to -2 (which implies there are at least 4 images in the list). If you want the last three the slice is [-3:] (from -3 to whatever the end is).

Indeed [-3:] get me the full array Smile

Thanks again!
Reply


Forum Jump: