Compress PNG images using PIL

I have a problem with increasing the opacity of the image. My original image is 230 KB enter image description here

after resizing the image using the code:

Method 1:  imh=imgg.resize((1000,500),Image.ANTIALIAS) #filesize is 558 KB
Method 2:  imh=imgg.resize((1000,500),Image.ANTIALIAS)
           im2 = imh.convert('P', palette=Image.ADAPTIVE) #filesize is 170KB

Now I add image transparency with this code:

def reduce_opacity(im, opacity,pathname):
    assert opacity >= 0 and opacity <= 1
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    else:
        im = im.copy()
    alpha = im.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    im.putalpha(alpha)
    jj=<pathname>
    im.save(jj)
    return im

Method 1: file size 598 KB Method 2: file size 383 KB

So, the best code I've received so far,

imh=imgg.resize((1000,500),Image.ANTIALIAS)
im2 = imh.convert('P', palette=Image.ADAPTIVE)
reduce_opacity(im2,0.5,name)

which gives me a file size of 383 KB. To add opacity, it should be open in RGBA mode, which increases the file size from 170 KB to 383 KB. I am not happy with this, I need to reduce the size more, is it there somehow, so that I can achieve this and not compromise the quality to a large extent?

+4

Source: https://habr.com/ru/post/1548311/


All Articles