How to fix extension problem in Pillow after resizing image?

Viewed 17

another newcomer here. Just started learning python, this is my first task I gave to myself. Image resize with keeping aspect ratio. Can't figure out how to keep extension when saving new file and how to add '_resized' to name. This is what I fugured out so far:

'''
  from PIL import Image
  import numpy as np
  import os
           
  img = Image.open("unsplash_3.jpg")
  img_name = (img.filename).split('.')[0]
  
  w = img.size[0]
  h = img.size[1]

  aspect = w/h
  
  #horizontal image
  if aspect > 1:   
     new_w = 1000
     new_h = np.round(new_w / aspect).astype(int)
  
  #vertical image
  elif aspect < 1: 
      new_h = 1000
      new_w = np.round(new_h*aspect).astype(int)
  
  img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)

  img.save(img_name, 'jpeg', )

  print (img_name)
'''

Using latest Python 3 and Pillow library.

Big thnx

0 Answers
Related