2

I have pictures that I want to resize as they are currently quite big. The pictures are supposed to be going to Power BI and Power BI has the maximum limitation of around 32k base64 string. I created a function to resize the image but the image has become blurry and less visible after resizing. The length of the base64 image of 1 picture was around 150,000 which came down to around 7000.

  # Converting into base64
  outputBuffer = BytesIO()              
  img2.save(outputBuffer, format='JPEG')
  bgBase64Data = outputBuffer.getvalue()
  # Creating a new column for highlighted picture as base64
  #image_base64_highlighted =  base64.b64encode(bgBase64Data).decode() ## http://stackoverflow.com/q/16748083/2603230
  
  #print(img2)
  resize_factor = 30000/len(base64.b64encode(bgBase64Data))
  im = Image.open(io.BytesIO(bgBase64Data))
  out = im.resize( [int(resize_factor * s) for s in im.size] )
  output_byte_io = io.BytesIO()
  out.save(output_byte_io, 'JPEG')
  final = output_byte_io.getvalue()
  image_base64_highlighted = base64.b64encode(final).decode()

I think it is shrinking the image too much. Is there anyway I can improve the visibility of the image. I want to be able to see at least the text in the image. I cannot post the images due to PII. Any idea?

hkay
  • 159
  • 1
  • 2
  • 12
  • 1
    *I think it is shrinking the image too much* .. *Any idea?* - Shrink it less.You calucaled the `resize_factor` in your code, so adjust it to your needs. – jps Jan 24 '23 at 16:44
  • You're right. I increased the number in resize_factor from 30,000 to 100,000 and it is clear now. My concern is, this will be going to production and I want to make sure the code is written in a way that it automatically takes care of it. Is it a good approach i.e. hard coding it from 30,000 to 100,000? – hkay Jan 24 '23 at 16:48

2 Answers2

2

Encoding with base64 adds around 30% to your image size, so you should aim for a JPEG size of 24kB to ensure it remains under 32kB when encoded.

You can reduce an image to a target size of 24kB using my answer here.


You can also use wand to reduce the quality of a JPEG till it reaches a certain size:

from wand.image import Image
import io

# Create a canvas with noise to make incompressible
with Image(width=640, height=480, pseudo='xc:') as canvas:
    canvas.noise('random')
    # This is the critical line that enforces a max size for your JPEG
    canvas.options['jpeg:extent'] = '72kb'
    jpeg = canvas.make_blob('jpeg')
    print(f'JPEG size: {len(jpeg)}')

You can do the same thing in the command-line by shelling out to ImageMagick:

magick INPUT.JPG -define jpeg:extent=24kb OUTPUT.JPG
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

I think you can do that with pygame itself. But its recommended for you to try open-cv python for this. I think you should use cv2.resize(). And the parameters are;

source : Input Image array (Single-channel, 8-bit or floating-point)

dsize : Size of the output array

dest : Output array (Similar to the dimensions and type of Input image array)

fx : Scale factor along the horizontal axis

fy : Scale factor along the vertical axis interpolation: One of the above interpolation methods

Pranav J
  • 82
  • 6
  • 1
    Thanks Pranav. I want to exhaust all the options if possible before moving from PIL due to the limitation from my work. Although this sounds very promising. I will give it a shot and see how this works if I can't get it to work with the existing libraries I'm using. – hkay Jan 24 '23 at 16:54