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

Поделиться
HTML-код
  • Опубликовано: 8 май 2021

Комментарии • 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!

  • @user-xu7jz4li7e
    @user-xu7jz4li7e 9 месяцев назад

    awesome tutorial!

  • @zinaespoir
    @zinaespoir 3 года назад

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

  • @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

  • @siddhantgulia8997
    @siddhantgulia8997 8 месяцев назад +1

    Aspect ratio is width/height

  • @user-xc5co5ce4e
    @user-xc5co5ce4e 3 года назад

    Can you do batch crop please

  • @qxxrd
    @qxxrd 2 месяца назад

    How can I resize an image from a URL?

  • @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

  • @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...

  • @autarroy
    @autarroy 2 года назад +1

    Nice video
    I need your help

    • @thecodebear
      @thecodebear  2 года назад

      Yes , what is it ?

    • @autarroy
      @autarroy 2 года назад

      @@thecodebear I have a small script with diffent size of photo's
      i want to resize all of them and put that in my tkinter script.
      if i send you my small script can you write the code for me.
      i tried it many times but unfortunetly

    • @autarroy
      @autarroy 2 года назад

      @@thecodebear I have a frame (width=100 and height=815)
      how to resize my images in that size and how i can make a new folder to
      put my resized images