Welcome to DOFBOT-Ultra repository
3. Image Writing
Image Writing
Function method: cv2.imwrite('yahboom1.jpg', img).
The first parameter is the saved file name, the second parameter is the image to be saved.
Below we demonstrate the image writing method. First, read an image yahboom.jpg, then write yahboom1.jpg.
Code path:
~/dofbot_opencv/scripts/1.OpenCV_basic/02_write_pic.ipynb
xxxxxxxxxximport cv2# 1 file reading 2 format parsing 3 data decoding 4 data loadingimg = cv2.imread('yahboom.jpg', 1)# cv2.imshow('yahboom, img) # see note belowcv2.imwrite('yahboom1.jpg', img) # 1 name 2 dataThe cv2.imshow('yahboom, img) function cannot be executed in JupyterLab. If you need to use this line to display the read image, you need to execute the python file via command line: python3 XX.py
xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])xxxxxxxxxximport ipywidgets.widgets as widgetsimage_widget = widgets.Image(format='jpg', width=320, height=240)display(image_widget)img = cv2.imread('yahboom1.jpg',1)image_widget.value = bgr8_to_jpeg(img)After the code block runs, you can see that yahboom.jpg has been written to yahboom1.jpg.
