Question: Is there a faser way to save pictures with Pillow in Python?
what happened up to here:
I have a code that creates an image. The image is created by a function. Each pixel of the image is created and should be saved as a single image.
If I do not save this, the calculation runs about 9 sec. If I save it it is 20 sec. Ergo more than 50% of the time is used for saving. It gets even worse when the images get bigger.
The bottelneck is the saving. I switched from PNG -> JPEG, which is about 40% faster.
Is there another way to speed up saving?
from PIL import Image, ImageDraw
import numpy as np
MAX_ITER = 80
def mandelbrot(c):
z = 0
n = 0
while abs(z) <= 2 and n < MAX_ITER:
z = z*z + c
n += 1
return n
WIDTH = 140
HEIGHT = 96
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1
palette = []
im = Image.new('RGB', (WIDTH, HEIGHT), (0, 0, 0))
draw = ImageDraw.Draw(im)
n = 0
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
IM_START + (y / HEIGHT) * (IM_END - IM_START))
m = mandelbrot(c)
color = 255 - int(m * 255 / MAX_ITER)
draw.point([x, y], (color, color, color))
im.save(f'test/output_{n}.jpg', 'JPEG')
n = n + 1
Soucre: https://www.codingame.com/playgrounds/2358/how-to-plot-the-mandelbrot-set/mandelbrot-set