【问题标题】:How to merge cropped images back to original image?如何将裁剪后的图像合并回原始图像?
【发布时间】:2020-01-21 18:45:24
【问题描述】:

我有一张尺寸为 10980x10980 的图像,我使用 python 脚本将此图像裁剪为 256x256 尺寸。我用于裁剪的脚本是:

from PIL import Image

  infile = 'Amritsar_T43SDR.tif'
  chopsize = 256

   img = Image.open(infile)
    width, height = img.size

     for x0 in range(0, width, chopsize):
          for y0 in range(0, height, chopsize):
          box = (x0, y0,
                x0+chopsize if x0+chopsize <  width else  width - 1,
                y0+chopsize if y0+chopsize < height else height - 1)
          print('%s %s' % (infile, box))
          img.crop(box).save('Train.%s.x%03d.y%03d.tif' % (infile.replace('.tif',''), x0, y0))

在 1855 张图像中按给定尺寸成功裁剪图像(大多数图像文件大小为 193K,但生成了一些大小为 171Kb 的图像)

现在我想回滚整个过程,即使用裁剪图像,将所有图块合并回原始图像。我们找到了一个函数rollback如下

def roll(image, delta):
    x0, y0 = img.size

delta = delta % xsize
if delta == 0: return image

part1 = image.crop((0, 0, delta, y0))
part2 = image.crop((delta, 0, x0, y0))
image.paste(part1, (x0-delta, 0, x0, y0))
image.paste(part2, (0, 0, x0-delta, y0))

return image

它没有捕捉到原始尺寸 或者,我们使用 ImageMajik 程序并发出命令为

montage -mode concatenate -tile 43x85 Train*.tif new_amritsar.tif

结果它合并了图像,但不是按照真正的顺序

请给我建议解决方案

问候 亚西尔

list of cropped images

list of cropped images with different size and scale as 10980 x 10980 were not split exactly 256 x 356

【问题讨论】:

  • 请问您使用的是什么操作系统?

标签: python image python-imaging-library


【解决方案1】:

如果您使用的是 Windows,您可能对我的其他答案有困难,所以这里是基于 Python 的答案。

再次,我建议您更改文件命名以适应 5 位长度的零填充。然后,您可以通过查看名称中带有 x00000 的文件来获得所有 y 偏移的列表,并通过查看名称中带有 y00000 的所有文件来获得所有 x 偏移的列表。

可能看起来像这样:

#!/usr/bin/env python3

import re
import glob

# Generate a list of all the Y offsets by looking at all the Train*x00000*tif filenames
yoffsets = []
for f in glob.glob("Train*x00000*tif"):
    # Look for a `y` followed by 5 digits
    yoffsets.append(int(re.findall(r'y(\d{5})', f)[0]))

# Generate a list of all the X offsets by looking at all the Train*y00000*tif filenames
xoffsets = []
for f in glob.glob("Train*y00000*tif"):
    # Look for an `x` followed by 5 digits
    xoffsets.append(int(re.findall(r'x(\d{5})', f)[0]))

# Generate filenames in order
for y in sorted(yoffsets):
   ypad = str(y).zfill(5)
   for x in sorted(xoffsets):
      xpad = str(x).zfill(5)
      print(f"Train...y{ypad}.x{xpad}.tif")

现在您可以像以前一样将文件名打印到文件并启动 ImageMagick montage,或者您可以创建一个大的空画布,在我的最终 @987654325 中一次打开文件@循环并将每一个粘贴到正确的位置。

【讨论】:

    【解决方案2】:

    未经测试,但试试这个。首先稍微改变你的文件命名以适应更多的图块:

    img.crop(box).save('Train.%s.y%05d.x%05d.tif' % (infile.replace('.tif',''), y0, x0))
    

    现在您的 xy 坐标应该颠倒以便于排序并且零填充到 5 个位置而不是 3 个位置。

    然后您应该能够以正确的顺序获取文件列表:

    ls Train.Amritsar_T43SDR*tif | sort -n > files.txt
    

    然后将它们蒙太奇:

    magick montage -geometry +0+0 -tile 43x @files.txt result.tif
    

    或者,如果您想要等效的单线:

    ls Train.Amritsar_T43SDR*tif | sort -n | magick montage -geometry +0+0 -tile 43x @- result.tif
    

    关键字:Python、图像处理、重新组装、拼图、ImageMagick、蒙太奇。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多