欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?Opencv實現(xiàn)圖片切割處理

 更新時間:2022年01月23日 09:53:29   作者:Python之魂  
這篇文章主要為大家詳細介紹了Python?Opencv實現(xiàn)圖片切割處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python Opencv實現(xiàn)圖片的切割處理,供大家參考,具體內(nèi)容如下

Opencv對圖片的切割:

方法一:

import os
from PIL import Image

def splitimage(src, rownum, colnum, dstpath):
? ? img = Image.open(src)
? ? w, h = img.size
? ? if rownum <= h and colnum <= w:
? ? ? ? print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))
? ? ? ? print('開始處理圖片切割, 請稍候...')

? ? ? ? s = os.path.split(src)
? ? ? ? if dstpath == '':
? ? ? ? ? ? dstpath = s[0]
? ? ? ? fn = s[1].split('.')
? ? ? ? basename = fn[0]
? ? ? ? ext = fn[-1]

? ? ? ? num = 0
? ? ? ? rowheight = h // rownum
? ? ? ? colwidth = w // colnum
? ? ? ? for r in range(rownum):
? ? ? ? ? ? for c in range(colnum):
? ? ? ? ? ? ? ? box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)
? ? ? ? ? ? ? ? img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext)
? ? ? ? ? ? ? ? num = num + 1

? ? ? ? print('圖片切割完畢,共生成 %s 張小圖片。' % num)
? ? else:
? ? ? ? print('不合法的行列切割參數(shù)!')

src = input('請輸入圖片文件路徑:')
if os.path.isfile(src):
? ? dstpath = input('請輸入圖片輸出目錄(不輸入路徑則表示使用源圖片所在目錄):')
? ? if (dstpath == '') or os.path.exists(dstpath):
? ? ? ? row = int(input('請輸入切割行數(shù):'))
? ? ? ? col = int(input('請輸入切割列數(shù):'))
? ? ? ? if row > 0 and col > 0:
? ? ? ? ? ? splitimage(src, row, col, dstpath)
? ? ? ? else:
? ? ? ? ? ? print('無效的行列切割參數(shù)!')
? ? else:
? ? ? ? print('圖片輸出目錄 %s 不存在!' % dstpath)
else:
? ? print('圖片文件 %s 不存在!' % src)

方法二:

# coding=utf-8
import numpy as np

import cv2
from PIL import Image

image = cv2.imread("../staticimg/oldimg_04.jpg")

b ?= np.array([[0,248], ?[512,254], [512,512],[0,512]], dtype = np.int32)
c ?= np.array([[0,0], ?[512,0], [512,254],[0,248]], dtype = np.int32)


roi_t = []
roi_c = []
for i in range(4):
? ? roi_t.append(b[i])
? ? roi_c.append(c[i])

roi_t = np.asarray(roi_t)
roi_t = np.expand_dims(roi_t, axis=0)
im = np.zeros(image.shape[:2], dtype="uint8")
cv2.polylines(im, roi_t, 1, 255)
cv2.fillPoly(im, roi_t, 255)

roi_c = np.asarray(roi_c)
roi_c = np.expand_dims(roi_c, axis=0)
imc = np.zeros(image.shape[:2], dtype="uint8")
cv2.polylines(imc, roi_c, 1, 255)
cv2.fillPoly(imc, roi_c, 255)

mask = im
maskc = imc
maskedtop = cv2.bitwise_and(image,image,mask=mask)
maskedbody = cv2.bitwise_and(image,image,mask=maskc)


imp = Image.fromarray(image)

arraytop = np.zeros((maskedtop.shape[0], maskedtop.shape[1], 4), np.uint8)
arraybody = np.zeros((maskedbody.shape[0], maskedbody.shape[1], 4), np.uint8)
arraytop[:, :, 0:3] = maskedtop
arraybody[:, :, 0:3] = maskedbody
arraytop[:, :, 3] = 0
arraytop[:,:,3][np.where(arraytop[:,:,0]>2)]=255
arraytop[:,:,3][np.where(arraytop[:,:,1]>2)]=255
arraytop[:,:,3][np.where(arraytop[:,:,2]>2)]=255
print(arraytop.max())
image_1 = Image.fromarray(arraytop)
image_1.save("666.jpg","PNG")

arraybody[:, :, 3] = 0
arraybody[:,:,3][np.where(arraybody[:,:,0]>2)]=255
arraybody[:,:,3][np.where(arraybody[:,:,1]>2)]=255
arraybody[:,:,3][np.where(arraybody[:,:,2]>2)]=255
print(arraybody.max())
image_2 = Image.fromarray(arraybody)
image_2.save("888.jpg","PNG")
# cv2.imwrite("333.jpg",maskedtop)
# cv2.imwrite("222.jpg",maskedbody)
# ---------------------

# def cut_img(image, array_points,array_points2):
# ? ? b = np.array(array_points, dtype=np.int32)
# ? ? c = np.array(array_points2, dtype=np.int32)
#
# ? ? roi_t = []
# ? ? roi_c = []
# ? ? for i in range(2):
# ? ? ? ? roi_t.append(b[i])
# ? ? ? ? roi_c.append(c[i])
#
# ? ? roi_t = np.asarray(roi_t)
# ? ? roi_t = np.expand_dims(roi_t, axis=0)
# ? ? im = np.zeros(image.shape[:2], dtype="uint8")
# ? ? cv2.polylines(im, roi_t, 1, 255)
# ? ? cv2.fillPoly(im, roi_t, 255)
#
# ? ? roi_c = np.asarray(roi_c)
# ? ? roi_c = np.expand_dims(roi_c, axis=0)
# ? ? imc = np.zeros(image.shape[:2], dtype="uint8")
# ? ? cv2.polylines(imc, roi_c, 1, 255)
# ? ? cv2.fillPoly(imc, roi_c, 255)
# ? ? mask = im
# ? ? maskc = imc
# ? ? kk = cv2.bitwise_and(image,image,mask=mask)
# ? ? kkc = cv2.bitwise_and(image,image,mask=maskc)
# ? ? cv2.imwrite("333.jpg",kk)
# ? ? cv2.imwrite("222.jpg",kkc)
# ? ? return cv2.bitwise_and(image, image, mask=mask)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 一篇文章帶你了解Python中的類

    一篇文章帶你了解Python中的類

    這篇文章主要給大家介紹了關于Python中類的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2021-09-09
  • 用python實現(xiàn)批量重命名文件的代碼

    用python實現(xiàn)批量重命名文件的代碼

    任務很簡單,某個目錄下面有幾千個文件,某些文件沒有后綴名,現(xiàn)在的任務就是將所有的沒有后綴名的文件加上后綴名,python有現(xiàn)成的函數(shù)可以實現(xiàn),但是在實現(xiàn)過程中遇到幾個問題,分享一下解決方法
    2012-05-05
  • 在python中用url_for構(gòu)造URL的方法

    在python中用url_for構(gòu)造URL的方法

    今天小編就為大家分享一篇在python中用url_for構(gòu)造URL的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python操作cfg配置文件方式

    python操作cfg配置文件方式

    今天小編就為大家分享一篇python操作cfg配置文件方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python實現(xiàn)文件按照日期命名的方法

    Python實現(xiàn)文件按照日期命名的方法

    這篇文章主要介紹了Python實現(xiàn)文件按照日期命名的方法,涉及Python針對文件的遍歷、讀寫及時間操作相關技巧,需要的朋友可以參考下
    2015-07-07
  • Python的Flask框架中配置多個子域名的方法講解

    Python的Flask框架中配置多個子域名的方法講解

    Fask中可以通過通配符子域的方式來部署多個子域名,這里我們就來作一個Python的Flask框架中配置多個子域名的方法講解,需要的朋友可以參考下
    2016-06-06
  • 使用Pyrex來擴展和加速Python程序的教程

    使用Pyrex來擴展和加速Python程序的教程

    這篇文章主要介紹了使用Pyrex來擴展和加速Python程序的教程,來自IBM官方技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • python自動12306搶票軟件實現(xiàn)代碼

    python自動12306搶票軟件實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了python自動12306搶票軟件的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python運行時修改業(yè)務SQL代碼

    Python運行時修改業(yè)務SQL代碼

    這篇文章主要介紹了Python運行時修改業(yè)務SQL代碼,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • Elasticsearch映射字段數(shù)據(jù)類型及管理

    Elasticsearch映射字段數(shù)據(jù)類型及管理

    這篇文章主要介紹了Elasticsearch映射字段數(shù)據(jù)類型及管理的講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04

最新評論