python - 使用Python OpenCV在循环中进行图像平铺

标签 python image loops opencv tiling

Python noob需要一些帮助!有人可以告诉我如何使用循环重写代码吗?尝试了一些不同的语法,但似乎没有用!

img = cv2.imread("C://Users//user//Desktop//research//images//Underwater_Caustics//set1//set1_color_0001.png")

    tile11=img[1:640, 1:360]
    cv2.imwrite('tile11_underwater_caustic_set1_0001.png', tile11)

    tile12=img[641:1280, 1:360]
    cv2.imwrite('tile12_underwater_caustic_set1_0001.png', tile12)

    tile13=img[1281:1920, 1:360]
    cv2.imwrite('tile13_underwater_caustic_set1_0001.png', tile13)

    tile21=img[1:640, 361:720]
    cv2.imwrite('tile21_underwater_caustic_set1_0001.png', tile21)

    tile22=img[641:1280, 361:720]
    cv2.imwrite('tile22_underwater_caustic_set1_0001.png', tile22)

    tile23=img[1281:1920, 361:720]
    cv2.imwrite('tile23_underwater_caustic_set1_0001.png', tile23)

    tile31=img[1:640, 721:1080]
    cv2.imwrite('tile31_underwater_caustic_set1_0001.png', tile31)

    tile32=img[641:1280, 721:1080]
    cv2.imwrite('tile32_underwater_caustic_set1_0001.png', tile32)

    tile33=img[1281:1920, 721:1080]
    cv2.imwrite('tile33_underwater_caustic_set1_0001.png', tile33)

如您所见,图像将被切成9个相等大小的片段,如何使用循环将其写入?

最佳答案

这不会产生与您的代码相同的结果,但是会给您一些想法:

img = cv2.imread('sample.jpg')
numrows, numcols = 4, 4
height = int(img.shape[0] / numrows)
width = int(img.shape[1] / numcols)
for row in range(numrows):
    for col in range(numcols):
        y0 = row * height
        y1 = y0 + height
        x0 = col * width
        x1 = x0 + width
        cv2.imwrite('tile_%d%d.jpg' % (row, col), img[y0:y1, x0:x1])

关于python - 使用Python OpenCV在循环中进行图像平铺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37842476/

相关文章:

c++ - 最快的 C/C++ 图像大小调整库

PHP 安全图像上传 - 附加代码?

python,向量之间的角度: need for computational efficiency

javascript - 寻找有关如何突出显示图像区域的建议

c - 使用 struct 和 switch 的意外循环

Objective-C 数组迭代速度

python - 组合/添加来自不同 word2vec 模型的向量

java - 在 Java 中压缩数据并在 Python 中解压缩

python - 使用python根据特殊字符将1个字符串分成2个字符串

python - 我尝试通过scapy发送UDP包但失败,为什么?