2.48a PNG File Does Not Load (Windows Bug)

Hi All,

I have written a script that loads an image into a texture.

import Blender
from Blender import *
#Make a new texture.
#fileName = 'c:\WINDOWS\Zapotec.bmp'
fileName = 'c:\radial_gradient_75x75.png'
myTex = Texture.New()
 print "trying to load file."
 myImage = Image.Load(fileName)
 print "done trying to load file."
 temp = myImage.getSize()
 imageWidth = temp[0]
 imageHeight = temp[1]
 print "Image Dimensions (" + str(imageWidth) + "," + str(imageHeight) +").
 print ("Unable to load texture file: " + fileName)

It works fine for a BMP file, but it does not work for a PNG file.

You can fetch the PNG file from this thread. Place it in the root of your C: drive on windows.

To observe the failure run the script with the BMP file name uncommented, then run the script with the PNG file uncomment. After each run examine the contents of the console window.

Can anyone tell me why? I am inclined to think that it is a bug in Blender.


Definately a bug on Windows!

On a whim, I renamed the file from “radial_gradient_75x75.png” to “RadialGradient.png”. Then I closed Blender and re-opened it and the script worked.

This explains a lot.

All over the board there are reports of PNG files failing and they seem to be mysterious and often unrepeatble. But I bet it is related to a file naming problem. Somewhere in the PNG load code of blender there is a bug when it encounters certain characters in the file name. I bet the image texture uses the same API call to load images as well.

Dang, I wonder who is going to fix that?

The bug is in your code. You need to make sure you either escape the slashes, or use the raw string format. For the latter do:

fileName = r'c:\radial_gradient_75x75.png' # notice the r before the first quotation mark

As for the answer on your question as to who is going to fix this: you, in your own code :wink:


Yeah, I know about escaping slashes in strings, but thats not it, also, I don’t see an “r” in my code on this current display.

If it is my code, then why would simply renaming the PNG file make it work?

Before the rename I was passing a string of “c:\radial_gradient_75x75.png”.
After the rename I was passing a string of “c:\RadialGradient.png”.

They both have slashes, yet one works and another does not. Also I passed “c:\WINDOWS\Zapotec.bmp” and it worked. No escaped slashes at all.

No, there is something wrong with the PNG import library in Blender. I have noticed this for years but have never been able to pin it down. I have even posted PNG files that don’t work with Blender but were created with photoshop. There is some mysterious bug in the PNG code. And I think I am on to something. It does not have anything to do with escaping the slashes. It has something to do with underscores and numbers. That is where the programmers should look for their bug.

If anyone is on windows, can you try my above test and verify or offer more debunk that the PNG library has an import name bug.

There is no r in your code, you need to add the r to make it a raw string

So no one is even going to try it out on windows.:frowning:

Oh well, I guess we all expect Blender to have bugs…

Post the blend…

@Atom - I tried what you have, found what the problem is and told you what the fix is.

edit: The reason that your C:\RadianBlabla.png works is that \R is not an escape character, whereas \r is…


I think I got it now.
“c:\RadialGraient.png” has no escpae characters.
c:\raidal_gradient_75x75.png" had a single escape character of ‘\r’. This causes Blender to mangle the name into “c:aidal_gradient_75x75.png” which does not exist.

Correct - and by prefixing a string with r makes it raw, thus ignoring escape characters and just understand them as how you read them:

filename = r'C:\radial_gradient.png'