Welcome to DOFBOT Pro repository
7. Picture cut
Picture cut
First read the image, and then get the pixel area in the array.
In the following code, select the shape area X: 300-500 Y: 500-700. Because the image size is 800*800, the selected area should not exceed this resolution.
Code path:
x~/dofbot_pro/dofbot_opencv/scripts/2.Transform/02_cut_pic.ipynb
ximport cv2img = cv2.imread('yahboom.jpg', 1)dst = img[500:700,300:500] #Select a rectangular area X:300-500 Y:500-700#cv2.imshow('image',dst)
#cv2.waitKey(0)The following will show the comparison of the two compressed images in the jupyterLab.
xxxxxxxxxx#bgr8转jpeg格式import enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])Below are the before and after images:
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) #The original imageimage_widget2.value = bgr8_to_jpeg(dst) #The image after cuttingAfter the program is run, we can see that some parts have been cut out, as shown in the following figure.
