2

I have a large folder of images of different filetypes (jpg + png). I would like to iterate through the folder and convert any pngs to jpegs. So for example image323.png would be converted to image323.jpg.

I would also like them to be resized so that they are all the same size without losing their scale/ratio.

I know I need to loop through them and probably use a conditional (if file ends in png, change to jpg) and this will involve using PIl but not sure how to put it all together.

heroZero
  • 173
  • 3
  • 18
  • Just so you are aware, stackoverflow is not a code writing service. To avoid your post being closed as off topic you are expected to provide code for what you have tried and what specific problem you are having issues with. – alexanderhurst Jun 12 '19 at 14:33
  • But to help you get started check out [for every file in a directory](https://stackoverflow.com/a/1120736/10626861) and [image conversion](https://stackoverflow.com/a/43258974/10626861). Good luck :) – alexanderhurst Jun 12 '19 at 14:35
  • Thanks Alex, that was lazy of me. I'll have a look through your links and come back with something more construcutive..apologies and thanks. – heroZero Jun 12 '19 at 14:43
  • What OS do you use? And is there a chance of collision where you already have `image123.jpg` but you find an `image123.png` which is going to clash when converted? – Mark Setchell Jun 12 '19 at 15:28
  • Hey again @MarkSetchell I am using a mac, but have access to linux too if makes any difference. – heroZero Jun 12 '19 at 15:31
  • Would you like a fast, one-liner parallel solution using GNU Parallel and ImageMagick, both of which you can install on macOS with **homebrew**? – Mark Setchell Jun 12 '19 at 15:37
  • Absolutely, sounds great ..faster and easier the better I guess! Actually have updated OP which might make your solution not possible? – heroZero Jun 12 '19 at 15:43

1 Answers1

2

The easiest way would be with ImageMagick which you can install on macOS with homebrew:

brew install imagemagick

Make a backup copy first, then you just need to go into the directory with the images and run:

magick mogrify -format jpg *.png

If you want them resized so that they all end up 600 pixels wide:

magick mogrify -format jpg -resize 600x *.png

If you want them resized so that they all end up 300 pixels tall:

magick mogrify -format jpg -resize x300 *.png

If you want them resized so that they all end up no more than 600 pixels wide and no more than 300 pixels tall:

magick mogrify -format jpg -resize 600x300 *.png

If you want them resized so that they all end up EXACTLY 600 pixels wide and EXACTLY 300 pixels tall even if that means distorting the original aspect ratio:

magick mogrify -format jpg -resize 600x300\! *.png

Depending on the number of images you have, how big they are and how fast your CPU and disk subsystem are, you may get on better with GNU Parallel which you can install the same way:

brew install parallel

The command to convert all PNG files to JPEGs in parallel then becomes:

parallel --dry-run convert {} -resize 600x {.}.jpg ::: *png
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks mark, formatting seems to work ...just one questin, is there a way to remove duplicates ...as in can I get delete/tm all pngs once they have been converted to jpg? – heroZero Jun 12 '19 at 15:58
  • 1
    You can delete all PNG files with `rm *png` – Mark Setchell Jun 12 '19 at 16:27
  • 1
    Or you can open **Finder** with `open .` and click **List View** and **Kind** and then all the PNGs will be listed together, so you can click the first PNG file, then shift-click the last PNG file and press CMD+DEL to delete the selected files. – Mark Setchell Jun 12 '19 at 16:34
  • 1
    You might need to be careful if you have two images with the same name and one is PNG and the other is JPG, but the images are different. Mogrify will overwrite the old JPG with the new PNG. Is that likely with your images? – fmw42 Jun 12 '19 at 16:38