Python实现图片切割拼接实验——numpy数组的脑洞玩法

视频资料:https://tv.sohu.com/v/dXMvMzM1OTQyMDI2LzExMzQxMDY1MS5zaHRtbA==.html
视频的内容介绍:一张照片,横着切成若干条,并且没有打乱,随后隔条分成了两份,然后把这两份各自拼接在一起,出现了跟两张原图一模一样的图片,将两张图竖着切成若干条,并且没有打乱,随后隔条分成了四份,出现了四张跟原图一模一样的图片(等比例缩小)

目标:使用Python实现图片切割拼接实验

效果:效果如下图所示,证实这个实验是真的,只不过处理后的像素降低了
在这里插入图片描述

原理:
Numpy对图像的处理实际上就是对ndarray的处理。图像和ndarray又有什么关系呢?图像是可以用ndarray数组来表示。如图我们可以用plt.imread()读取一张图片的数据,返回的就是这张图片的ndarray数组。通过对ndarray的处理实现图片操作

步骤解析:
【1】图片读取
读取一、PIL库的image

import numpy as np# pip install numpy
import PIL.Image as img# pip install PIL
data=np.array(img.open('test.jpg'))

读取二、matplotlib库的pyplot

import numpy as np   # pip install numpy
import matplotlib.pyplot as plt  # pip install matplotlib
data=plt.imread('test.jpg')

在这里插入图片描述

# 查看数组的形状
data.shape
# (800,800,3), 
# 第一个800代表图片的像素宽度-纵轴像素,
# 第二个800代表图片的像素长度-横轴像素,
#3代表RGB通道数,(有些图片格式是3通道,有些图片格式是4通道)

【2】图片切割 & 数组拼接

#图像切割——横轴切
width=data.shape[1]
width0= np.split(data,range(10,width,10),axis=1)
width1=width0[::2]
width2=width0[1::2]
#数组的拼接——1轴|纵轴
test1 = np.concatenate(width1,axis=1)
test2 = np.concatenate(width2,axis=1)
print(test1.shape)
plt.imshow(test1)

在这里插入图片描述

#对切割后的test1再进行纵轴切割
length=test1.shape[0]
length0= np.split(test1,range(10,length,10),axis=0)#test1 test2的length和原图等长
length1=length0[::2]
length2=length0[1::2]
#数组的拼接——0轴|横轴
test3 = np.concatenate(length1,axis=0)
test4 = np.concatenate(length2,axis=0)
print(test3.shape)
plt.imshow(test3)

在这里插入图片描述

【3】图片保存
保存一、scipy.misc

import scipy.misc
scipy.misc.imsave('test3.jpg',test3)

保存二、PIL库的image

#image
img.fromarray(test3).save("test3.jpg")

保存三、matplotlib库的pyplot

plt.imsave("test3.jpg",test3)

完整代码:整理定义了一个函数

import numpy as np   # pip install numpy
import matplotlib.pyplot as plt  # pip install matplotlib
jpg_path='test.jpg'
#n为切割的大小,n越大,像素越小
def cut_jpg(jpg_path,n):
    # 读取图片
    data=plt.imread(jpg_path)
    #图像切割_横轴切
    width=data.shape[1]
    width0= np.split(data,range(n,width+1,n),axis=1)#左闭右开所以+1
    width1=width0[::2]
    width2=width0[1::2]
    #数组的拼接
    test1 = np.concatenate(width1,axis=1)
    test2 = np.concatenate(width2,axis=1)
    #图像切割_纵轴切
    length=test1.shape[0]
    #test1 test2的length和原图等长,可以尝试同时切割
    length0= np.split(test1,range(n,length+1,n),axis=0)#左闭右开所以+1
    length1=length0[::2]
    length2=length0[1::2]
    #数组的拼接
    test3 = np.concatenate(length1,axis=0)
    test4 = np.concatenate(length2,axis=0)
    return test3
#返回处理后的数组对象,test1,test2,test3,test4都是一样的,此处返回一组即可
test3=cut_jpg(jpg_path,5)
#保存图片
plt.imsave("test305.jpg",test3)
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
numpy数组元素周围的操作可以通过以下几种方式实现: 1. 切片操作:可以使用numpy数组的切片操作来获取数组中元素的周围元素。 例如,对于一个二维数组arr,要获取第i行第j列元素周围的元素,可以使用如下切片操作: ```python arr[i-1:i+2, j-1:j+2] ``` 这将返回一个3x3的子数组,其中心元素为arr[i,j],周围的8个元素为该子数组的其余元素。 2. 使用numpy.pad()函数:numpy.pad()函数可以用来在数组的边缘添加一个或多个值,从而扩展数组的大小。可以使用该函数来添加额外的行和列,然后通过索引访问周围的元素。 例如,对于一个二维数组arr,要获取第i行第j列元素周围的元素,可以使用如下代码: ```python padded_arr = np.pad(arr, ((1, 1), (1, 1)), mode='constant') surrounding = padded_arr[i:i+3, j:j+3] ``` 这将在数组的边缘添加一行和一列,并使用常量值填充这些额外的元素。然后可以使用切片操作来获取中心元素周围的元素。 3. 使用numpy.roll()函数:numpy.roll()函数可以用来沿着给定轴滚动数组的元素。可以使用该函数来将数组的行和列进行滚动,从而获取周围的元素。 例如,对于一个二维数组arr,要获取第i行第j列元素周围的元素,可以使用如下代码: ```python rows, cols = arr.shape row_indices = np.arange(i-1, i+2) % rows col_indices = np.arange(j-1, j+2) % cols surrounding = arr[row_indices][:, col_indices] ``` 这将将第i行向上和向下滚动一行,并将第j列向左和向右滚动一列,从而获取中心元素周围的元素。使用模运算可以确保在数组的边缘滚动时正确处理索引。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诡途

你的鼓励是我 创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值