Welcome to Raspberry Pi 5 repository
2. Cut the image
2. Cut the image
Image cutting first reads the image, and then obtains the pixel area in the array. In the following code, select the shape area X: 300-500 Y: 500-700. Note that the image size is 800*800, so the selected area should not exceed this resolution.
The code is running on jupyter lab
ximport cv2img = cv2.imread('yahboom.jpg', 1)dst = img[500:700,300:500] #Select the rectangular area X: 300-500 Y: 500-700#cv2.imshow('image',dst)#cv2.waitKey(0)
The following will display two compressed image comparison displays in the jupyterLab control.
xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75):return bytes(cv2.imencode('.jpg', value)[1])
Compare the before and after images below:
xxxxxxxxxximport ipywidgets.widgets as widgetsimage_widget1 = widgets.Image(format='jpg', )image_widget2 = widgets.Image(format='jpg', )# display the container in this cell's outputdisplay(image_widget1)display(image_widget2)img1 = cv2.imread('yahboom.jpg',1)image_widget1.value = bgr8_to_jpeg(img1) #Original imageimage_widget2.value = bgr8_to_jpeg(dst) #Cut image
