Python Fu - Math operations failing - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Python Fu - Math operations failing (/Thread-Python-Fu-Math-operations-failing) |
Python Fu - Math operations failing - BaconWizard17 - 01-31-2023 Hi all, I'm setting up a script, and part of it is to scale the image to a max size of 256 for one type of texture and a max size of 128 for a different type of texture. That aspect isn't working, so I isolated it into a separate script, and it's still not working. I've managed to figured out where the issue is happening, but not why. Here's the isolated code: Code: #!/usr/bin/env python The issue is the line where the scaleFactor is being determined. For whatever reason, scaleFactor = int(maxSize) / int(currentWidth) is giving a value of 0, even though maxSize is nonzero (defined as 256) and currentWidth is also nonzero (512 in the image I was testing). I used pdb.gimp_message() to verify the values right before the operation, and they're what I expect. Maybe I'm doing the division wrong? I also tried converting maxSize and currentWidth to integers (with int()), but that didn't change anything either. Any thoughts would be appreciated. RE: Python Fu - Math operations failing - Kevin - 01-31-2023 scaleFactor = int(maxSize) / int(currentWidth) is returning zero because you're doing integer arithmetic. You need to make one or both into float values to get a fractional scaleFactor Code: > > > int(512) / int(258) RE: Python Fu - Math operations failing - BaconWizard17 - 02-01-2023 (01-31-2023, 09:56 AM)Kevin Wrote: scaleFactor = int(maxSize) / int(currentWidth) is returning zero because you're doing integer arithmetic. You need to make one or both into float values to get a fractional scaleFactor The int() was something that I added later as an attempt to fix the issue, in case the original values were being output as strings. When I first posted my message, I had int() around those values, but later edited it out because the original code just said scaleFactor = maxSize / currentWidth. I did test with one of the values defined as a float() instead, and it worked. So are the values produced by image.width/image.height strings? Is that why it was originally failing? Edit: I realized that I can check the documentation for this question, and that that operation produces an integer. RE: Python Fu - Math operations failing - Ofnuts - 02-01-2023 (02-01-2023, 12:19 AM)BaconWizard17 Wrote: I did test with one of the values defined as a float() instead, and it worked. So are the values produced by image.width/image.height strings? Is that why it was originally failing? In Gimp most coordinates values are integers (because they point at a specific pixel). The notable exception is paths where everything is a float. Be also aware that the values you receive as input parameters from things like PF_SLIDER are always floats, and may have to be cast to ints before use. RE: Python Fu - Math operations failing - BaconWizard17 - 02-01-2023 (02-01-2023, 02:48 PM)Ofnuts Wrote:(02-01-2023, 12:19 AM)BaconWizard17 Wrote: I did test with one of the values defined as a float() instead, and it worked. So are the values produced by image.width/image.height strings? Is that why it was originally failing? Thanks for the tip! You all have been really helpful with me trying to figure out my issues. |