Bird
Raised Fist0
Computer Visionml~10 mins

Writing/saving images in Computer Vision - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save an image using OpenCV.

Computer Vision
import cv2
image = cv2.imread('input.jpg')
cv2.[1]('output.jpg', image)
Drag options to blanks, or click blank then click option'
Aresize
Bimshow
Cimwrite
Dimread
Attempts:
3 left
💡 Hint
Common Mistakes
Using cv2.imshow instead of cv2.imwrite.
Trying to read the image again instead of saving.
2fill in blank
medium

Complete the code to save a grayscale image using OpenCV.

Computer Vision
import cv2
image = cv2.imread('input.jpg', [1])
cv2.imwrite('gray_output.jpg', image)
Drag options to blanks, or click blank then click option'
Acv2.IMREAD_UNCHANGED
Bcv2.IMREAD_GRAYSCALE
Ccv2.IMREAD_COLOR
Dcv2.COLOR_BGR2GRAY
Attempts:
3 left
💡 Hint
Common Mistakes
Using color conversion codes instead of read flags.
Using IMREAD_COLOR which reads a color image.
3fill in blank
hard

Fix the error in saving an image with compression parameters.

Computer Vision
import cv2
image = cv2.imread('input.jpg')
cv2.imwrite('compressed.jpg', image, [1])
Drag options to blanks, or click blank then click option'
A[cv2.IMWRITE_JPEG_QUALITY, 90]
Bcv2.IMWRITE_JPEG_QUALITY=90
C{cv2.IMWRITE_JPEG_QUALITY: 90}
D(cv2.IMWRITE_JPEG_QUALITY, 90)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing parameters as a dictionary or tuple.
Using assignment syntax inside the function call.
4fill in blank
hard

Fill both blanks to save a PNG image with compression level 3.

Computer Vision
import cv2
image = cv2.imread('input.png')
cv2.imwrite('output.png', image, [[1], [2]])
Drag options to blanks, or click blank then click option'
Acv2.IMWRITE_PNG_COMPRESSION
Bcv2.IMWRITE_JPEG_QUALITY
C3
D90
Attempts:
3 left
💡 Hint
Common Mistakes
Using JPEG flags for PNG images.
Passing quality value instead of compression level.
5fill in blank
hard

Fill all three blanks to save a JPEG image with quality 85 and grayscale read.

Computer Vision
import cv2
image = cv2.imread('input.jpg', [1])
cv2.imwrite('output.jpg', image, [[2], [3]])
Drag options to blanks, or click blank then click option'
Acv2.IMREAD_GRAYSCALE
Bcv2.IMWRITE_JPEG_QUALITY
C85
Dcv2.IMREAD_COLOR
Attempts:
3 left
💡 Hint
Common Mistakes
Using color read flag instead of grayscale.
Mixing PNG compression flags with JPEG quality.

Practice

(1/5)
1. What does the function cv2.imwrite() do in computer vision?
easy
A. Converts an image to grayscale
B. Reads an image from a file
C. Displays an image in a window
D. Saves an image to a file on disk

Solution

  1. Step 1: Understand the purpose of cv2.imwrite()

    This function is used to save image data to a file on your computer.
  2. Step 2: Differentiate from other OpenCV functions

    Functions like cv2.imread() read images, and cv2.imshow() display images, but cv2.imwrite() specifically saves images.
  3. Final Answer:

    Saves an image to a file on disk -> Option D
  4. Quick Check:

    cv2.imwrite() = Save image [OK]
Hint: Remember 'imwrite' means 'image write' to file [OK]
Common Mistakes:
  • Confusing imwrite with imread
  • Thinking it displays images
  • Assuming it converts image formats automatically
2. Which of the following is the correct syntax to save an image stored in variable img to a file named output.jpg using OpenCV?
easy
A. cv2.save('output.jpg', img)
B. cv2.imwrite('output.jpg', img)
C. cv2.write('output.jpg', img)
D. cv2.saveImage('output.jpg', img)

Solution

  1. Step 1: Recall the correct OpenCV function name

    The function to save images is cv2.imwrite(), not save or write.
  2. Step 2: Check the argument order

    The first argument is the filename as a string, the second is the image variable.
  3. Final Answer:

    cv2.imwrite('output.jpg', img) -> Option B
  4. Quick Check:

    Correct function and argument order = cv2.imwrite('output.jpg', img) [OK]
Hint: Use 'imwrite' with filename first, then image [OK]
Common Mistakes:
  • Using non-existent functions like cv2.save
  • Swapping argument order
  • Missing quotes around filename
3. What will be the output of the following code snippet?
import cv2
img = cv2.imread('input.png')
success = cv2.imwrite('saved.png', img)
print(success)
medium
A. True if image saved successfully, False otherwise
B. The saved image data
C. The filename 'saved.png'
D. An error message

Solution

  1. Step 1: Understand cv2.imwrite() return value

    This function returns a boolean: True if saving worked, False if it failed.
  2. Step 2: Analyze the print statement

    The code prints the boolean stored in success, so output is True or False.
  3. Final Answer:

    True if image saved successfully, False otherwise -> Option A
  4. Quick Check:

    imwrite() returns success boolean [OK]
Hint: imwrite returns True/False for success [OK]
Common Mistakes:
  • Expecting image data as output
  • Thinking it prints filename
  • Assuming it throws error on failure
4. Identify the error in this code snippet that tries to save an image:
import cv2
img = cv2.imread('photo.jpg')
cv2.imwrite(img, 'output.jpg')
medium
A. cv2.imread cannot read jpg files
B. cv2.imwrite requires file extension in filename
C. Arguments to cv2.imwrite are in wrong order
D. cv2.imwrite cannot save images

Solution

  1. Step 1: Check cv2.imwrite() argument order

    The first argument must be the filename string, second the image data.
  2. Step 2: Identify the mistake in the code

    The code passes img first and filename second, which is incorrect.
  3. Final Answer:

    Arguments to cv2.imwrite are in wrong order -> Option C
  4. Quick Check:

    Filename first, image second in imwrite() [OK]
Hint: Filename must be first argument in imwrite() [OK]
Common Mistakes:
  • Swapping filename and image arguments
  • Assuming imread can't read jpg
  • Thinking imwrite can't save images
5. You want to save a grayscale image stored in gray_img as a PNG file and ensure the save was successful. Which code snippet correctly does this?
hard
A. if cv2.imwrite('gray.png', gray_img): print('Saved successfully') else: print('Save failed')
B. cv2.imwrite(gray_img, 'gray.png') and print('Saved successfully')
C. cv2.imwrite('gray.png', gray_img) print('Saved successfully')
D. if cv2.imwrite('gray.png'): print('Saved successfully')

Solution

  1. Step 1: Use cv2.imwrite() with correct arguments

    The first argument is the filename string, second is the image data variable.
  2. Step 2: Check the return value to confirm success

    Use an if statement to check if cv2.imwrite() returns True, then print success message; else print failure.
  3. Final Answer:

    if cv2.imwrite('gray.png', gray_img): print('Saved successfully') else: print('Save failed') -> Option A
  4. Quick Check:

    Check imwrite() return before confirming save [OK]
Hint: Check imwrite() return value to confirm save success [OK]
Common Mistakes:
  • Swapping arguments in imwrite
  • Not checking if save succeeded
  • Passing wrong argument types