Hello everyone I'm trying to open a image that i have downloaded through a link. I searched on the site and found something very useful and implemented that into my code.
*if* __name__ == "__main__":
import urllib
droste = urllib.urlopen("http://is.gd/cHqT")
with open("droste.png", "wb") as imgFile:
imgFile.write(droste.read())
print "Got it!"
droste = Image.open("droste.png")
while droste:
droste.show()
droste = reveal(droste)
if droste:
open("droste.png", "wb").write(droste)
droste = Image.open("droste.png")
The error occurs on the 7th line "droste = Image.open("droste.png")". I'm getting a IOError: cannot identify image file. I know the image has been downloaded because the codes runs great until that particular line and the line print "Got it!" actually confirms that its been downloaded. I don't know if I need to specify the path of the image file in the parameter in the open instead the of image name. Or maybe I need to check the path of the file. Please help.
Your code is functional. The problem is how you are running it. You mentioned in your comments that you are using PythonAnywhere. PythonAnywhere is not set up to do anything graphical. It will download the image into the correct directory, but PIL will not function correctly with PythonAnywhere.
Try the following code to test this.
import urllib
if __name__ == "__main__":
droste = urllib.urlopen("http://is.gd/cHqT")
with open("droste.png", "wb") as imgFile:
imgFile.write(droste.read())
print "Got it!"
print "Now lets test if it really exists..."
try:
with open("droste.png", "rb") as imgFile:
pass
print "There were no errors so the file exists"
except:
print "ERROR: image was not saved properly!"
If you start up a BASH session with PythonAnywhere, you will see that the file droste.png exists, and you can download it to your computer and view it. Your program is OK.
If you really want to use your program, or get serious about python programming. You really should install Python locally to your computer. If you want to keep your code in the cloud, then use dropbox, github, or bitbucket. PythonAnywhere has uses, but normally you will just want to have python on your computer.
Related
So, I'm working on a personal project. I made two python scripts using youtube-dl to download a song and a thumbnail, respectively. The script for the song downloads the song as an .mp3 file with a custom name (designated by an argument). The script for the thumbnail downloads the .webp file of the thumbnail with a custom name (designated by an argument) and converts it to png and then jpg. I put these two into functions and in different folders, like this.
yt2mp3 -- youtubeMP3.py
__main__.py
thumbnails.py -- __main__.py
downloadimg.py
The full scripts can be found at this github repo: https://github.com/ignition-ctrl/yt2mp3
The code is not pretty but it works. However, the issue is this. I have a function in my .bashrc called downloadmusic(). It'll take two arguments, the link and the custom filename and run the python script youtubeMP3.py with the arguments. The youtubeMP3.py has the function download_music() and within that function it has a reference to the function download_thumbnail from downloadimg.py. My problem is that from my terminal, I can see that it runs download_thumbnail twice. Once when starting the script, and then after running download_music(). I only want it to run after download_music(). The code is supposed to only run in this code.
determiner = input("Do you want to download the thumbnail?")
if determiner == "yes" or "y" or "Yes":
downloadimg.download_thumbnail(str(ytname), str(filename))
else:
exit(0)
That's the only reference to the download_thumbnail, but I can see the terminal output from download_thumbnail() before the print statements I put in download_music(). I also get two copies of the jpg file that comes from download_thumbnail(). I've been scratching my head about this all day. If anyone could help, I'd appreciate it.
First time download_thumbnail() is called when you import the module. It tries to run
try:
download_thumbnail(sys.argv[1], sys.argv[2])
except IndexError:
raise NameError("Please provide a link and your desired filename")
you can wrap this inside an if statement like this
if __name__ == '__main__':
try:
download_thumbnail(sys.argv[1], sys.argv[2])
except IndexError:
raise NameError("Please provide a link and your desired filename")try:
download_thumbnail(sys.argv[1], sys.argv[2])
so that this block will run only when the file is executed directly, not when importing.
I'm a begginer, and I wanted to learn OpenCV in python, so I installed it by PIP
pip install opencv-contrib-python
The first program I made using the libary dosen't worked, it displays an error that was raised by imread() method:
[Errno 2] No such file or directory
I checked a couple of times, and both file and directory exists!
After some time I tried running a diffrent program I made before, that reads files by read() method, and it showed up the same error, it was working before I installed cv2, and i haven't changed it by this time.
Maybe there's something in the code so there are both of these:
import cv2 as cv
img = cv.imread('/Desktop/uczemsiemprogramowacniepaczec/zdjencia/artest.jpg')
cv.imshow('window', img)
cv.waitKey(0)
#this is the first program, it is supposed to show artest.jpg in a diffrent window
the second one:
import keyboard
import time
print("five seconds till spam")
time.sleep(5)
P = open("C:/Users/tymon/Desktop/E/wpisza.txt")
T = P.readlines()
P.close()
for i in T:
keyboard.press_and_release('enter')
time.sleep(1)
keyboard.write(i.replace("\n", ""))
keyboard.press_and_release('enter')
#this was supposed to send an entire wpisza.png file in the chat
Finally, sorry for possible bad english (it is not my native language)
I searched for the solution to this problem everywhere on the Internet, and I haven't found any, so I hope someone knows this...
Okay, so first, I would check the path again, maybe left click on your image, see properties and check the path there. Also, try to use relative paths. Your path is always FROM your file. So if your path is /Project/main.py and your image is in /Project/test_images/t1.jpeg your code will be:
import cv2 as cv
img = cv.imread('./test_images/t1.jpeg')
cv.imshow('window', img)
cv.waitKey(0)
So I've recently gotten into mapmaking with Python using matplotlib and Basemap. For some reason my code breaks when I go to execute m.readshapefile() because it can't find the .shp.
I downloaded the .zip for this and put it on my desktop at C:\Users\mattd\Desktop\pop\ne_110m_populated_places. I put
m.readshapefile('C:\Users\mattd\Desktop\pop\ne_110m_populated_places',
'populated_places')
and it can't breaks because it can't find the file.
In the link you attached, there was no file named populated_places
I did find a ne_110m_populated_places.shp. Also, it's also good practice to name your variables what they are and do sanity checks in your own code instead of relying on libraries to do that for you.
from os import path
shape_dir = 'C:\Users\mattd\Desktop\pop\ne_110m_populated_places'
shape_file = 'ne_110m_populated_places'
shape_file_full = shape_file + '.shp'
# check if we provided valid paths to our file and directory
if path.isdir(shape_dir) is False:
print ("%s does not exist!" % (shape_dir))
quit()
if path.isfile(shape_file_fill) is False:
print ("%s does not exist" % (shape_file_full))
quit()
# if it makes it here then you know it's an issue with the library
base_map = Basemap(...)
base_map.readshapefile(shape_dir, shape_file)
I want to open .wav file in default program. But it doesn´t work. This is my code:
audiofile=(myFile[index]+".wav") # I have all files in array (without ".wav")
try:
try:
os.system('xdg-open audiofile')
except:
os.system('start audiofile')
except:
print "error"
I don´t get any error, but it doesn´t work. How can I solve it? Thank you.
You aren't substituting the name of the audio file into your OS commands, so it can't possibly work.
You'd need something like:
os.system('xdg-open ' + audiofile)
This assumes that you have a default application associated with .wav files, which of course you can test by trying your command manually.
You might also want to check the return value of os.system for an error code, rather than relying on exceptions.
First of all, you should fill the variable audiofile into the command, not the string 'audiofile' itself
os.system('xdg-open %s' % audiofile)
Second,
os.system will NOT throw an exception when xdg-open or start doesn't exist in system.
Determine the type of system first by platform.system
>>> import platform
>>> platform.system()
'Linux'
Here is my python program:
#!/usr/bin/env/ python
import cv
capture1=cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_WIDTH,320)
cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_HEIGHT,240)
while 1:
cam1=cv.QueryFrame(capture1);
cv.SaveImage("camera.jpg",cam1);
cv.WaitKey(11)
print 'Done!'
On crontab:
#reboot sudo python /home/program.py >/home/result.txt
But its not saving the image.Definitely I have done something wrong! I got the similar problem when I was reading image cv2.imread("image.jpg") but it was returning None so I added full path to the image /home/image.jpg.That problem was solved!.Is cron not getting camera feed?
Thanx for help!
First and foremost, make sure the application succeeds communicating with the camera:
import cv
capture1 = cv.CaptureFromCAM(0)
if not capture1 :
print "!!! Failed to open a camera interface"
# Ideally, exit the application.
cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_WIDTH,320)
cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_HEIGHT,240)
Remember to test if the frame was successfully retrieved from the camera:
while 1:
frame = cv.QueryFrame(capture1);
if not frame:
print "!!! Failed to retrieve frame"
break
# Right now, your code overwrites the same file at every iteration of the loop.
# It might be better to add a BREAK at the end for testing purposes.
cv.SaveImage("camera.jpg", frame);
# There's no need to call WaitKey() if the image is not displayed on a window.
#cv.WaitKey(11)
print 'Done!'
SaveImage() will fail when the application doesn't have permission to write files in the directory from where it was executed. Since crontab is responsible to call your application, I imagine that it does that from a directory where the user doesn't have the right permissions. If this is the case, I strongly suggest you to feed SaveImage() with the full path to the file.
The problem was in cv.ShowImage or cv2.imshow.When I commented this line everything worked fine! Previously the program got stuck at this this line.(while execution through cron).[That I was writing in my original program]