前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实现图片切割拼接实验——numpy数组的脑洞玩法

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

作者头像
诡途
发布2022-05-09 19:02:55
7190
发布2022-05-09 19:02:55
举报
文章被收录于专栏:诡途的python路诡途的python路

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

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

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

在这里插入图片描述
在这里插入图片描述

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

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

代码语言:javascript
复制
import numpy as np# pip install numpy
import PIL.Image as img# pip install PIL
data=np.array(img.open('test.jpg'))

读取二、matplotlib库的pyplot

代码语言:javascript
复制
import numpy as np   # pip install numpy
import matplotlib.pyplot as plt  # pip install matplotlib
data=plt.imread('test.jpg')
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
# 查看数组的形状
data.shape
# (800,800,3), 
# 第一个800代表图片的像素宽度-纵轴像素,
# 第二个800代表图片的像素长度-横轴像素,
#3代表RGB通道数,(有些图片格式是3通道,有些图片格式是4通道)

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

代码语言:javascript
复制
#图像切割——横轴切
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)
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#对切割后的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

代码语言:javascript
复制
import scipy.misc
scipy.misc.imsave('test3.jpg',test3)

保存二、PIL库的image

代码语言:javascript
复制
#image
img.fromarray(test3).save("test3.jpg")

保存三、matplotlib库的pyplot

代码语言:javascript
复制
plt.imsave("test3.jpg",test3)

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

代码语言:javascript
复制
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)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档