I'm using pillow and working with pretty large images (at least 10500 x 10500 px), which in turn uses up quite a lot of memory. I was wondering if there was a way to lower it and tried using a compressed image to load (which would be ~400kb instead of 420mb), instead of directly creating a new one.. but the memory usage is the same:
Line # Mem usage Increment Line Contents
================================================
151 35.969 MiB 0.742 MiB base = Image.open("C:/Users/Nick/Desktop/transparent.png")
152 456.992 MiB 421.023 MiB base.load()
155 877.641 MiB 420.648 MiB base_hallway = Image.new("RGBA", (map_width_px, map_height_px))
I also tried using a jpg or Image.new() with RGB only for the second image, but ditching the alpha channel didn't work either.
Line # Mem usage Increment Line Contents
================================================
151 36.309 MiB 0.766 MiB base = Image.open("C:/Users/Nick/Desktop/transparent.png")
152 457.359 MiB 421.051 MiB base.load()
156 457.367 MiB 0.008 MiB base_hallway = Image.open("C:/Users/Nick/Desktop/blackjpg.jpg")
157 878.312 MiB 420.945 MiB base_hallway.load()
Mainly the operation being run on the base images is pasting other images on top of them in different positions. The rooms or hallways also have operations on them, but use almost no memory in comparison, such as picking the proper position to paste depending on the previous room or hallway, rotating if necessary, etc. But since it requires dozens or even hundreds of items pasted on top, I can't close the base images after every iteration (so only base OR base_hallway is open at any one time). I tried to open the base and base_hallway images only when needed, which requires a lot of save and close operations as well. That ended up increasing the time it takes for the code to run tenfold.. Simplified:
room = Image.open(open_room)
if next_tile == "room":
base.paste(room, box=(rand_width_position, rand_height_position), mask=room)
elif next_tile == "hallway" or next_tile == "junction":
base_hallway.paste(room, box=(rand_width_position, rand_height_position), mask=room)
Is there any way to optimize the memory usage?
Thanks!