Skip to content

Commit 18e938d

Browse files
committed
Add Example 2 - imread() and _imwrite()
1 parent 660668f commit 18e938d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

examples/ex02_imread_imwrite.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Import OpenCV
2+
import cv2
3+
4+
# Call `cv2.imread()` to read an image from the MicroPython filesystem, just
5+
# like in any other Python environment! Make sure to copy the image to the
6+
# MicroPython filesystem first, and set the path to the image file as needed
7+
#
8+
# If your board can mount an SD card, you can instead load the image to the SD
9+
# card and change the path to point to the SD card
10+
#
11+
# Note - only BMP and PNG formats are currently supported in MicroPython OpenCV
12+
img = cv2.imread("test_images/sparkfun_logo.png")
13+
14+
# Show the image for a moment
15+
cv2.imshow(display, img)
16+
key = cv2.waitKey(1000)
17+
18+
# Let's modify the image! Here we use `cv2.Canny()` to perform edge detection
19+
# on the image, which is a common operation in computer vision
20+
edges = cv2.Canny(img, 100, 200)
21+
22+
# Display the modified image
23+
cv2.imshow(display, edges)
24+
25+
# Now we'll save the modified image to the MicroPyhton filesystem using
26+
# `cv2.imwrite()`, just like in any other Python environment!
27+
#
28+
# Again, SD cards are supported, just change the path to point to the SD card
29+
#
30+
# Note - only BMP and PNG formats are currently supported in MicroPython OpenCV
31+
success = cv2.imwrite("test_images/sparkfun_logo_edges.png", edges)
32+
33+
# Check if the image was saved successfully
34+
if success:
35+
print("Image saved successfully!")
36+
else:
37+
print("Failed to save the image!")
30.3 KB
Loading

0 commit comments

Comments
 (0)