Open
Description
I am trying to convert between numpy and vips image formats, using vips rotate function then converting back to numpy.
I have a function to convert from numpy to vips, based off the documentation
# Function to convert an image from numpy format to vips
def convert_to_vips(np_image):
dtype_to_format = {
'uint8': 'uchar',
'int8': 'char',
'uint16': 'ushort',
'int16': 'short',
'uint32': 'uint',
'int32': 'int',
'float32': 'float',
'float64': 'double',
'complex64': 'complex',
'complex128': 'dpcomplex',
}
height, width, bands = np_image.shape
linear = np_image.reshape(width * height * bands)
vips_image = pyvips.Image.new_from_memory(linear.data, width, height, bands, dtype_to_format[str(np_image.dtype)])
return vips_image
However when I ry to convert from vips to numpy I get massive ram usage (16gb computer vips uses 12gb+ then the process kills). My conversion function is
# Function to convert from vips format to numpy
def convert_to_numpy(vips_image):
format_to_dtype = {
'uchar': np.uint8,
'char': np.int8,
'ushort': np.uint16,
'short': np.int16,
'uint': np.uint32,
'int': np.int32,
'float': np.float32,
'double': np.float64,
'complex': np.complex64,
'dpcomplex': np.complex128,
}
# Loading a vips image into memory causes a massive memory spike, in excess of all 16g's of ram so... dont know why it does this
#buffer=vips_image.write_to_memory()
#np_image = np.ndarray(buffer, dtype=format_to_dtype[vips_image.format], shape=[vips_image.height, vips_image.width, vips_image.bands])
vips_image.write_to_file("temp.tif", )
np_image = imread("temp.tif")
os.remove("temp.tif")
return np_image
Note that I was using write_to_memory() thought that was the issue but it also does this on write_to_file()
My full program code is:
# Function to convert from vips format to numpy
def convert_to_numpy(vips_image):
format_to_dtype = {
'uchar': np.uint8,
'char': np.int8,
'ushort': np.uint16,
'short': np.int16,
'uint': np.uint32,
'int': np.int32,
'float': np.float32,
'double': np.float64,
'complex': np.complex64,
'dpcomplex': np.complex128,
}
# Loading a vips image into memory causes a massive memory spike, in excess of all 16g's of ram so... dont know why it does this
#buffer=vips_image.write_to_memory()
#np_image = np.ndarray(buffer, dtype=format_to_dtype[vips_image.format], shape=[vips_image.height, vips_image.width, vips_image.bands])
vips_image.write_to_file("temp.tif", )
np_image = imread("temp.tif")
os.remove("temp.tif")
return np_image
# Function to convert an image from numpy format to vips
def convert_to_vips(np_image):
dtype_to_format = {
'uint8': 'uchar',
'int8': 'char',
'uint16': 'ushort',
'int16': 'short',
'uint32': 'uint',
'int32': 'int',
'float32': 'float',
'float64': 'double',
'complex64': 'complex',
'complex128': 'dpcomplex',
}
height, width, bands = np_image.shape
linear = np_image.reshape(width * height * bands)
vips_image = pyvips.Image.new_from_memory(linear.data, width, height, bands, dtype_to_format[str(np_image.dtype)])
return vips_image
def large_img_rotate(image, angle):
img = convert_to_vips(image)
img_rotated = img.rotate(angle, interpolate=pyvips.Interpolate.new("nearest"))
img = convert_to_numpy(img_rotated)
return img
img = imread('photo.tif')
rotated_img = large_img_rotate(img, 346.2)
cv2.imwrite('rotated_photo.tif', rotated_img)