I created a function that uses some python functions through the reticulate package, specially opening the image with PIL :
image <- "~/Desktop/image.jpg" pil.image <- reticulate::import( "PIL.Image", convert = FALSE ) img <- pil.image$open( image )
Then I do a few things with the image (I extract several cultures), which works fine. Here is an example of what I am doing ( outputs is a data frame with the cultures I need, so crop.grid is just a 4- crop.grid vector.
crop.grid <- c( outputs$x.start[x], outputs$y.start[x], outputs$x.stop[x], outputs$y.stop[x] ) crop.grid <- as.integer( crop.grid ) crop.grid <- reticulate::r_to_py( crop.grid ) output.array <- img$crop( box = crop.grid ) output.array$save( output.filename )
After that, I want to clear the image from memory (the images that I open are very large, so they take up a lot of memory). I am trying to close an image in python:
img$close()
As in R:
rm( img ) gc()
And replacing the object with what I know is very small.
img <- reticulate::r_to_py( 1L )
All of these things work fine, but my RAM is still registered as very full. I try them with each of my python objects, but the only thing that effectively clears the RAM is to restart the R session.
I know that in python I would be better off opening an image with with to clear it at the end of the process, but I'm not sure how to implement this with reticulate .
--- UPDATE with a similar version of python :
If I do the above in python directly:
from PIL import Image img = Image.open( "/home/user/Desktop/image.jpg" ) output = img.crop( [0,0,100,100] )
And then close:
output.close() img.close()
The memory is cleared. The same does not work from inside R. That is:
output.array$close() img$close() gc()
Does not clear memory.