使用 skimage (Python) 批量处理 Tiff 文件

发布于 2025-01-13 03:05:05 字数 668 浏览 2 评论 0原文

我正在寻找;在 Python 中打开、处理和保存多个 TIFF。

我有以下代码可以打开、处理和保存 1(一)个 TIFF,但我在处理多个文件时遇到问题:

import skimage.io
import skimage.viewer
import skimage                 
import skimage.io    
         
# Read 1 image.TIF:
image = skimage.io.imread(fname=path)
image[2,1]= 1.0

# Process the file (make binary)
gray_image = skimage.color.rgb2gray(image)

# Blur the image to denoise (larger sigma = more noise removed)
blurred_image = skimage.filters.gaussian(gray_image, sigma=5)

# Adding threshold, t:
t = 0.8
binary_mask = blurred_image < t

# Save the file to another location:
skimage.io.imsave(fname=path, arr = binary_mask)

非常感谢任何帮助!

I am looking to; open, process and save multiple TIFFs in Python.

I have the following code to open, process and save 1 (one) TIFF, but I have trouble with multiple files:

import skimage.io
import skimage.viewer
import skimage                 
import skimage.io    
         
# Read 1 image.TIF:
image = skimage.io.imread(fname=path)
image[2,1]= 1.0

# Process the file (make binary)
gray_image = skimage.color.rgb2gray(image)

# Blur the image to denoise (larger sigma = more noise removed)
blurred_image = skimage.filters.gaussian(gray_image, sigma=5)

# Adding threshold, t:
t = 0.8
binary_mask = blurred_image < t

# Save the file to another location:
skimage.io.imsave(fname=path, arr = binary_mask)

Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2025-01-20 03:05:05

以下是一种可能有所帮助的多处理方法:

import skimage
from concurrent.futures import ProcessPoolExecutor
from glob import glob
import os.path

source_dir = '<your source directory>'
target_dir = '<your target directory>'
filetype = '*.tif'

def process(path):
    image = skimage.io.imread(fname=path)
    image[2,1] = 1.0
    gray_image = skimage.color.rgb2gray(image)
    blurred_image = skimage.filters.gaussian(gray_image, sigma=5)
    outpath = os.path.join(target_dir, os.path.basename(path))
    arr = blurred_image < 0.8
    skimage.io.imsave(fname=outpath, arr=arr)

def main():
    with ProcessPoolExecutor() as executor:
        filelist = glob(os.path.join(source_dir, filetype))
        executor.map(process, filelist)

if __name__ == '__main__':
    main()

使用 glob 识别与 *.tif 模式匹配的所有文件,然后利用 ProcessPoolExecutor 的 map 函数在其自己的进程中处理每个文件。由于处理主要是 CPU 密集型的,因此多处理可能是最适合这种情况的方法

Here's a multiprocessing approach that may help:

import skimage
from concurrent.futures import ProcessPoolExecutor
from glob import glob
import os.path

source_dir = '<your source directory>'
target_dir = '<your target directory>'
filetype = '*.tif'

def process(path):
    image = skimage.io.imread(fname=path)
    image[2,1] = 1.0
    gray_image = skimage.color.rgb2gray(image)
    blurred_image = skimage.filters.gaussian(gray_image, sigma=5)
    outpath = os.path.join(target_dir, os.path.basename(path))
    arr = blurred_image < 0.8
    skimage.io.imsave(fname=outpath, arr=arr)

def main():
    with ProcessPoolExecutor() as executor:
        filelist = glob(os.path.join(source_dir, filetype))
        executor.map(process, filelist)

if __name__ == '__main__':
    main()

Use glob to identify all the files matching the *.tif pattern then utilise the ProcessPoolExecutor's map function to process each file in its own process. As the processing is mainly CPU intensive, multiprocessing is likely to be the best fit for this

绮烟 2025-01-20 03:05:05

是否有必要并行化?您正在执行的处理量并不大。如果您不需要并行处理,您可以在图像上运行 for 循环

import skimage.io
import skimage.viewer
import skimage                 
import skimage.io
import os
import glob

# set up an in and out directory
in_dir = 'directory\with\images'
out_ir = 'directory\for\procecessed\images'

# make a list of all of the raw image files
os.chdir(in_dir)
filelist = glob.glob('*.png') # change to whatever file pattern you need here


for file_iter in filelist:
        
    os.chdir(in_dir)
    image = skimage.io.imread(fname=file_iter)
    image[2,1]= 1.0

    # Process the file (make binary)
    gray_image = skimage.color.rgb2gray(image)

    # Blur the image to denoise (larger sigma = more noise removed)
    blurred_image = skimage.filters.gaussian(gray_image, sigma=5)

    # Adding threshold, t:
    t = 0.8
    binary_mask = blurred_image < t

    # Save the file to another location:
    out_filename = file_iter[:-4] + 'processed.png' # make new filename based on old filename
    os.chdir(out_dir)
    skimage.io.imsave(fname=out_filename, arr = binary_mask)

Is it necessary that this be parallelized? It's not a huge bit of processing that you are performing. If you don't need parallel processing you can just run a for loop on your images

import skimage.io
import skimage.viewer
import skimage                 
import skimage.io
import os
import glob

# set up an in and out directory
in_dir = 'directory\with\images'
out_ir = 'directory\for\procecessed\images'

# make a list of all of the raw image files
os.chdir(in_dir)
filelist = glob.glob('*.png') # change to whatever file pattern you need here


for file_iter in filelist:
        
    os.chdir(in_dir)
    image = skimage.io.imread(fname=file_iter)
    image[2,1]= 1.0

    # Process the file (make binary)
    gray_image = skimage.color.rgb2gray(image)

    # Blur the image to denoise (larger sigma = more noise removed)
    blurred_image = skimage.filters.gaussian(gray_image, sigma=5)

    # Adding threshold, t:
    t = 0.8
    binary_mask = blurred_image < t

    # Save the file to another location:
    out_filename = file_iter[:-4] + 'processed.png' # make new filename based on old filename
    os.chdir(out_dir)
    skimage.io.imsave(fname=out_filename, arr = binary_mask)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文