Following this post, I successfully reduced the byte size of an image to the desired level, but the problem is that I succeeded to do that only for JPEG. When I try to perform this for, say, png
, the function does not do what is expected:
def get_highest_acceptable_quality(image, filename, target):
q_min, q_max = 1, 96
# Highest acceptable quality found
q_acc = -1
while q_min <= q_max:
m = math.floor((q_min + q_max) / 2)
buffer = BytesIO()
image.save(buffer, format="png", compress_level=m)
s = buffer.getbuffer().nbytes # the s value is always the same for all values of m
...
The strange thing here is that the value of s remains the same irrespective of what the value of m
is. Which is not the case for JPEG (here the entire function works just fine). I am completely lost because I am not a pro in image processing stuff. Any help?