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...
Related
I want to convert an image RGB dataset to Grayscale dataset using pillow. I want to write a script that takes in the dataset path and converts all images one by one into grayscale. At the end I want to save this script, and want to run this script on the server to avoid the copying of huge data to the server.
Probably this code would work for you?
Code:
import os
from PIL import Image
dir = '/content/pizza_steak/test/pizza'
for i in range(len(os.listdir(dir))):
# directory where images are stored
dir = '/content/pizza_steak/test/steak'
# get the file name
file_name = os.listdir(dir)[i]
# creating a final path
final_path = dir + '/' + file_name
# convet and save the image
Image.open(final_path).convert('L').save(f"/content/meow/gray{file_name}")
# uncomment this is you want to delete the file
# os.remove(final_path)
I am trying to crop high-resolution images to something more manageable. I am trying to read in a directory rather than individual images and save the new cropped images in another directory. I would like to make all the output images as .png as I have in my code.
import cv2
path = './imgs2/P5.png'
img= cv2.imread (path)
imgcropped = img [1:400, 1:400]
cv2.imwrite ('./imgs/P5-cropped', imgcropped)
Any help with this problem is appreciated.
Here's what I use in this case:
import os
import cv2
path = 'path/to/image/dir/'
dest_path = 'path/to/destination/'
for f in os.listdir(path):
image = cv2.imread(os.path.join(path, f))
imgcropped = image[1:400, 1:400]
cv2.imwrite(os.path.join(dest_path, f), imgcropped)
Assuming that:
the images in path are already .png
path contains only the images you want to convert
os.listdir will give you the names of the files inside your origin dir (including extension), which you can use in imwrite to save the image in your destination dir with the same filename.
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!
I have the following line of code:
img = cv.imread("c:/users/admin/downloads/sack.jpg", 1)
It is not reading anything into img. img is showing None. sack.jpg exists, and is an image file.
import os
import cv2
image_path = r"c:/users/admin/downloads/sack.jpg"
assert os.path.isfile(image_path)
img = cv2.imread(image_path, 1)
Either the assert will fail, or the read will succeed, or the file is not a valid image.
Notice the r preceding the path.
Also notice cv2, not cv.
EDITED
This worked for me on WINDOWS:
import cv2
path = r'c:/users/admin/downloads/sack.jpg'
img = cv2.imread(path)
cv2.imshow('imgTitle',img)
cv2.waitKey()
you can check how cv2.imread works here.
Note that:
It's (cv2) not cv.
Add cv.waitKey() so your image won't open and close very fast.
Anyway using the manual path writing is not very practical in the
real time applications, insted of that you can try using libraries
like os and pathlib, it's very recommended too.
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')