It is currently 24 Jan 2022, 23:01

• Featuring Members' Images •
Could be related to 'Need simple help please'
tim
tim
skinnyhouse
animicule
skinnyhouse

GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  🡆 Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Need simple help please

Share your Python Fu Scripts/plug-ins for GIMP here
n3306tx
Forum Member
59th Place
15 Post(s)

Need simple help please

Post#1 by n3306tx » 08 Jan 2022, 23:26


For some reason, I just cannot grasp coding what should be simple tasks in Python. In the example below, I simply want to check to see if my image is grayscale and if so, convert it to RGB. Here is the code, but it doesn't work. The remainder of the code I have written below this works just fine with no problems of the plugin running.

Code: Select all

#! /usr/bin/env python2
from gimpfu import *
def python_Test(img, layer):
    #Check if image is grayscale mode
    is_gray = pdb.gimp_drawable_is_gray(img)
    if (is_gray==TRUE):
        pdb.gimp_image_convert_rgb(img)
...
(if the 3 lines above are deleted, the remainder of code from here on works fine which is to duplicate the layer, rename it and make it the active layer.)
	newLayer = pdb.gimp_layer_copy(layer, pdb.gimp_drawable_has_alpha(layer)) #Duplicate layer
	pdb.gimp_image_insert_layer(img, newLayer, None, -1)
	pdb.gimp_drawable_desaturate(newLayer,3)	#Desaturate duplicate layer
	active_layer_name = pdb.gimp_item_get_name(layer) + " copy"
	active_layer = newLayer
...

I would really appreciate any help because it bugs me that I can't grasp something that should be simple. Thank you.
GIMP Learn rocks...

tim
GL Blogger
1st Place
5775 Post(s)

Post#2 by tim » 09 Jan 2022, 00:16


I don't see anything wrong with code,
except for the fact that if the 3 lines are included then the whole bottom code is part of indentation that belongs to the if statement
so it'll only work if the image is gray because if it's not gray it'll skip everything because of indentation.
aka Tin
World Food Program | Good Deeds Forum

         

Steve Reid
Script Coder
24th Place
172 Post(s)

Post#3 by Steve Reid » 09 Jan 2022, 07:10


You can't use a mix of tabs and spaces for indentation, it has to be one or the other

https://www.python.org/dev/peps/pep-000 ... -or-spaces

You also need to change

is_gray = pdb.gimp_drawable_is_gray(img)
to
is_gray = pdb.gimp_drawable_is_gray(layer)

regards steve
GIMP Learn rocks...

n3306tx
Forum Member
59th Place
15 Post(s)

Post#4 by n3306tx » 18 Jan 2022, 01:03


Thanks Tin and Steve. It seemed to be a combination of things you both mentioned. I finally got this to work with the following code. I really appreciate the help on this.

Code: Select all

#Check if image is grayscale mode
	is_gray = pdb.gimp_drawable_is_gray(layer)
	if (is_gray == TRUE):
		pdb.gimp_image_convert_rgb(img)
	#end if
GIMP Learn rocks...


GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  🡆 Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Post Reply New Topic
  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 8 guests