脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python Opencv实现图片切割处理

Python Opencv实现图片切割处理

2022-08-31 10:16Python之魂 Python

这篇文章主要为大家详细介绍了Python Opencv实现图片切割处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Python Opencv实现图片的切割处理,供大家参考,具体内容如下

Opencv对图片的切割:

方法一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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('不合法的行列切割参数!')
 
src = input('请输入图片文件路径:')
if os.path.isfile(src):
    dstpath = input('请输入图片输出目录(不输入路径则表示使用源图片所在目录):')
    if (dstpath == '') or os.path.exists(dstpath):
        row = int(input('请输入切割行数:'))
        col = int(input('请输入切割列数:'))
        if row > 0 and col > 0:
            splitimage(src, row, col, dstpath)
        else:
            print('无效的行列切割参数!')
    else:
        print('图片输出目录 %s 不存在!' % dstpath)
else:
    print('图片文件 %s 不存在!' % src)

方法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43431189/article/details/93511316

延伸 · 阅读

精彩推荐