# writing .png image not working

**URL:** https://discourse.itk.org/t/writing-png-image-not-working/1431
**Category:** Uncategorized
**Created:** [November 21, 2018, 5:19am UTC](https://discourse.itk.org/t/writing-png-image-not-working/1431 "2018-11-21T05:19:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![hothao](https://discourse.itk.org/user_avatar/discourse.itk.org/hothao/32/923_2.png) [@hothao](https://discourse.itk.org/u/hothao)
#### Post date: [November 21, 2018, 5:19am UTC](https://discourse.itk.org/t/writing-png-image-not-working/1431/1 "2018-11-21T05:19:44Z")

</div>

Hi everyone! I’m trying to write Cannyfilter image as a .png image.

It has no bugs. But it does not work. Here is the message:

itk::ExceptionObject (000000926B4FD6A0)

Location: "void \_\_cdecl itk::PNGImageIO::WriteSlice(const class std::basic\_string\<char,struct std::char\_traits\<char\>,class std::allocator\<char\> \> &,const void \*)"

File: c:\itk\src\modules\io\png\src\itkpngimageio.cxx

Line: 519

_ **Description: PNG supports unsigned char and unsigned short** _

I changed the type of pixeltype: to unsigned char or unsigned short, but I got the error: _cannot convert from ‘itk::Concept::lsFloatingPoint\<unsigned char\>::Constraints::FalseT’ to ‘itk::Concept::Detail::UniqueType\_bool\<true\>’_

I get the same error with writing .jpg or .dcm image.

This is the example code I run:

#include "itkImage.h"

#include "itkImageFileReader.h"

#include "itkImageFileWriter.h"

#include "itkRescaleIntensityImageFilter.h"

#include "itkCastImageFilter.h"

#include "itkCannyEdgeDetectionImageFilter.h"

#include "itksys/SystemTools.hxx"

#include \<sstream\>

#include "QuickView.h"

int main(int argc, char \*argv[])

{

if(argc \< 2)

{

std::cerr \<\< "Usage: " \<\< argv[0]

\<\< " Filename [variance lower\_threshold upper\_threshold]"

\<\< std::endl;

return EXIT\_FAILURE;

}

std::string inputFileName = argv[1];

double variance = 2.0;

double upperThreshold = 0.0;

double lowerThreshold = 0.0;

if (argc \> 2)

{

variance = atof(argv[2]);

}

if (argc \> 3)

{

lowerThreshold = atof(argv[3]);

}

if (argc \> 4)

{

upperThreshold = atof(argv[4]);

}

typedef itk::Image\<float, 2\> FloatImageType;

typedef itk::Image\<unsigned char, 2\> ImageType;

typedef itk::ImageFileReader\<FloatImageType\> ReaderType;

ReaderType::Pointer reader = ReaderType::New();

reader-\>SetFileName(inputFileName);

typedef itk::CannyEdgeDetectionImageFilter \<FloatImageType, ImageType\> CannyEdgeDetectionImageFilterType;

CannyEdgeDetectionImageFilterType::Pointer cannyFilter = CannyEdgeDetectionImageFilterType::New();

cannyFilter-\>SetInput(reader-\>GetOutput());

cannyFilter-\>SetVariance( variance );

cannyFilter-\>SetUpperThreshold( upperThreshold );

cannyFilter-\>SetLowerThreshold( lowerThreshold );

QuickView viewer;

[//viewer.AddImage](https://viewer.AddImage)\<DoubleImageType\>(reader-\>GetOutput());

[//viewer.AddImage](https://viewer.AddImage)\<DoubleImageType\>(cannyFilter-\>GetOutput());

viewer.AddImage(

reader-\>GetOutput(), true,

itksys::SystemTools::GetFilenameName(inputFileName));

std::stringstream desc;

desc \<\< "Canny image, variance = "\<\< variance\<\< std::endl

\<\< "upperThreshold = " \<\<upperThreshold \<\< std::endl

\<\<"lowerThreshold = " \<\<lowerThreshold;

viewer.AddImage(cannyFilter-\>GetOutput(), true, desc.str());

std::stringstream ss;

ss \<\< "canny1.png";

typedef itk::ImageFileWriter\<ImageType\> FileWriteType;

FileWriteType::Pointer write = FileWriteType::New();

write-\>SetFileName(ss.str());

write-\>SetInput(cannyFilter-\>GetOutput());

try

{

write-\>Update();

}

catch (itk::ExceptionObject & err)

{

std::cerr \<\< "ExceptionObject caught !" \<\< std::endl;

std::cerr \<\< err \<\< std::endl;

viewer.Visualize();

return EXIT\_SUCCESS;

}

}

Thank you for your help! 🙂

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 21, 2018, 2:58pm UTC](https://discourse.itk.org/t/writing-png-image-not-working/1431/2 "2018-11-21T14:58:34Z")

</div>

You need to rescale `cannyFilter->GetOutput()` to `unsigned char`. Read rescale filter’s [docs](https://itk.org/Doxygen/html/classitk_1_1RescaleIntensityImageFilter.html) and look at examples linked in it.

The IO infrastructure can cast between integral types. Floating point types are not handled due to varying range standards, e.g. some filters produce output in `[0,1]` range, while others might utilize `±inf`.
