5

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

Zanna
  • 70,465

2 Answers2

4

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

1

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)
muru
  • 197,895
  • 55
  • 485
  • 740
Wboy
  • 345