-1

I have images on my computer and i need to sort them out. Can i sort images by imagemagick or python by dimensions into folders. For example

  1. Image width or height greater than 3000 will belong to folder 1.
  2. Image width or height greater that 2000 will belong to folder 2.

Image dimensions : Widthxheight = 3500x500 or 500x3500 = It belong to folder 1 Image dimensions : Widthxheight = 2500x500 or 500x2500 = It belong to folder 2

Take only the greater value from the image dimension and sort out into folders.

Thank you.

1 Answers1

0

You could hack something together with the help of the Pillow image library.

First, you would need to install it, ideally into a virtualenv (see also: What is a virtualenv, and why should I use one?):

$ pip install Pillow

Then, assuming your image directory is '/your/images' (yours will likely be different), you could scan images therein:

>>> import PIL.Image
>>> import pathlib
>>> images = pathlib.Path('/your/images')
>>> for path in images.iterdir():
...     # Skip directories
...     if not path.is_file():
...         continue
...     # TODO: Handle image decoding errors.
...     image = PIL.Image.open(path)
...     size = max(image.size)
...     if size > 3000:
...         # TODO: Actually move to folder 1.
...         print(f'{image} ==> folder 1')
...     elif size > 2000:
...         # TODO: Actually move to folder 2.
...         print(f'{image} ==> folder 2')
... 
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3840x2160 at 0x7FC9F3E3CD90> ==> folder 1
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1500x2384 at 0x7FC9F3E42F40> ==> folder 2

I left the actual move into folders 1/2 as an exercise for you. You could use Path.rename() or many other options such as shutil.move().

Vytas
  • 754
  • 5
  • 14