def read_image_data(self): if self.imdtype == 'not_implemented': raise AttributeError, "image data type: %s" % self.imdtype if ('packed' in self.imdtype): return self.read_packed_complex() elif ('rgb' in self.imdtype): return self.read_rgb() else: data = binIO.read_data_array(self.filename, self.imbytes, self.byte_offset, self.imdtype) imsize = self.imsize.tolist() if self.order == 'F': if self.record_by == 'spectrum': swapelem(imsize, 0, 1) data = data.reshape(imsize, order = self.order) data = np.swapaxes(data, 0, 1).copy() elif self.record_by == 'image': data = data.reshape(imsize, order = 'C') elif self.order == 'C': if self.record_by == 'spectrum': data = data.reshape(np.roll(self.imsize,1), order = self.order) data = np.rollaxis(data, 0, self.dim).copy() elif self.record_by == 'image': data = data.reshape(self.imsize, order = self.order) return data
def read_rgb(self): self.imsize = list(self.imsize) self.imsize.append(4) self.imsize = tuple(self.imsize) data = binIO.read_data_array(self.filename, self.imbytes, self.byte_offset) data = data.reshape(self.imsize, order="C") # (B, G, R, A) if self.imdtype == "rgb": data = data[:, :, -2::-1] # (R, G, B) self.mode += "rgb_" self.imsize = list(self.imsize) self.imsize[-1] = self.imsize[-1] - 1 self.imsize = tuple(self.imsize) elif self.imdtype == "argb": data = np.concatenate((data[:, :, -2::-1], data[:, :, -1:]), axis=2) # (R, G, B, A) self.mode += "rgba_" return data
def read_rgb(self): self.imsize = list(self.imsize) self.imsize.append(4) self.imsize = tuple(self.imsize) data = binIO.read_data_array(self.filename, self.imbytes, self.byte_offset) data = data.reshape(self.imsize, order='C') # (B, G, R, A) if self.imdtype == 'rgb': data = data[:, :, -2::-1] # (R, G, B) self.mode += 'rgb_' self.imsize = list(self.imsize) self.imsize[-1] = self.imsize[-1] - 1 self.imsize = tuple(self.imsize) elif self.imdtype == 'argb': data = np.concatenate((data[:, :, -2::-1], data[:, :, -1:]), axis=2) # (R, G, B, A) self.mode += 'rgba_' return data
def read_image_data(self): if self.imdtype == "not_implemented": raise AttributeError, "image data type: %s" % self.imdtype if "packed" in self.imdtype: return self.read_packed_complex() elif "rgb" in self.imdtype: return self.read_rgb() else: data = binIO.read_data_array(self.filename, self.imbytes, self.byte_offset, self.imdtype) imsize = self.imsize.tolist() if self.order == "F": if self.record_by == "spectrum": swapelem(imsize, 0, 1) data = data.reshape(imsize, order=self.order) data = np.swapaxes(data, 0, 1).copy() elif self.record_by == "image": data = data.reshape(imsize, order="C") elif self.order == "C": if self.record_by == "spectrum": data = data.reshape(np.roll(self.imsize, 1), order=self.order) data = np.rollaxis(data, 0, self.dim).copy() elif self.record_by == "image": data = data.reshape(self.imsize, order=self.order) return data
def read_packed_complex(self): if (self.imsize[0] != self.imsize[1]) or (len(self.imsize)>2): msg = "Packed complex format works only for a 2Nx2N image" msg += " -> width == height" print msg raise ImageModeError('FFT') # print "This image is likely a FFT and each pixel is a complex number" # print "You might want to display its complex norm" # print "with a logarithmic intensity scale: log(abs(IMAGE))" self.mode += 'FFT_' N = int(self.imsize[0] / 2) # think about a 2Nx2N matrix # read all the bytes as 1D array of 4-Byte float tmpdata = binIO.read_data_array(self.filename, self.imbytes, self.byte_offset, 'float32') # create an empty 2Nx2N ndarray of complex data = np.zeros(self.imsize, 'complex64', 'C') # fill in the real values: data[N, 0] = tmpdata[0] data[0, 0] = tmpdata[1] data[N, N] = tmpdata[2*N**2] # Nyquist frequency data[0, N] = tmpdata[2*N**2+1] # Nyquist frequency # fill in the non-redundant complex values: # top right quarter, except 1st column for i in xrange(N): # this could be optimized start = 2 * i * N + 2 stop = start + 2 * (N - 1) - 1 step = 2 realpart = tmpdata[start:stop:step] imagpart = tmpdata[start+1:stop+1:step] data[i, N+1:2*N] = realpart + imagpart * 1j # 1st column, bottom left quarter start = 2 * N stop = start + 2 * N * (N - 1) - 1 step = 2 * N realpart = tmpdata[start:stop:step] imagpart = tmpdata[start+1:stop+1:step] data[N+1:2*N, 0] = realpart + imagpart * 1j # 1st row, bottom right quarter start = 2 * N**2 + 2 stop = start + 2 * (N - 1) - 1 step = 2 realpart = tmpdata[start:stop:step] imagpart = tmpdata[start+1:stop+1:step] data[N, N+1:2*N] = realpart + imagpart * 1j # bottom right quarter, except 1st row start = stop + 1 stop = start + 2 * N * (N - 1) - 1 step = 2 realpart = tmpdata[start:stop:step] imagpart = tmpdata[start+1:stop+1:step] complexdata = realpart + imagpart * 1j data[N+1:2*N, N:2*N] = complexdata.reshape(N-1, N) # fill in the empty pixels: A(i)(j) = A(2N-i)(2N-j)* # 1st row, top left quarter, except 1st element data[0, 1:N] = np.conjugate(data[0, -1:-N:-1]) # 1st row, bottom left quarter, except 1st element data[N, 1:N] = np.conjugate(data[N, -1:-N:-1]) # 1st column, top left quarter, except 1st element data[1:N, 0] = np.conjugate(data[-1:-N:-1, 0]) # 1st column, top right quarter, except 1st element data[1:N, N] = np.conjugate(data[-1:-N:-1, N]) # top left quarter, except 1st row and 1st column data[1:N, 1:N] = np.conjugate(data[-1:-N:-1, -1:-N:-1]) # bottom left quarter, except 1st row and 1st column data[N+1:2*N, 1:N] = np.conjugate(data[-N-1:-2*N:-1, -1:-N:-1]) return data
def read_packed_complex(self): if (self.imsize[0] != self.imsize[1]) or (len(self.imsize) > 2): msg = "Packed complex format works only for a 2Nx2N image" msg += " -> width == height" print msg raise ImageModeError("FFT") # print "This image is likely a FFT and each pixel is a complex number" # print "You might want to display its complex norm" # print "with a logarithmic intensity scale: log(abs(IMAGE))" self.mode += "FFT_" N = int(self.imsize[0] / 2) # think about a 2Nx2N matrix # read all the bytes as 1D array of 4-Byte float tmpdata = binIO.read_data_array(self.filename, self.imbytes, self.byte_offset, "float32") # create an empty 2Nx2N ndarray of complex data = np.zeros(self.imsize, "complex64", "C") # fill in the real values: data[N, 0] = tmpdata[0] data[0, 0] = tmpdata[1] data[N, N] = tmpdata[2 * N ** 2] # Nyquist frequency data[0, N] = tmpdata[2 * N ** 2 + 1] # Nyquist frequency # fill in the non-redundant complex values: # top right quarter, except 1st column for i in xrange(N): # this could be optimized start = 2 * i * N + 2 stop = start + 2 * (N - 1) - 1 step = 2 realpart = tmpdata[start:stop:step] imagpart = tmpdata[start + 1 : stop + 1 : step] data[i, N + 1 : 2 * N] = realpart + imagpart * 1j # 1st column, bottom left quarter start = 2 * N stop = start + 2 * N * (N - 1) - 1 step = 2 * N realpart = tmpdata[start:stop:step] imagpart = tmpdata[start + 1 : stop + 1 : step] data[N + 1 : 2 * N, 0] = realpart + imagpart * 1j # 1st row, bottom right quarter start = 2 * N ** 2 + 2 stop = start + 2 * (N - 1) - 1 step = 2 realpart = tmpdata[start:stop:step] imagpart = tmpdata[start + 1 : stop + 1 : step] data[N, N + 1 : 2 * N] = realpart + imagpart * 1j # bottom right quarter, except 1st row start = stop + 1 stop = start + 2 * N * (N - 1) - 1 step = 2 realpart = tmpdata[start:stop:step] imagpart = tmpdata[start + 1 : stop + 1 : step] complexdata = realpart + imagpart * 1j data[N + 1 : 2 * N, N : 2 * N] = complexdata.reshape(N - 1, N) # fill in the empty pixels: A(i)(j) = A(2N-i)(2N-j)* # 1st row, top left quarter, except 1st element data[0, 1:N] = np.conjugate(data[0, -1:-N:-1]) # 1st row, bottom left quarter, except 1st element data[N, 1:N] = np.conjugate(data[N, -1:-N:-1]) # 1st column, top left quarter, except 1st element data[1:N, 0] = np.conjugate(data[-1:-N:-1, 0]) # 1st column, top right quarter, except 1st element data[1:N, N] = np.conjugate(data[-1:-N:-1, N]) # top left quarter, except 1st row and 1st column data[1:N, 1:N] = np.conjugate(data[-1:-N:-1, -1:-N:-1]) # bottom left quarter, except 1st row and 1st column data[N + 1 : 2 * N, 1:N] = np.conjugate(data[-N - 1 : -2 * N : -1, -1:-N:-1]) return data