mlik
mlik

Reputation: 63

Imagemagick - use mogrify only for files with colorspace CMYK

I need to use a jpg conversion script only for CMYK colorspace files. The problem is that there are a lot of jpg files (colospace sRGB and CMYK) in the folder. I want to use the mogrify function. I've already created a general script to change all files in a folder. I need to create a filter to make the script work only for JPG with CMYK files

cd "C:\Program Files\ImageMagick-7.0.11-Q16-HDRI\"
magick mogrify -profile c:\test\profile\USWebCoatedSWOP.icc -profile "c:\test\profile\sRGB Color Space Profile.icm" -quality 80 c:\test\*.jpg

Upvotes: 0

Views: 461

Answers (2)

mlik
mlik

Reputation: 63

I was able to get the effect I needed. I found a similar script and edited it according to my needs. I am not a programmer so sorry for the syntax. Maybe it will be useful to someone.

@echo off
for /F "delims=" %%I in ('dir /B /S *.jpg') do call :process "%%I"
goto :eof

:process
C:\Progra~1\ImageMagick-7.0.11-Q16-HDRI\magick identify -verbose %1 | find /c /i "Colorspace: CMYK" > c:\test\temp.txt
set /p count=<c:\test\temp.txt
del c:\test\temp.txt
if %count%==1 (
C:\Progra~1\ImageMagick-7.0.11-Q16-HDRI\magick.exe mogrify -profile c:\test\profile\USWebCoatedSWOP.icc -profile c:\test\profile\sRGBColorSpaceProfile.icm -quality 80 "%~dpn1.jpg"
)
goto :eof

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207748

As Fred (@fmw42) says, mogrify can't restrict its processing based on image type like that. However, it can work with a list of filenames. So, if filelist.txt looks like this:

image32.jpg
image43.jpg
...

you can get mogrify to resize those files like this:

magick mogrify -resize 640x480 @filelist.txt

So, now the issue is how to generate a list of CMYK files. I can't tell you exactly how to do that on Windows but I can tell you how to get started and hopefully you can work out the rest.

If you run this command, it will print a line with each image's colourspace and filename into a file:

magick identify -format "%[colorspace]:%f\r\n" *.jpg > filelist.txt

Sample Output

CMYK:a.jpg
sRGB:b.jpg

where:

  • %[colorspace] is the image's colourspace
  • : is just a separator that I chose at random
  • %f is the image filename
  • \r\n are Windows CR+LF line endings.

You then need to select only lines starting CMYK and discard the first field and your job is done. In Linux/macOS, I would use:

magick identify -format "%[colorspace]:%f\n" *.jpg | grep "^CMYK" | cut -d: -f2 > filelist.txt

You can get grep and cut for Windows, but there are probably other (more native) ways of selecting lines in Windows using FOR loops like this.

Upvotes: 3

Related Questions