blob: 5bf8cd5cb3c856df427fcda2845b0ed15be7ddd8 [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001#
2# File : pycimg.py
3# ( Python file )
4#
5# Description : Show how to import .cimg and .cimgz files into python (numpy).
6# This file is a part of the CImg Library project.
7# ( http://cimg.eu )
8#
9# Copyright : Antonio Albiol, Universidad Politecnica Valencia (SPAIN)
10#
11# In case of issues or comments contact Antonio Albiol at:
12# aalbiol (at) dcom.upv.es
13#
14# Licenses : This file is 'dual-licensed', you have to choose one
15# of the two licenses below to apply.
16#
17# CeCILL-C
18# The CeCILL-C license is close to the GNU LGPL.
19# ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html )
20#
21# or CeCILL v2.1
22# The CeCILL license is compatible with the GNU GPL.
23# ( http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.html )
24#
25# This software is governed either by the CeCILL or the CeCILL-C license
26# under French law and abiding by the rules of distribution of free software.
27# You can use, modify and or redistribute the software under the terms of
28# the CeCILL or CeCILL-C licenses as circulated by CEA, CNRS and INRIA
29# at the following URL: "http://www.cecill.info".
30#
31# As a counterpart to the access to the source code and rights to copy,
32# modify and redistribute granted by the license, users are provided only
33# with a limited warranty and the software's author, the holder of the
34# economic rights, and the successive licensors have only limited
35# liability.
36#
37# In this respect, the user's attention is drawn to the risks associated
38# with loading, using, modifying and/or developing or reproducing the
39# software by the user in light of its specific status of free software,
40# that may mean that it is complicated to manipulate, and that also
41# therefore means that it is reserved for developers and experienced
42# professionals having in-depth computer knowledge. Users are therefore
43# encouraged to load and test the software's suitability as regards their
44# requirements in conditions enabling the security of their systems and/or
45# data to be ensured and, more generally, to use and operate it in the
46# same conditions as regards security.
47#
48# The fact that you are presently reading this means that you have had
49# knowledge of the CeCILL and CeCILL-C licenses and that you accept its terms.
50#
51
52import numpy as np
53import zlib
54import os
55
56typesDict={'float':'float32' ,'double':'float64',
57'unsigned_short':'uint16','unsigned_char':'uint8',
58'int':'int32', 'short':'int16'}
59
60def cimgread( filename ):
61 """ USAGE: a= cimgread(filename)
62 For CImg Images:
63 * returns a npy array in the case of cimg
64 * Supports compression
65 * It squeezes singleton dimensions. If a CImg image has dimensions (w,h,1,c)
66 the returned python object will have shape
67 a.shape --> (h,w,c)
68 * a(y,x,z,c) to access one element
69 For CImgList:
70 * returns a list of npy arrays
71 * if original CImgList has nimages, then
72 len(a) --> nimages
73 * To access one pixel of the j-th image use a[j](y,x,z,c)
74
75 """
76
77 basename, file_extension = os.path.splitext(filename)
78 fa = open(filename, 'rb')
79
80 out =[]
81 line0 = fa.readline() #Endiannes
82 tiposdato=line0.split()
83 number_of_images=int(tiposdato[0])
84 datatypecimg=tiposdato[1].decode()
85 endiannes = tiposdato[2]
86
87 datatype = typesDict[datatypecimg];
88
89 for n in range(number_of_images):
90 line1 = fa.readline() # Dimensions
91 dimensiones = line1.split()
92 width = int(dimensiones[0]);
93 height = int(dimensiones[1]);
94 depth = int(dimensiones[2]);
95 spectrum = int(dimensiones[3]);
96
97 if file_extension == '.cimgz':
98 csize= int(dimensiones[4].decode()[1:])
99 data = fa.read(csize)
100 data = zlib.decompress(data)
101 else:
102 data = fa.read(width*height*depth*spectrum*dtype(datatype).itemsize)
103
104 flattened = np.frombuffer(data,dtype=datatype)
105
106 cimg=flattened.reshape((spectrum,depth,height,width))
107 cimg=np.squeeze(np.transpose(cimg,(2,3,1,0)))
108 out.append(cimg)
109
110 fa.close()
111 if len(out)==1:
112 return out[0]
113 return out