1

I have a numpy matrix with data of type np.uint8 which I want to convert into a .jpg image without saving it as a file.

I want to be able to adjust the compression and to read the resulting size in bytes / bits of that .jpg file.

Is that possible?

So far I tried the following, but do not know how to adjust the compression and how to get the resulting size of the .jpg file:

from PIL import Image
from io import BytesIO
import numpy as np

n=100
I_np = np.random.randint(0,255,size=(n,n),dtype=np.uint8)
I = Image.fromarray(I_np)

with BytesIO() as f:
    I.save(f, format='JPEG')
sbha
  • 9,802
  • 2
  • 74
  • 62
Gilfoyle
  • 3,282
  • 3
  • 47
  • 83

0 Answers0