【发布时间】:2016-08-31 19:34:10
【问题描述】:
我有多个图像,我正在使用 for 循环。我想通过在 x 和 y 中将初始图像滚动一个像素并每次保存滚动图像来为每个图像获得 5 个伪层。
我正在练习一张图片,以确保我可以从中得到五层,但没有乐趣。
我的代码在下面,但它只保存了第一张图片;输出为 pl_0.tif
list_frames = glob.glob('*.gif')
for index, fname in enumerate(list_frames):
im = Image.open(fname)
shift=1
imary0 = np.array(im) # initial image (first layer)
imary1x = np.roll(imary0, shift, axis=0) # shift image by one pixel to create second layer
imary1xy = np.roll(imary1x, shift, axis=1)
imary2x = np.roll(imary1xy, shift, axis=0) # shift previous image again to create third layer
imary2xy = np.roll(imary2x, shift, axis=1)
imary3x = np.roll(imary2xy, shift, axis=0) # shift previous image again to create fourth layer
imary3xy = np.roll(imary3x, shift, axis=1)
imary4x = np.roll(imary3xy, shift, axis=0) # shift previous image again to create fifth layer
imary4xy = np.roll(imary4x, shift, axis=1)
im0 = Image.fromarray(imary0)
im0.save('pl_{}.tif'.format(index))
im1 = Image.fromarray(imary1xy)
im1.save('pl_{}.tif'.format(index))
im2 = Image.fromarray(imary2xy)
im2.save('pl_{}.tif'.format(index))
im3 = Image.fromarray(imary3xy)
im3.save('pl_{}.tif'.format(index))
im4 = Image.fromarray(imary4xy)
im4.save('pl_{}.tif'.format(index))
im.close()
有什么建议吗? (如果这很容易解决,请随时嘲笑我)
【问题讨论】:
-
是什么让你认为
'pl_{}.tif'.format(index)多次调用会给出不同的结果? -
@njzk2:答案是:在没有喝咖啡的情况下长时间看到相同的代码。
-
您应该能够将
roll调用组合成单行,并使用 for 循环来编写文件,看起来这就是您要在那里做的事情 -
不幸的是@Jean-FrançoisFabre!但我还在学习,所以我会接受所有反馈!