将中值模糊的结果导出到类中并导出为TIFF

时间:2016-08-30 21:13:06

标签: python image-processing python-imaging-library tiff

我正在开发一个程序,该程序将中值模糊应用到多帧tiff文件中。我的目标是过滤tiff序列中的每一帧,然后将结果保存为相同的序列,只需过滤。但是,无论何时我运行它,它只保存最后一帧,因为我不知道如何在运行时将数据正确保存到单独的序列中。

#takes .tiff, loads it into PIL, converts to greyscale, sets attributes in PIL form
im = Image.open('example_recording.tif').convert('L')
im.save('greyscale_example.tif')

width,height = im.size
image_lookup = 0

#creates class used to de-sequence the animation


class ImageSequence:
    def __init__(self, im):
        self.im = im
    def __getitem__(self, ix):
        try:
            if ix:
                self.im.seek(ix)
            return self.im
        except EOFError:
            raise IndexError # end of sequence; needed to avoid process from unecessary breaking once it runs to the end of the tiff file animation


for frame in ImageSequence(im):
            imarray = np.array(frame)
            Blur = cv2.medianBlur(imarray,5)
            im = Image.fromarray(Blur)

im.save('corrected.tif')
#(saves actually only the last frame of the movie, med-corrected)

根据安德鲁斯的建议,代码被修改为如下所示:

im = Image.open('example_recording.tif')
width,height = im.size
image_lookup = 0
n = 1
while True:
    try:
        im.seek(n)
        n = n+1
    except EOFError:
        print "length is",  n
        break;
    #this solves the length issue as ImageSequence doesnt have _len_ attribute
class ImageSequence:
    def __init__(self, im):
        self.im = im
    def __getitem__(self, ix):
        try:
            if ix:
                self.im.seek(ix)
            return self.im
        except EOFError:
            raise IndexError
depth = n
target_array = np.zeros((width, height, depth))
for index, frame in enumerate(ImageSequence(im)):
    imarray = np.array(frame)
    Blur = cv2.medianBlur(imarray,5)
    print type(Blur)
    im = Image.fromarray(Blur)
    im.save('corrected_{}.tif'.format(index))
    print n

所以现在它的效果非常好!

1 个答案:

答案 0 :(得分:1)

' This script creates a shortcut of the MyApp application and places it in the Public Desktop folder for all users.

Option Explicit

Dim objExec, output, objDir, objWSH, objFSO, link, DesktopPath, AppPath, IconPath, DirPath

Set objWSH = WScript.CreateObject("WScript.Shell")
Set objExec = objWSH.Exec("Where /R ""C:\Program Files\MyApp"" ""MyApp.exe"" ")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

output = objExec.StdOut.ReadLine
DirPath = Replace(output, "\MyApp.exe", "")
IconPath = (DirPath & "\MyAppIcon.ico")
AppPath = (DirPath & "\MyApp.exe")
DesktopPath = "C:\Users\Public\Desktop"

' If file exists define where the shortcut should point to
If objFSO.FileExists(AppPath) Then
 set link = objWSH.CreateShortcut(DesktopPath & "\MyApp.lnk")

' Define icon settings
 link.TargetPath = AppPath
 link.IconLocation = IconPath
 link.Description = "MyApp"
 link.WindowStyle = 3
 link.WorkingDirectory = DirPath
 link.Save
Else
 WScript.Echo "Program file does not exist"

 End If

这给你提供了一系列很好的矩阵,我认为你必须扭转你所做的所有事情来拉出你的图像,但我不是PIL专家。

编辑:

depth = len(ImageSequence(im))
target_array = np.zeros((width, height, depth))
for index, frame in enumerate(ImageSequence(im)):
    imarray = np.array(frame)
    Blur = cv2.medianBlur(imarray,5)
    target_array[:, :, index] = Blur

这应该为每个循环提供一个图像,至少。