<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection">
<div dir="auto">Hi Daniela,
<div dir="auto">within a python script, you need to call the python function image_convert. It takes as input an ITK image. </div>
<div dir="auto">So, say you have a series of dicom files representing a 3D image and you want to convert it to float, you could do: </div>
<div dir="auto"><br /></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">import os</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">import fnmatch</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">import itk</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">import gatetools as gt</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"><br /></span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"># get file in current folder and filter out those ending on .dcm</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">dicomfiles = fnmatch.filter(os.listdir('./'), '*.dcm’)</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"># read them in and get them as ITK image</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">itk_image = gt.read_dicom(dicomfiles)</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"># pass the ITK image to the convert function and specify to which type you want to convert</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">converted_image = gt.image_convert(itk_image, pixeltype=‘float’)</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"># you get again an ITK image and yu can continue manipulating it as you wish.</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"># You may dump it with:</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">itk.imwrite(converted_image, ‘converted_image.mhd’)</span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"><br /></span></div>
<div dir="auto">(I haven’t tested this bit of code, but it should work.)</div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);"><br /></span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">Technical remark for python newcomers: </span></div>
<div dir="auto"><span style="color: var(--textColor); background-color: var(--backgroundColor);">When writing gt. preceding the function name, you are referring to the namespace of the imported module. The module’s name gatetools has been aliased to gt for brevity. That is not mandatory, but often practical. </span><br /></div>
<div dir="auto"><br /></div>
<div dir="auto">Hope that helps. </div>
</div>
</div>
<div name="messageSignatureSection"><br />
<div class="matchFont">Best,<br />
Nils</div>
</div>
<div name="messageReplySection">On 30 Jan 2020 at 12:56 +0100, Botnariuc, Daniela <daniela.botnariuc.19@ucl.ac.uk>, wrote:<br />
<blockquote type="cite" class="spark_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;">
<div class="WordSection1">
<p class="MsoNormal">Dear GATE users,</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I have been using the gatetools (thank you for this great contribution!) from the command line but I would like to run them from a python script. I am not as familiar with python as I should be to understand the code in detail. I tried to transform the bash scripts that run the tools from the command line to python files but I am having problems with the type of input/output the functions require.</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">For example, for the gt_image_convert, how should I write my input (path to the dicom folder) and output in python to run the function?</p>
<p class="MsoNormal">I thought it would be something like: (but I am missing something)</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">gt_image_convert('/home/gate/Documents/dicom_folder','output.mhd','float')</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Thank you. I look forward to hearing from you.</p>
<p class="MsoNormal">Kind regards,</p>
<p class="MsoNormal">Daniela</p>
<p class="MsoNormal"></p>
<p class="MsoNormal"> </p>
</div>
_______________________________________________<br />
Gate-users mailing list<br />
Gate-users@lists.opengatecollaboration.org<br />
http://lists.opengatecollaboration.org/mailman/listinfo/gate-users<br /></blockquote>
</div>
</body>
</html>