I understand and thank you for it.
I don’t want to argue I just want to figure out what going on in your case, but now we have too many variables in the example, I want to isolate your issue.
My logic here:
- We have custom render_script, custom material, manual image loader - too many places where something could go wrong.
- I want to reduce the count of these places.
- My first step is removing the manual loader. I just assign texture for the model in the editor. Result: I can’t reproduce the issue. It’s strange, If this is sorting or render pipeline problem it should be the same behavior even if we do so.
- Ok, that means that problem somewhere in the loading process. I am trying to replace your loader with loader I know. Result: everything works fine with premultiply_alpha flag (and we have an issue without this flag).
- Hmm, Ok, by default png doesn’t have premultiplied alpha. But let’s check your image:
5.1 Your image:
image.png.zip (13.9 KB)
You have pallets image, let’s convert it to RGBA:
from PIL import Image
import numpy
import sys
def main(finput, foutput):
image = Image.open(finput)
image.convert("RGBA").save("rgba.png")
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
python3 convert_to_rgba.py image.png rgba.png
Result:
rgba.png.zip (32.7 KB)
Let’s check our example with this new rgba.png
-> the same issue. Great! But now it’s easier to analyze our source image.
5.2 Let’s check our image colours.
from PIL import Image
import numpy
import sys
def is_premultiplied(array):
for element in array:
for color in element:
if color[3] != 255:
if color[0] > color[3] or color[1] > color[3] or color[2] > color[3]:
return False
return True
def main(finput):
image = Image.open(finput)
array = numpy.array(image)
if is_premultiplied(array):
print("Premultiplied!")
return
print("Non-premultiplied!")
if __name__ == "__main__":
main(sys.argv[1])
check_is_pemultiplyed.py rgba.png
Result:
Non-premultiplied!
- Ok, Let’s premultiply colors then:
from PIL import Image
import numpy
import sys
def is_premultiplied(array):
for element in array:
for color in element:
if color[3] != 255:
if color[0] > color[3] or color[1] > color[3] or color[2] > color[3]:
return False
return True
def main(finput, output):
image = Image.open(finput)
array = numpy.array(image)
if is_premultiplied(array):
print("Premultiplied!")
return
print("Non-premultiplied!")
for element in array:
for color in element:
color[0] = color[0] * (color[3] / 255.)
color[1] = color[1] * (color[3] / 255.)
color[2] = color[2] * (color[3] / 255.)
Image.fromarray(array).save(output)
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
premultiply.py rgba.png premult.png
premult.png.zip (32.3 KB)
Result: everything works fine with premult.png
and false
flag
Checking:
check_is_pemultiplyed.py premult.png
Premultiplied!
If I made a mistake somewhere in my logic, pls give me know.
Of course, this is still strange behavior but should we spend our time if we know that everything works fine with the right input file? (of course, if I didn’t miss something in my investigation)