Question: I'm looking to save/write photos that are not blurry. How would one go about combining the two codes below?

```
import cv2

image = cv2.imread('./facesData/ID.jpg')
cv2.Laplacian(image, cv2.CV_64F).var()

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.2, 8)
    for x,y,w,h in faces:

        sampleN = sampleN + 1
        cv2.imwrite("./facesData/ID." + str(sampleN) + ".jpg", gray[y:y+h, x:x+w])
        cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
        cv2.waitKey(2)

    cv2.imshow('user', img)
    cv2.waitKey(1)

    if sampleN > 20:
        break

cap.release()
cv2.destroyAllWindows()

```

Answer: 
This moves your blurry images into a separate folder ([code source](https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/)).

```
# import the necessary packages
import cv2
import os
from pathlib import Path

#%% Setup paths
script_dir = str(Path(__file__).parents[0]) # path this script is running in
source_images_dir = os.path.join(script_dir, 'images')

#%%
def variance_of_laplacian(image):
    # compute the Laplacian of the image and then return the focus
    # measure, which is simply the variance of the Laplacian
    return cv2.Laplacian(image, cv2.CV_64F).var()

#%% loop over the input images
threshold = 200

for file_name in os.listdir(source_images_dir):
    image_path = os.path.join(source_images_dir, file_name)
    image = cv2.imread(image_path) # load the image
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to grayscale
    fm = variance_of_laplacian(image) # calculate blur
    text = "Not Blurry"

    # if the focus measure is less than the supplied threshold,
    # then the image should be considered "blurry"
    if fm >= threshold:
        text = "Blurry"

    #%% Once ready to move clear images, uncomment this section
    else:
        focused_images_path = os.path.join(script_dir,
                                           'blurry_images',
                                           file_name)
        os.rename(image_path, focused_images_path)

    #%% Comment out the following section when ready to batch move your images
    cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
    cv2.imshow("Image", image) # Press ENTER to cycle through images
    key = cv2.waitKey(0)

```

To use it setup your folder structure like:
[![enter image description here](https://i.stack.imgur.com/zxOZ3.png)](https://i.stack.imgur.com/zxOZ3.png)
```
ret, frame = cap.read()
if face_extractor(frame) is not None:
    count = count+1;
    face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

    file_name_path = './facesData/ID.' +str(id)+ '.' +str(count)+ '.' + '.jpg'
    if cv2.Laplacian(face, cv2.CV_64F).var() >500:
        cv2.imwrite(file_name_path, face)

    else:
        count -= 1

     cv2.imshow('user', frame)

else:
    pass

if cv2.waitKey(1) == 13 or count == 20

```