栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python实现图片n宫格裁剪和拼接

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python实现图片n宫格裁剪和拼接

效果展示:

代码如下:

 

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import math
import random

# 读入图片
im = Image.open('ruanbin.jpg')  # 这里放置你的图片路径
im = np.array(im)  # 将图片转化成numpy数组
# 输入一个数字并计算行数和列数 输入的数字必须能开平方
n = int(input())
row = int(math.sqrt(n))  # 计算每一行图片个数

# 高度和宽度
H, W = im.shape[0], im.shape[1]
height = H // row
width = W // row
# 宽度和高度取模
rowmod = H % row
colmod = W % row
# 高度和宽度的起始像素和结束像素
startrow = rowmod // 2
startcol = colmod // 2
endrow = startrow + row * height
endcol = startcol + row * width

List = []
for h in range(startrow, endrow, height):
    for w in range(startcol, endcol, width):
        temp = im[h:h + height, w:w + width, :]
        List.append(temp)
        # plt.imshow(temp)
# print(len(List))
random.shuffle(List)
List1 = []
# 对图片进行行拼接
for i in range(row):
    img = List[i * row]
    for j in range(row - 1):
        img = np.concatenate((img, List[j + i * row + 1]), axis=1)
    List1.append(img)  # 行拼接的每个图片存储在一个列表中
img1 = List1[0]
# 再对刚才列表的图片进行竖拼接
for i in range(1, row):
    img1 = np.concatenate((img1, List1[i]), axis=0)
plt.imshow(img1)
plt.show()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/961051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号