Shivam Mitra Shivam Mitra - 1 year ago 51
Python Question

Not able to save captured image in right directory

I am working on a project related to image processing. I would like to capture an image from a webcam and want to display it on webpage. I am using django framework for web stuff.

Program to capture image from webcam in

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
import cv2

def home(request):
return render(request,'detect/home.html',{})

def get_image(camera):
retval, im =
return im

def webcam():
camera_port = 0
ramp_frames = 30
camera = cv2.VideoCapture(camera_port)
for i in xrange(ramp_frames):
temp = get_image(camera)
print("Taking image...")
camera_capture = get_image(camera)
file = "/detect/static/test_image.png"
cv2.imwrite(file, camera_capture)

def display(request):
return render(request,'detect/display.html',{})

Here is a screenshot of my directory structure:
enter image description here

If I don't mention any path and only include the name of the image file(file = "test_image.png"), the image gets saved in /moody/project.
I would like to save image in /moody/project/detect/static/.

Answer Source

Use the fullpath from / or the relative path from your current dir, using a dot ('.') before the first '/' in the filepath.

file = "./detect/static/test_image.png"