[solved] Batch convert images so they are all JPG with minimal loss of data

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

[solved] Batch convert images so they are all JPG with minimal loss of data

Post by mhulse »

I have a big folder of images (jpg, png, tif, etc.)

I need to convert all of them so they are all JPGs.

I am writing a script that checks the image extension and converts to JPG only if not already a JPG format.

Questions:

1. If the file extension is .jpeg, .jif, .jfif, can I just change the file name extension to ".jpg" without converting (note that I will be skipping files that already end in ".jpg")?

2. When converting, I want to avoid loss of quality (at least, keep it to a minimum - I understand how jpg compression works); with that said, what would be the best command to do the conversion on a lot of unknown file types?

I was hoping that this would do the trick (because it's simple):

convert input.png output.jpg

Will that retain as much of the original image's settings as possible (i.e., resolution, quality, etc.)?

I did a quick test on a JPG:

https://www.diffchecker.com/ERYIXm64

Interestingly, the changes look minimal.

Basically, I want to convert to JPG without changing, or changing only a minimal amount, of the original image as possible.

Thanks in advance!!! :)
Last edited by mhulse on 2016-12-23T11:27:48-07:00, edited 1 time in total.
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch convert images so they are all JPG with minimal loss of data

Post by snibgo »

In IM, all JPG compression is lossy. Compression is controlled by "-quality X" where 0 <= X <= 100. Small numbers are low quality, high compression. High numbers are high quality, low compression. So "-quality 100" gives the least change.
snibgo's IM pages: im.snibgo.com
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Batch convert images so they are all JPG with minimal loss of data

Post by mhulse »

Hi snibgo! Thanks for the quick reply, I really appreciate it!!!

So, looks like I should do:

convert input.png -quality 100 output.jpg

Do you know if that will retain things like resolution? The original images may have various PPI settings.

Basically, all I want to do is normalize the file extensions/types in the folder I'm working on. I plan on just converting non-JPGs ... Once converted, I will save the JPG to the same folder and throw away its old non-JPG file.

Thanks again for the help!
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert images so they are all JPG with minimal loss of data

Post by fmw42 »

1. If the file extension is .jpeg, .jif, .jfif, can I just change the file name extension to ".jpg" without converting (note that I will be skipping files that already end in ".jpg")?
IM will decompress and recompress and thus lose some quality. So you should do that processing from your OS.

Note that some jpg files do not have units set nor possibly density (resolution), so when processing with IM, be sure to set the units, if not specified.

Do you care if sRGB or CMYK or do you need to convert them all to sRGB and add a profile, so they all view properly and consistently on different devices?
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Batch convert images so they are all JPG with minimal loss of data

Post by mhulse »

Hi Fred! Thank you so much for your reply, I really appreciate it! :)

Great questions and tips! Thank you!
fmw42 wrote:IM will decompress and recompress and thus lose some quality. So you should do that processing from your OS.
Perfect! I will do that. :)
fmw42 wrote:Note that some jpg files do not have units set nor possibly density (resolution), so when processing with IM, be sure to set the units, if not specified.
Great! Here's what I have so far:

Code: Select all

convert \
"${args.inputImage}" \
-quality 100 \
-units PixelsPerInch \
-density 300 \
-depth 8 \
"${args.outputImage}" 2> /dev/null
Though, I am curious, if density and depth is already set (before conversion), will IM retain those "settings" when converting to JPG if they are not set in conversion script?
fmw42 wrote:Do you care if sRGB or CMYK or do you need to convert them all to sRGB and add a profile, so they all view properly and consistently on different devices?
In this case, I want to retain the profile and colorspace of the original images. I don't mind if CMYK or RGB. In the end, I will be printing these images and I am using Indesign to manage colors.

Thanks again for your help, I really appreciate it! :)
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert images so they are all JPG with minimal loss of data

Post by fmw42 »

If you specify a units and/or density, then IM will change the image to those you specify. If you do not want that, then you need to check the units and density before processing the image and skip if you like the results. See %x and %y for resolution and %U for units at http://www.imagemagick.org/script/escape.php. Likewise, you can check for profiles with %[profiles] and %[profile:XXX]. You can also check for quality with %[quality] or %Q.

You can also check the format using %m (JPEG, etc) rather than looking at the suffix.

So you would likely need to check and store each of these and then use a bunch of conditionals (depending upon your OS) to decide what IM convert command to call or given one convert command, what variables to insert to include changes.
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Batch convert images so they are all JPG with minimal loss of data

Post by mhulse »

Wow, that's excellent info Fred!!!! Thank you so much for you help!

Thank you snibgo and Fred for your pro help and guidance, I greatly appreciate it! :)
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
Post Reply