【发布时间】:2016-01-19 17:52:36
【问题描述】:
我正在处理一个需要连接大量图像 (80282) 的项目。每张图片都是256 x 256像素,有的文件是空的(没有图片),所以我需要创建一个空白图片来替换文件。我有这种格式的数据:data-D0H0-X52773-Y14041
X 和 Y 对应于我需要按顺序连接的坐标。顺序是从左上角 X52773-Y14314 到右下角 X52964-Y14041。在 X 上进行了 294 次迭代,在 Y 上进行了 274 次迭代。这是我编写的无法正常工作的代码,如果您有任何想法,我可以使用任何帮助,目前,我的图像在 Y 上没有很好地对齐。例如,图像 X10-Y10 不在图像 X10-Y11 的下方。我认为我在正确使用 try: 时遇到了一些问题,除了: 谢谢你的帮助!
from PIL import Image
width = 75264
height = 70144
new_im = Image.new('RGBA', (75264, 70144))
x_offset = 0
y_offset = 0
coordinate = {}
coordinate['x']=52672
coordinate['y']=14314
#top image line should be from: X52,672-Y14,314 to X52,965-Y14,314
#bottom image line should be from: X52,672-Y14,041 to X52,965-Y14,041
for irow in range(0, 274):
for icol in range(0, 294):
try:
if (x_offset == width):
coordinate['y'] = coordinate['y'] - 1
coordinate['x'] = 52672
img = Image.open("data-D0H0-X"+str(coordinate['x'])+"-Y"+str(coordinate['y'])+".png")
except:
coordinate['x'] = coordinate['x'] + 1
blank = Image.new('RGBA', (256,256))
new_im.paste(blank, (x_offset, y_offset))
x_offset += 256
if (x_offset == width):
x_offset = 0
y_offset += 256
break
new_im.paste(img, (x_offset, y_offset))
x_offset += 256
if (x_offset == width):
x_offset = 0
y_offset += 256
coordinate['x'] = coordinate['x'] + 1
new_im.show()
new_im.save('full_image.png')
编辑: 这是我根据您的回答修改的新代码。但是,我仍然收到一个错误: struct.error: 'I' 格式需要 0
不确定我现在的坐标计算是否正确。
代码:
from PIL import Image
import glob
import imghdr
width = 75264
height = 70144
new_im = Image.new('RGBA', (width, height))
for filename in glob.glob('data-D0H0-X*.png'):
tmp_arr = filename.split('-')
x_coord = int(tmp_arr[2][1:6])
y_coord = int(tmp_arr[3][1:6])
info = imghdr.what(filename)
if (info == "png"):
new_img = Image.open(filename)
else:
new_img = Image.new('RGBA', (256,256))
x_coord = (x_coord-52672)*256
y_coord = (14314-y_coord)*256
print x_coord, y_coord
new_im.paste(new_img, (x_coord, y_coord))
new_im.show()
new_im.save('full_image.png')
【问题讨论】:
标签: python image python-imaging-library