繁体   English   中英

如何在具有ITK颜色图的文件夹中转换多张图片?

[英]How do I convert multiple pictures, in a folder with ITK color map?

每次我在终端上运行程序时,它都会打印出来:thumb0496.jpg未转换{}未转换

无论我做什么,都永远行不通...我是Python的新手,已经通过Anaconda与OpenCV,Pip和ITK一起安装了它。 我只做了4天而被卡住了。 Python也是我的第一语言。 为什么我的代码不起作用?

如果此代码看起来类似。 我不得不尝试结合一些要素。 不幸的是,我找不到该职位。 之前的代码更糟,但我(以某种方式)对其进行了修复。 这只是我自己无法解决的(新)问题!

import cv2
import sys
import itk
import os,glob

from os import listdir,makedirs
from os.path import isfile,join
path = '/Users/admin/Desktop/ff'
dstpath = '/Users/admin/Desktop/test'

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

RGBPixelType = itk.RGBPixel[PixelType]
RGBImageType = itk.Image[RGBPixelType, Dimension]

ColormapType = itk.CustomColormapFunction[PixelType, RGBPixelType]
colormap = ColormapType.New()

ColormapFilterType = itk.ScalarToRGBColormapImageFilter[ImageType,RGBImageType]
colormapFilter1 = ColormapFilterType.New()

colormapFilter1.SetInput(reader.GetOutput())
colormapFilter1.SetColormap(colormap)

WriterType = itk.ImageFileWriter[RGBImageType]
writer = WriterType.New()
writer.SetFileName(dstpath)
writer.SetInput(colormapFilter1.GetOutput())

try:
    makedirs(dstpath)
except:
    print ("Directory already exist, images will be written in same folder")
files = [f for f in listdir(path) if isfile(join(path,f))]
for image in files:
    try:
        reader = ReaderType(os.path.join(path,image))
        map = ColormapFilterType(reader, PixelType, RGBImageType, ImageType)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,map)
    except:
        print ("{} is not converted".format(image))
for fil in glob.glob("*.jpg"):
    try:
        img = ReaderType(os.path.join(path,fil))
        map_imag = ColormapType(img, PixelType, RGBImageType,ImageType)
        cv2.imwrite(os.path.join(dstpath,fil),map_image)
    except:
        print('{} is not converted')

您为什么不从一个可行的示例开始,逐渐改变它以适合您的需求? 可以在快速入门指南和此博客文章中找到示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM