Pygame application not opening a png - python

For some reason, my python program will not open my file in a directory called imgs in which I have a png file that I wish it to display when it runs.
The imgs directory is in the same location as my python program on my C drive. I have made sure that I am entering the correct names and that there is no / before imgs
It always pops up with the message
pygame.error: Couldn't open imgs/tileGrass1
tileGrass1 being the png
Any suggestions on how to fix this??

Few checks to ensure a pygame program is running well
Ensure You Initialize the pygame library at the beginning of your code
pygame.init()
Ensure Path to your image is correct and that the image exists. Check that the extension is correct (png, jpg, jpeg, etc)
It would also be useful if you share the code to your program so that the exact error is easily noted

Related

tkinter.filedialog.askopenfilename() function: How can I use it?

So currently I'm reading images into my Python project with the cv2.imread() function, but it would be much cooler if I could go to my user interface, which I created with the tkinter module, and choose my picture from my Windows Explorer.
I found this function in Internet. If I use this my Windows explorer opens and I can select my picture, but nothing happens afterwards. I want save the picture in img but it doesnt work. Do you know what the problem is?
path = tkinter.filedialog.askopenfilename(title='select', filetypes='.png')
img = cv2.imread(path, 1)

Downloading an image in Python does not properly display it

I am using PyCharm in order to download an image from a fixed URL
This is the code I'm using in order to do it:
import urllib.request
import random
def download_web_image(url):
name=random.randrange(0,1000)
fullname=str(name)+".jpg"
urllib.request.urlretrieve(url,fullname)
download_web_image('imagizer.imageshack.com/v2/626x626q90/673/MT82dR.jpg')
This is what happens when I double click the downloaded image:
But as you see the image is already downloaded in the Python directory and it is a proper image:
What do I have to do in order for the image to be properly displayed in Pycharm?
This has nothing to do with the fact that the image was downloaded using Python. You will see the same behaviour if you copy an image into your project folder. You can fix it by telling PyCharm about which file types should be displayed as images.
In Settings (File->Settings on Windows) expand Editor then select File Types
Choose Image from the list of Regognized File Types (scroll down...) then make sure that Registered Patterns contains all the file types you want to be displayed as images. If *.jpg is not listed there, add it using the green + on the right.

Can't open files saved using windows long path workaround

I am saving matplotlib figures savefig(filename) from my python script using an absolute path, which is very long.
I had problems with max path length limitation on windows Errno2 no such file or directory, so I started appending \\?\ to the beginning of the path and it seemed to work.
However, some of my saved figures seem to be corrupted, as I try to open them with windows default image viewer and it just closes without showing the image. Some other figures saved by the exactly same procedure worked fine though.
I have noticed that the figures that do not open have the \\?\ appended to the location property in the windows explorer file property dialog:
The ones working does not have it:
EDIT:
I found new information: When the whole absolute path + filename has 260 characters or more (without the \\?\ in the beginning), then I get this corrupted behavior and can't open my file.
EDIT 2:
I tried creating a text file (with open(filename,'wb') instead of saving a matplotlib figure. I get the same \\?\ in the location propert and when I try to open it this is what I get:
So basically the \\?\ allows me to create a figure or textfile, but I cannot open it anyway.
EDIT 3 - well it turns out the files are not corrupted, as I can move them to a folder closer to root and they can be opened with no problems.
Any ideas how to solve this?

How to add images to Pythonista's ui designer

I have been fooling around with python and Pythonista 2.5 on iOS. I currently am far too inexperienced to create good UIs in scripts and need some help using the designer in Pythonista. I currently wish to add an image asset, yet I am only able to use stock images provided, is there any folder path I can follow to add my images to that list, or is there another simple way of doing it?
Keep in mind I have little experience and thank you for any help!
You can store the images files in any folder. A simple way to copy external images would be to first copy it in clipboard (may be from photos) and then save it as png file by running the following script.
(Custom images are currently not supported in the UI editor, You have to load them via code.
https://forum.omz-software.com/topic/3668/images-in-ui-designer
But you can use the [+] button in the code editor (at the top) to view bundled images/textures. (images in the current directory are also shown.)
https://forum.omz-software.com/topic/3760/itunes-file-sharing )
import clipboard
image = clipboard.get_image()
image.show()
image.save('img1.png')
You can also use dropbox or appex scripts to store images.

QImage does not load image even when it exists

def setPixData(self,data):
print(data) #this print the image file name correctly e.g asdfghj.jpg
img=QtGui.QImage(QtCore.QCoreApplication.applicationDirPath ()+"/temp/"+data)
pix=QtGui.QPixmap.fromImage(img)
self.driver_ui.pix.setPixmap(pix)
This loads the image correctly when I run it from the development code.
Once I freeze it with cx_freeze and install it in final destination directory, it does not load the image. Even though the image is in the specified path.
I have tried hardcoding the full path into the frozen version, still no luck.
I've placed a f.write() at every other line of the above code, and they all ran with no error.
I figured the problem is from QImage since it returns true on isNull()
What could be the problem, If QImage is not being added by cx_freeze, it will sure throw and exception and wont run the f.write() on next line.
I am lost here.
NOTE: it's a windows GUI app in python.
You are likely giving it an incorrect file path. The fact that the file exists somewhere doesn't help QImage, if you don't give it a correct file path.

Categories