This question already has an answer here:

I want to resize multiple images that are stored in multiple sub folders in a directory. And I want to replace those images with resized ones by deleting the original ones

Why?

I’ve folder icons for a lot of content in a separate partition. Some of these images are around a megabyte and are 500x500 which is in my opinion overkill for folder icons on 1080p display and I think it’s also reducing performance of nautilus and taking up some valuable space

How is the folder structure?

├── [4.0K]  Zombieland (2009) H
│   ├── [664K]  .folder.png
│   └── [606M]  Zombieland (2009) H.mkv
└── [4.0K]  Zootopia (2016)
    ├── [203K]  .folder.png
    ├── [2.7G]  Zootopia (2016).mkv
    └── [119K]  Zootopia (2016).srt

I want those .folder.png ‘s resized to 160x160. Answer should work on Deep directories.

Other data

  • Images are generally .png but also .icns, .ico
  • Images can be stored in really deep directories
  • all the images have 1:1 aspect ratio
  • all the images are named .folder.extension
  • There is an answer here but I think it only works on one image

I'll be glad to provide more information

marked as duplicate by Sumeet Deshmukh, muru command-line May 14 '17 at 12:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    @SundarLabhar I think it's a little different – Sumeet Deshmukh May 14 '17 at 10:48
  • 1
    There are multiple answers there, including at least one that works on multiple images. – muru May 14 '17 at 12:24
  • @muru after noticing that, I also marked it as duplicate, thanks – Sumeet Deshmukh May 14 '17 at 12:27
up vote 2 down vote accepted

I found an easy solution, run these following commands

sudo apt-get install nautilus-image-converter
nautilus -q
  • Now go to the directory using Nautilus
  • Make sure Show hidden files is enabled
  • Make a search for .folder
  • Select all the files you want to
  • Right click and select Resize Images
  • Select image size as custom size option and fill width = 160 and Height = 160
  • And file name as Resize in Place
  • Press Enter

Your images should be resized to 160

Source


Strangely it didn't work on .icns

Not command line, but this python script should do the job for you :)

run it in the root directory you want to affect.

import PIL,os,glob

DIMENSIONS = (160,160)
FILETYPES = ['*.ico','*.icns','*.png']


def get_pictures_from_directory(subject_path,filetypes):
    lst = []
    for extension in filetypes:
        lst.extend(glob.glob(subject_path+"/"+extension))
    return (lst)

def get_folders_in_curr_directory(directory):
    return ([d for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d))])


def load_and_resize_image(img_path,size_tuple):
    img = PIL.Image.open(img_path)
    img = img.resize(size_tuple)
    return (img)

def save_image(img,img_path):
    img.save(img_path)


def resize_pictures(pictures,DIMENSIONS):
    for picture in pictures:
        img = load_and_resize_image(picture,DIMENSIONS)
        save_image(img,picture)

def run_recursive_resize(base_path,DIMENSIONS,FILETYPES):
    directories = get_folders_in_curr_directory(base_path)
    pictures = get_pictures_from_directory(base_path,FILETYPES)
    resize_pictures(pictures,DIMENSIONS)
    for directory in directories:
         next_path = base_path +'/'+ directory
         run_recursive_resize(next_path,DIMENSIONS,FILETYPES)

run_recursive_resize('.',DIMENSIONS,FILETYPES)

Not the answer you're looking for? Browse other questions tagged or ask your own question.