Welcome to DOFBOT-Ultra repository
7. Picture Cut
7. Picture Cut
Image cutting first reads the image, then obtains the pixel region from the array. In the code below, the rectangular region is X: 300-500 Y: 500-700. Note that the image size is 800*800, so the selected area should not exceed this resolution.
Code path:
~/dofbot_opencv/scripts/2.Transform/02_cut_pic.ipynb
ximport cv2img = cv2.imread('yahboom.jpg', 1)dst = img[500:700,300:500] # Here select rectangular area X: 300-500 Y: 500-700#cv2.imshow('image',dst)#cv2.waitKey(0)Below, the JupyterLab widget displays a comparison of the two compressed images
xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])Below is the before and after image comparison:
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) # cropped imageAfter the program runs, you can see that part of the image has been cropped out.
