Python 3.x Python图像连接

Python 3.x Python图像连接,python-3.x,opencv,Python 3.x,Opencv,我把图像分成4块 我想用随机放置的方式重新制作它们。 例如:frame2\u frame3\u frame1\u frame4 image=cv2.imread("example.jpg") height, width, channels = image.shape frame_1=image[1:int(height/2)+1,0:int(width/2)] frame_2=image[1:int(height/2)+1,int(width/2)+1:int(width)+1] frame_

我把图像分成4块 我想用随机放置的方式重新制作它们。 例如:frame2\u frame3\u frame1\u frame4

image=cv2.imread("example.jpg") 
height, width, channels = image.shape
frame_1=image[1:int(height/2)+1,0:int(width/2)]
frame_2=image[1:int(height/2)+1,int(width/2)+1:int(width)+1]
frame_3=image[int(height/2)+1:int(height)+1,0:int(width/2)+1]
frame_4=image[int(height/2)+1:int(height)+1,int(width/2)+1:int(width)+1]
total=[frame_1+frame_2]
谢谢

试试这个

import cv2
import numpy as np

img=cv2.imread("1.jpg") 

#resize image to make sure 4 pieces have the same dimension
image = cv2.resize(img, (400,400),interpolation=cv2.INTER_AREA)

height, width, channels = image.shape

frame_1=image[0:int(height/2),0:int(width/2)]
frame_2=image[0:int(height/2),int(width/2):int(width)]
frame_3=image[int(height/2):int(height),0:int(width/2)]
frame_4=image[int(height/2):int(height),int(width/2):int(width)]

new_image = np.empty_like(image)

new_image[0:int(height/2),0:int(width/2)] = frame_3
new_image[0:int(height/2),int(width/2):int(width)] = frame_2
new_image[int(height/2):int(height),0:int(width/2)] = frame_1
new_image[int(height/2):int(height),int(width/2):int(width)] = frame_4

cv2.imshow("image",image)
cv2.imshow("new_image",new_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

我不确定您是否完全想学习如何使用Python完成这项工作,或者您是否只是想完成这项工作。如果是前者,请忽略我的回答,如果是后者,这在终端中非常简单,使用ImageMagick,无需编写任何代码

从该图像开始
colorwheel.png

在终端中运行以下命令(或在Windows上运行命令提示符):

命令说。。。“打开
colorwheel.png
并分成4个相等的部分。交换第一部分和第三部分,并以MIFF格式写出四幅图像。将其传递到ImageMagick的
蒙太奇
命令中,并将图像连接在一起,中间不留任何间隙。”


或者,如果你想把它切成16块,让它跳起来:

magick colorwheel.png -crop 4x4@ -swap 0,14 -swap 1,12 -swap 6,9  miff:- | magick montage -geometry +0+0 miff:- result.png

所以。。到目前为止,您尝试了什么?感谢您的关注,它确实很有用,但我打算学习如何使用Python实现这一点。很酷-没有问题。祝你的项目好运!我不知道这是否会引起兴趣,但我有一个名为shuffle的bash unix Imagemagick脚本,它可以进行这种处理和其他处理。
@Mark Setchell
如果您不介意,请发送我的电子邮件地址。你可以在我的网站上找到我的。我想我们前一段时间沟通过,但我把你的电子邮件地址弄丢了。没什么重要的。我只是想把它拿出来。Thanks@fmw42我刚刚给你发了电子邮件。谢谢你的关注,它正在处理我的照片,现在我将尝试随机放置。
magick colorwheel.png -crop 4x4@ -swap 0,14 -swap 1,12 -swap 6,9  miff:- | magick montage -geometry +0+0 miff:- result.png