Resize Images in bulk and maintain aspect ratio with python | python project for beginners

Поділитися
Вставка
  • Опубліковано 18 вер 2024

КОМЕНТАРІ • 18

  • @EduardoNicoleit
    @EduardoNicoleit Рік тому +6

    from PIL import Image
    import os
    def resize(im, new_width):
    width, height = im.size
    ratio = height/width
    new_height = int(ratio*new_width)
    resized_image = im.resize((new_width,new_height))
    return resized_image
    files = os.listdir("assets")
    extensions = ['jpg','png','jpeg']
    for file in files:
    ext = file.split('.')[-1]
    if ext in extensions:
    im = Image.open('assets/'+file)
    im_resized = resize(im, 600)
    new_dir = 'resized_images'
    if not os.path.exists(new_dir):
    os.makedirs(new_dir)
    file = f'{new_dir}/{file}.jpg'
    im_resized.save(file, 'png', quality=100)
    Here you add the photos inside another folder called "assets" then it creates another folder with the resized images. Thanks for the video!

  • @zinaespoir
    @zinaespoir 3 роки тому

    I hope you put the code in the description for your followers. Thank you and good job.

  • @立法院很忙
    @立法院很忙 11 місяців тому

    awesome tutorial!

  • @siddhantgulia8997
    @siddhantgulia8997 10 місяців тому +1

    Aspect ratio is width/height

  • @ThanasornsawanVarathanamongkol

    What's about fix height? Is new_width = int(ratio*new_height) ?

  • @chitti9974
    @chitti9974 2 роки тому

    Here you are calculating new height, to maintain aspect ratio, but i want all images to resize to 224 X 224 by maintaining aspect ratio, what more should i do after resizing images as per your code

  • @qxxrd
    @qxxrd 5 місяців тому

    How can I resize an image from a URL?

  • @user-xc5co5ce4e
    @user-xc5co5ce4e 3 роки тому

    Can you do batch crop please

  • @saudagarnegeriseberang8137
    @saudagarnegeriseberang8137 Рік тому

    Hello sir, i tried follow the tutorial step by step. I would love to resize a 150 px image into 1080 px however, the result is still ugly. Could anyone here give me some advices

  • @autarroy
    @autarroy 2 роки тому +1

    Nice video
    I need your help

  • @chitti9974
    @chitti9974 2 роки тому

    Sir,
    i have some medical images with different sizes, I want to resize all of them to 224 X 224 without loosing aspect ratio.....how to do it sir

    • @thecodebear
      @thecodebear  2 роки тому

      You cant make them square shape without loosing the aspect ratio

    • @chitti9974
      @chitti9974 2 роки тому

      @@thecodebear thank you sir

    • @chr0my134
      @chr0my134 2 роки тому

      @@chitti9974 you can box them with padding but it becomes so blurry...