I want source code for creating video from given images using opencv and python2.7. I try this code but it gives me an error of NoneType object has no attribute 'shape'
import cv2
img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg')
img3 = cv2.imread('3.jpg')
height , width , layers = img1.shape
video = cv2.VideoWriter('video.avi',-1,1,(width,height))
video.write(img1)
video.write(img2)
video.write(img3)
cv2.destroyAllWindows()
video.release()
This is a common pitfall when assigning file-path that is relative. You are probably running the script with a command such as:
python somefolder/myscript.py
And you think that because 1.jpg and the rest of the pictures are in the same folder somefolder that you've done well. That is not the case. Python understands your desire for 1.jpg as being in the same folder that you used when running the script, so it searches it not in somefolder but in the parent of somefolder, because that's where you were when you ran the script.
Solutions:
Run the script only from the folder it's in (which isn't very useful)
Make the path absolute - replace 1.jpg with /full/path/to/1.jpg (and do that for all the images of course)
Make the path relative to the script, using the __file__ property and os.path module: img1_path = os.path.join(os.path.dirname(__file__), '1.jpg')
Related
Im back with another probably stupid question!, my new issue is the following:
im trying to save a picture from my webcam into a specific folder using OpenCV imwrite,
its not working obviously here is my code:
import cv2 as cv
cam = cv.VideoCapture(0)
s, img = cam.read()
if s:
path = r"\imgtest\selfietest.jpg"
cv.imwrite(path, img)
Any suggestions for edits or fixes?, i've tried copying the file using shutil and moving it using the same module, i also tried to use the OS module to move it but it threw an "Access Denied" error so i would prefer to not need to grant the application admin rights every time i launched it thanks in advance!!
((ALSO ASSUME I KNOW NOTHING ABOUT PYTHON))
Use a full path, forward slashes, and r'' to pass a raw path. cv2.imwrite(r"D:/....jpg", img)
I'm trying to recreate this project on my local machine. It's designed to run on Google Colab and I've recreated it there, and it works just fine. I want to try running it on my local machine now, so I installed all the required packages, anaconda, Juypter Notebook etc.
When I come to the part where I process the images:
# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
img = cv2.imread(path) # Reads image and returns np.array
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Converts into the corret colorspace (GRAY)
img = cv2.resize(img, (320, 120)) # Reduce image size so training can be faster
X.append(img)
#Processing label in image path
category = path.split("/")[3]
label = int(category.split("_")[0][1])
y.append(label)
It throws the following error:
IndexError: list index out of range
The code has not been changed, for the most part, and the dataset is the same. The only difference is I'm running locally vs google colab. I searched online and someone said do len(path) to verify that (in my case) it goes up to [3], which it does (its size 33).
Code has changed here:
I did not use this line, as I'm not using google colab:
from google.colab import files
The "files" is used in this part of code:
# We need to get all the paths for the images to later load them
imagepaths = []
# Go through all the files and subdirectories inside a folder and save path to images inside list
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
path = os.path.join(root, name)
if path.endswith("png"): # We want only the images
imagepaths.append(path)
print(len(imagepaths)) # If > 0, then a PNG image was loaded
On my local machine, I removed the from google.colab... line, and ran everything else normally. The keyword files is used in the code snippet above, however when running it I was thrown no errors. **NOTE len(path) on Jupyter shows 33, len(path) on Google shows 16..?**
Does anyone have any idea what the issue could be? I don't think it came from removing that one line of code. If it did, what do you suggest I do to fix it?
Your local machine is running on Windows while the colab runs on linux and the path separators are different for both.
Now you need to replace
category = path.split("/")[3]
with
category = path.split("\\")[2]
And your code should work.
I want to know how to import files.jpg in another folder to use in my program.
from myfolder import picture.jpg
import cv2
img = cv2.imread("picture.jpg", 1)
cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
You only use import to interact with python modules (.py files).
To open an image you just use appropriate image path in cv.open
For example:
import cv2
img = cv2.imread("myfolder/picture.jpg", 1)
cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
You do not need to import the .jpg file since it is not a python module. You can access the file directly from the cv2.imread() method by providing the absolute file path instead of just the name of the file.
For example, if picture.jpg was inside Documents/Project, then the code would be as follows:
img = cv2.imread("Documents/Project/picture.jpg", 1)
As for finding the absolute path of a file, the process is different for different operating systems. Here is how to find it on a mac, and here's the same for windows.
Hope this helps!
If I run this:
import qrcode
img = qrcode.make('Some data here')
I don't know where it saves the actual image. I've tried CD'ing to a path and run the Python code, I have had the script in a directory. But when running the code above it doesn't create a QR image file in the directory. Where is it saved?!
I've tried running qr "Some data here" > test.png from the command-line which works perfectly. But not the module itself for some reason.
The make function doesn't output an image. For that you have to do something like this after running that function:
img = qr.make_image(fill_color="black", back_color="white")
Read the docs.
I have written a small script to convert a RGB image to grey.
When I run the script in terminal form current dir (python3 image.py), it works perfectly fine.
But when I rum it from a dir lower like python3 proc/image.py it creates a result image with 0 bytes size.
here is the code of image.py
import os
import numpy as np
import cv2
def sw(name):
dir_path = os.path.dirname(os.path.realpath(__file__))
img_path = os.path.join(dir_path, name)
print(img_path)
img = cv2.imread(name, 0)
rslt_path = os.path.join(dir_path, "schwarn_g.jpeg")
print(rslt_path)
cv2.imwrite(rslt_path, img)
return img
if __name__ == "__main__":
sw('schwarn.jpeg')
Can somebody explain this behavior?
Thanks a lot!
You are opening the image that you want to work on with the following line:
img = cv2.imread(name, 0)
And since your script always calls the function with this line:
sw('schwarn.jpeg')
you are always trying to open the img: schwarn.jpg from the same directory that you are running the script. All your os.path joining is affecting where the grayscale image is outputted to - not where the original image is read from.
This means that since the program is trying to open the img in the same directory that you are running the script, the img can only be loaded in when you are running the script in the same directory as the img. Therefore, when you try and run the script in a different directory, the img cannot be read in and the resulting greyscale image has no contents...