aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.42k stars 2.44k forks source link

in-memory JPEG augmentations #811

Open serycjon opened 2 years ago

serycjon commented 2 years ago

Hi, it seems JPEG augmentations are using temporary file on disk to do the JPEG compression: https://github.com/aleju/imgaug/blob/0101108d4fed06bc5056c4a03e2bcb0216dac326/imgaug/augmenters/arithmetic.py#L1747

This can be done in-memory (-> probably more efficiently) using io.BytesIO(). Something like the following:

import io
from PIL import Image
import numpy as np

pil_image = Image.fromarray(np_rgb_img)
buf = io.BytesIO()
pil_image.save(buf, format='JPEG', quality=0.25)
pil_image = Image.open(buf)
np_img_with_compression = np.asarray(pil_image)