您好,登錄后才能下訂單哦!
小編給大家分享一下Python圖片處理之圖片裁剪的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
首先會(huì)吧?
1.有張照片
這是網(wǎng)上隨便找的一張照片,自行保存測(cè)試
2.看看照片
運(yùn)行代碼,其中show_img函數(shù)是展示照片
3.選擇角點(diǎn)
按照左上,右上,右下,左下的順序選擇四個(gè)角點(diǎn)
如果擔(dān)心自己選不好,可以直接去除我代碼里的points的注釋,那是我自己用的原版
4.最終結(jié)果
import 沒什么好說的
#如果python沒有安裝cv2,那么就安裝python-opencv就好 import cv2 as cv import numpy as np
獲取圖片的長(zhǎng)寬
#輸入cv.imread后的圖片,通過點(diǎn)擊四個(gè)點(diǎn)選擇要裁剪的部分 def get_window_size(src, bound=600): h,w = src.shape[0], src.shape[1] if h > w: h, w = bound, int(w*bound/h) else: h, w = int(h*bound/w), bound return (h, w)
通過鼠標(biāo)獲取圖片的坐標(biāo)點(diǎn),順序是左上,右上,右下,左下
class Indexer: def __init__(self, bound=4): self.id = 0 self.bound = bound def get_id(self): self.id = (self.id + 1) return (self.id) def on_EVENT_LBUTTONDOWN(event, x, y, flags, param): if event == cv.EVENT_LBUTTONDOWN: img = param['src'] win_name = param['window'] indexer = param['indexer'] points = param['points'] curr_id = indexer.get_id() points.append((x, y)) print('第{}個(gè)頂點(diǎn): ({},{})'.format(curr_id, x, y)) cv.circle(img, (x, y), 10, (0, 0, 255), thickness=2) cv.putText( img, str(curr_id), # 文字 (x, y), # 坐標(biāo) cv.FONT_HERSHEY_PLAIN, 5, # 字號(hào) (0, 0, 255), # 字體顏色 thickness=2 # 粗細(xì) ) cv.imshow(win_name, img) #輸入cv.imread后的圖片,通過點(diǎn)擊四個(gè)點(diǎn)選擇要裁剪的部分 def get_points(src): points = [] indexer = Indexer() h, w=get_window_size(src) win_name = 'get_points' cv.namedWindow(win_name, cv.WINDOW_NORMAL) cv.resizeWindow(win_name, width=w, height=h) cv.imshow(win_name, src) cv.setMouseCallback(win_name, on_EVENT_LBUTTONDOWN, param={'src': src, 'window': win_name, 'indexer': indexer, 'points': points}) cv.waitKey(0) cv.destroyAllWindows() if len(points)>4: return points[0:4] # print(points) # points=[(2, 14), (90, 50), (87, 194), (1, 204)] return points #輸入cv.imread后的圖片,展示圖片長(zhǎng)什么樣 def show_img(src): win_name = 'show_img' h, w=get_window_size(src) cv.namedWindow(win_name, cv.WINDOW_NORMAL) cv.resizeWindow(win_name, width=w, height=h) cv.imshow(win_name, src) cv.waitKey(0) cv.destroyAllWindows()
將圖片截取,并按照指定的長(zhǎng)寬比恢復(fù)成矩形
def photo_cut_restore(src,points,H,W): target_points = [(0, 0), (W, 0), (W, H), (0, H)] points, target_points = np.array(points, dtype=np.float32), np.array(target_points, dtype=np.float32) M = cv.getPerspectiveTransform(points, target_points) # print('透視變換矩陣:', M) result = cv.warpPerspective(src_copy, M, (0, 0)) result = result[:H, :W] win_name = 'Result' cv.namedWindow(win_name, cv.WINDOW_NORMAL) cv.resizeWindow(win_name, width=W, height=H) cv.imshow(win_name,result) cv.waitKey(0) cv.destroyAllWindows() return result
主程序
if __name__ == '__main__': path = './1.jpg' src = cv.imread(path) src_copy = src.copy() show_img(src) W = 20 H = 20 # points=[(112, 308), (175, 310), (176, 369), (113, 369)] points=get_points(src) n = 20 W = int(W * n) H = int(H * n) result=photo_cut_restore(src_copy,points,H,W) output_file = 'result.jpg' cv.imwrite(output_file, result)
誒,氣不氣,好不容易一段段復(fù)制完,結(jié)果最后居然有一鍵復(fù)制的地方
import cv2 as cv import numpy as np #輸入cv.imread后的圖片,通過點(diǎn)擊四個(gè)點(diǎn)選擇要裁剪的部分 def get_window_size(src, bound=600): h,w = src.shape[0], src.shape[1] if h > w: h, w = bound, int(w*bound/h) else: h, w = int(h*bound/w), bound return (h, w) class Indexer: def __init__(self): self.id = 0 def get_id(self): self.id = (self.id + 1) return (self.id) def on_EVENT_LBUTTONDOWN(event, x, y, flags, param): if event == cv.EVENT_LBUTTONDOWN: img = param['src'] win_name = param['window'] indexer = param['indexer'] points = param['points'] curr_id = indexer.get_id() points.append((x, y)) print('第{}個(gè)頂點(diǎn): ({},{})'.format(curr_id, x, y)) cv.circle(img, (x, y), 10, (0, 0, 255), thickness=2) cv.putText( img, str(curr_id), # 文字 (x, y), # 坐標(biāo) cv.FONT_HERSHEY_PLAIN, 5, # 字號(hào) (0, 0, 255), # 字體顏色 thickness=2 # 粗細(xì) ) cv.imshow(win_name, img) #輸入cv.imread后的圖片,通過點(diǎn)擊四個(gè)點(diǎn)選擇要裁剪的部分 def get_points(src): points = [] indexer = Indexer() h, w=get_window_size(src) win_name = 'get_points' cv.namedWindow(win_name, cv.WINDOW_NORMAL) cv.resizeWindow(win_name, width=w, height=h) cv.imshow(win_name, src) cv.setMouseCallback(win_name, on_EVENT_LBUTTONDOWN, param={'src': src, 'window': win_name, 'indexer': indexer, 'points': points}) cv.waitKey(0) cv.destroyAllWindows() if len(points)>4: return points[0:4] # print(points) # points=[(2, 14), (90, 50), (87, 194), (1, 204)] return points #輸入cv.imread后的圖片,展示圖片長(zhǎng)什么樣 def show_img(src): win_name = 'show_img' h, w=get_window_size(src) cv.namedWindow(win_name, cv.WINDOW_NORMAL) cv.resizeWindow(win_name, width=w, height=h) cv.imshow(win_name, src) cv.waitKey(0) cv.destroyAllWindows() def photo_cut_restore(src,points,H,W): target_points = [(0, 0), (W, 0), (W, H), (0, H)] points, target_points = np.array(points, dtype=np.float32), np.array(target_points, dtype=np.float32) M = cv.getPerspectiveTransform(points, target_points) # print('透視變換矩陣:', M) result = cv.warpPerspective(src_copy, M, (0, 0)) result = result[:H, :W] win_name = 'Result' cv.namedWindow(win_name, cv.WINDOW_NORMAL) cv.resizeWindow(win_name, width=W, height=H) cv.imshow(win_name,result) cv.waitKey(0) cv.destroyAllWindows() return result if __name__ == '__main__': path = './3.jpg' src = cv.imread(path) src_copy = src.copy() # show_img(src) W = 20 H = 20 # points=[(124, 182), (181, 177), (180, 243), (125, 266)] points=get_points(src) print(points) n = 20 W = int(W * n) H = int(H * n) result=photo_cut_restore(src_copy,points,H,W) output_file = 'result.jpg' cv.imwrite(output_file, result)
看完了這篇文章,相信你對(duì)“Python圖片處理之圖片裁剪的示例分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。