python - 在Python中重新排列图像

标签 python python-3.x image numpy matplotlib

我正在尝试加载 tif 格式的图像文件并将其重新排列成长条,但保留水平顺序。当我尝试使用 numpy reshape 时,它​​只是将不同的行相互重叠或损坏图像。 假设我有一个像这样的数组

imagearray = [1,2,3,4,5,6,7,8,9,10],
             [0,0,0,0,0,0,0,0,0,0],
             [1,1,1,1,1,1,1,1,1,1],
             [2,2,2,2,2,2,2,2,2,2] 

我想重新排列它,使其看起来像这样

[1,2],
[0,0],
[1,1],
[2,2],
[3,4],
[0,0],
[1,1],
[2,2]

等等。我的问题是在重新排列它时保持该顺序,而无需使用更多内存,因为使用 reshape 或重新排列这些 3GB 图像是我可以使用更少内存的方法。我认为这也更困难,因为图像在技术上具有形状(n,m,3),我不完全理解如何操作。 this is what the output looks like来自this image当它看起来更类似于this

这是我正在使用的文件的基础知识

import cv2
import matplotlib.pyplot as plt
from PIL import Image, ImageTk
Image.MAX_IMAGE_PIXELS = None
width =  4000
height = 4000
rollwidth = 1000
I = cv2.imread("image path")
print("read image")
resizedImage = cv2.resize(I,(height,width), interpolation= cv2.INTER_LANCZOS4)
print("combined done")
testimage = np.reshape(resizedImage,((np.ceil(width/rollwidth).astype(np.int64)*height),rollwidth,3), order ='C')
cv2.imwrite("save path", testimage)
print("done")

我认为这与重复标志略有不同,因为使用建议的步骤我仍然无法获得所需的结果

最佳答案

基于this post ,我们只需将其扩展到 3D 情况,记住我们需要分割第二个轴,同时保持第三个轴(颜色 channel 一)不变 -

# a is input 3D array
# ncols would be the width of the desired output image
m,n,r = a.shape
b = a.reshape(m,-1,ncols,r).swapaxes(0,1).reshape(-1,ncols,r)

背后的动机在this post中有详细讨论。 .

关于python - 在Python中重新排列图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58520335/

相关文章:

python - df.mean 不是该系列的真正平均值吗?

python - 如何使用 unittest.mock 调试补丁方法

python - 需要将响应保存到文件并在终端中打印状态

python - 如何通过 Cloudflare 使用 Python 或 DDNS 检查公共(public) IP

python - 在 Python 中乘除非类型和整数

html - em中的标题 Logo 大小

css - 响应式布局中的中心图像

python - 如何将变化的数据读入字典?

python - 用连续线绘制茎

asp.net - 如何在asp.net的Gridview中获取路径存储在数据库中的图像