fdintino / pillow-avif-plugin

A pillow plugin that adds avif support via libavif
BSD 2-Clause "Simplified" License
77 stars 12 forks source link

Converted AVIF files larger than source images #11

Closed vsuvarna closed 9 months ago

vsuvarna commented 2 years ago

I have a small concern regarding this plugin. I'm using it to convert jpegs pulled from the internet to avif images. In doing so, the operation is performed successfully, but the resulting file is often larger than the source jpeg. One key reason I was converting the images is to reduce file size and save bandwidth. I would appreciate any insight or help as to why this is happening. Is there a step I'm doing incorrectly or does the plugin need a fix.

Here's the code I'm using:

#Save poster files
poster_small_image = requests.get(poster_small_url)
with open(f'.\TMDB_posters_jpeg\small\\{movie_id}.jpeg', 'wb') as f:
          f.write(poster_small_image.content)

poster_medium_image = requests.get(poster_medium_url)
with open(f'.\TMDB_posters_jpeg\medium\\{movie_id}.jpeg', 'wb') as f:
          f.write(poster_medium_image.content)

#Convert to AVIF
with Image.open(f'.\TMDB_posters_jpeg\medium\\{movie_id}.jpeg') as poster_medium_jpeg:
    poster_medium_jpeg.save(f'.\TMDB_posters_avif\medium\\{movie_id}.avif')

with Image.open(f'.\TMDB_posters_jpeg\small\\{movie_id}.jpeg') as poster_small_jpeg:
    poster_small_jpeg.save(f'.\TMDB_posters_avif\small\\{movie_id}.avif')

Thank you.

fdintino commented 2 years ago

Like any of the other image codecs, the size of the file is determined by the arguments passed to save, the most important of which is quality. See these docs for a complete list of the options that can be passed to save() for AVIF images.

This comes up often enough in the issues that I should perhaps decrease the default quality.

vsuvarna commented 2 years ago

Thank you fdintino for the response. What you say makes perfect sense. I was a little confused why my avifs were larger than the original images, while the same operation through ffmpeg resulted in much smaller sizes.

I tried adding the parameters as suggested and it works perfectly. For me 60 was a good number for quality, I added additional flags for the aom codec and speed down to 1, to help the compressor take its time for better results.

If you have any further suggestions regards to these settings or further optimizations, please do share. Thanks again for pointing me towards the right direction and helping me solve this problem.