我认为这会更容易,但过了一段时间我终于放弃了这个,至少在几个小时内......
我想从一组游戏中时光倒流的照片中重现这个尾随的星星图像。灵感来自于:
The original author使用VirtualDub拍摄的低分辨率视频帧并与imageJ结合使用。我想我可以轻松地重现这个过程,但是使用Python的内存意识更强,所以我可以使用the original high-resolution images来获得更好的输出。
我的算法的想法很简单,一次合并两个图像,然后通过合并生成的图像和下一个图像进行迭代。这样做了几百次并适当地称重,以便每张图像对最终结果都有相同的贡献。
我对python很新(我不是专业的程序员,这很明显),但环顾四周我认为Python Imaging Library是非常标准的,所以我决定使用它(纠正我)如果你认为别的东西会更好的话。)
这是我到目前为止所拥有的:
#program to blend many images into one
import os,Image
files = os.listdir("./")
finalimage=Image.open("./"+files[0]) #add the first image
for i in range(1,len(files)): #note that this will skip files[0] but go all the way to the last file
currentimage=Image.open("./"+files[i])
finalimage=Image.blend(finalimage,currentimage,1/float(i+1))#alpha is 1/i+1 so when the image is a combination of i images any adition only contributes 1/i+1.
print "\r" + str(i+1) + "/" + str(len(files)) #lousy progress indicator
finalimage.save("allblended.jpg","JPEG")
这可以实现预期,但结果图像是黑暗的,如果我只是尝试增强它,很明显由于像素值的深度不足而导致信息丢失。 (我不确定这里适当的术语是什么,颜色深度,颜色精度,像素大小)。 这是使用低分辨率图像的最终结果:
或者我尝试使用完全4k x 2k分辨率(来自另一组照片):
所以,我尝试通过设置图像模式来修复它:
firstimage=Image.open("./"+files[0])
size = firstimage.size
finalimage=Image.new("I",size)
但显然Image.blend不接受该图像模式。
ValueError:图片模式错误
有什么想法吗?
(我还尝试通过将图像与im.point(lambda i:i * 2)组合之前将其相乘来使图像“不那么暗”,但结果同样糟糕)
答案 0 :(得分:20)
这里的问题是你要平均每个像素的亮度。这似乎是明智的,但它实际上并不是你想要的 - 明亮的星星会“平均化”,因为它们会在图像中移动。采取以下四个框架:
1000 0000 0000 0000
0000 0100 0000 0000
0000 0000 0010 0000
0000 0000 0000 0001
如果你平均那些,你会得到:
0.25 0 0 0
0 0.25 0 0
0 0 0.25 0
0 0 0 0.25
何时需要:
1000
0100
0010
0001
不是混合图像,而是可以尝试在每个像素的任何图像中看到最大值。如果您有PIL,可以在ImageChops中尝试更轻的功能。
from PIL import ImageChops
import os, Image
files = os.listdir("./")
finalimage=Image.open("./"+files[0])
for i in range(1,len(files)):
currentimage=Image.open("./"+files[i])
finalimage=ImageChops.lighter(finalimage, currentimage)
finalimage.save("allblended.jpg","JPEG")
这是我得到的:
编辑:我阅读了Reddit帖子,看到他实际上结合了两种方法 - 一种用于星迹,另一种用于地球。这是您尝试的平均值的更好实现,具有适当的权重。我使用numpy数组代替uint8 Image数组。
import os, Image
import numpy as np
files = os.listdir("./")
image=Image.open("./"+files[0])
im=np.array(image,dtype=np.float32)
for i in range(1,len(files)):
currentimage=Image.open("./"+files[i])
im += np.array(currentimage, dtype=np.float32)
im /= len(files) * 0.25 # lowered brightness, with magic factor
# clip, convert back to uint8:
final_image = Image.fromarray(np.uint8(im.clip(0,255)))
final_image.save('all_averaged.jpg', 'JPEG')
这是图像,然后您可以将图像与前一个星迹相结合。