using convert with find in script

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?".
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

using convert with find in script

Post by zollsa »

I have a simple script set up to convert everything in a folder up to 2 days oldto jpg, remove the transpanency of any files, and move it to another folder. I am getting an error that I have spent days searching for an answer to. If I get rid of the find command, it seems to work great. I am running Ubuntu 12.04 with imagemagick version imagemagick 8:6.6.9.7-5ubuntu3.2. Below are the 2 script versions, the error, and convert -list configure output. Anything that can be done to fix this?

Error

Code: Select all

convert: unable to open image `/home/scott/test/%[filename:original].JPG':  @ error/blob.c/OpenBlob/2587.
convert: missing an image filename `/home/scott/Pictures/201902.jpg' @ error/convert.c/ConvertImageCommand/3011.
convert: unable to open image `/home/scott/test/%[filename:original]_L.JPG':  @ error/blob.c/OpenBlob/2587.
convert: missing an image filename `/home/scott/Pictures/201902.jpg' @ error/convert.c/ConvertImageCommand/3011.
With find

Code: Select all

find /home/scott/Pictures/* -mtime -2 | xargs convert -set filename:original %t -background white +matte -thumbnail 400x400 /home/scott/test/%[filename:original].JPG
find /home/scott/Pictures/* -mtime -2 | xargs convert -set filename:original %t -background white +matte -thumbnail 160x160 /home/scott/test/%[filename:original]_L.JPG
Without find

Code: Select all

convert /home/scott/Pictures/* -set filename:original %t -background white +matte -thumbnail 400x400 /home/scott/test/%[filename:original].JPG
convert /home/scott/Pictures/* -set filename:original %t -background white +matte -thumbnail 160x160 /home/scott/test/%[filename:original]_L.JPG
convert -list configure

Code: Select all

Path: /usr/share/ImageMagick-6.6.9/configure.xml

Name          Value
-------------------------------------------------------------------------------
CC            gcc -std=gnu99 -std=gnu99
CFLAGS        -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/lqr-1 -fopenmp -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Wall -pthread
CODER_PATH    /usr/lib/ImageMagick-6.6.9/modules-Q16/coders
CONFIGURE     ./configure  '--prefix=/usr' '--sysconfdir=/etc' '--mandir=${prefix}/share/man' '--infodir=${prefix}/share/info' '--with-modules' '--with-gs-font-dir=/usr/share/fonts/type1/gsfonts' '--with-magick-plus-plus' '--with-djvu' '--enable-shared' '--without-dps' '--without-fpx' '--with-perl-options=INSTALLDIRS=vendor' '--x-includes=/usr/include/X11' '--x-libraries=/usr/lib/X11' 'CFLAGS=-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro' 'CPPFLAGS=-D_FORTIFY_SOURCE=2' 'CXXFLAGS=-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security'
CONFIGURE_PATH /etc/ImageMagick/
COPYRIGHT     Copyright (C) 1999-2011 ImageMagick Studio LLC
CPPFLAGS      -I/usr/include/ImageMagick
CXX           g++
CXXFLAGS      -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -pthread
DEFS          -DHAVE_CONFIG_H
DELEGATES     bzlib djvu fontconfig freetype gvc jpeg jng jp2 lcms lqr openexr png rsvg tiff x11 xml wmf zlib
DISTCHECK_CONFIG_FLAGS 'CFLAGS=-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security' 'CPPFLAGS=-D_FORTIFY_SOURCE=2' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro' --disable-deprecated --with-quantum-depth=16 --with-umem=no --with-autotrace=no --with-dps=no --with-fpx=no --with-gslib=no --with-fontpath= --with-gs-font-dir=/usr/share/fonts/type1/gsfonts --with-perl=no
DOCUMENTATION_PATH /usr/share/doc/ImageMagick-6.6.9/
EXEC-PREFIX   /usr
EXECUTABLE_PATH /usr/bin
FEATURES      OpenMP 
FILTER_PATH   /usr/lib/ImageMagick-6.6.9/modules-Q16/filters
HOST          x86_64-unknown-linux-gnu
LDFLAGS       -L/usr/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/X11
LIB_VERSION   0x669
LIB_VERSION_NUMBER 6,6,9,7
LIBRARY_PATH  /usr/lib/ImageMagick-6.6.9
LIBS          -lMagickCore -llcms -ltiff -lfreetype -ljpeg -llqr-1 -lglib-2.0 -lfontconfig -lXext -lSM -lICE -lX11 -lXt -lbz2 -lz -lm -lgomp -lpthread -lltdl
NAME          ImageMagick
PCFLAGS       -fopenmp
PREFIX        /usr
QuantumDepth  16
RELEASE_DATE  2012-08-17
SHARE_PATH    /usr/share/ImageMagick-6.6.9
SVN_REVISION  4345
TARGET_CPU    x86_64
TARGET_OS     linux-gnu
TARGET_VENDOR unknown
VERSION       6.6.9
WEBSITE       http://www.imagemagick.org

Path: [built-in]

Name          Value
-------------------------------------------------------------------------------
NAME          ImageMagick
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: using convert with find in script

Post by glennrp »

find /home/scott/Pictures/*
should be
find /home/scott/Pictures
(without the "/*)
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

Re: using convert with find in script

Post by zollsa »

I made the change and the errors still exist

Code: Select all

convert: unable to open image `/home/scott/test/%[filename:original].JPG':  @ error/blob.c/OpenBlob/2587.
convert: missing an image filename `/home/scott/Pictures/201902.jpg' @ error/convert.c/ConvertImageCommand/3011.
convert: unable to open image `/home/scott/test/%[filename:original]_L.JPG':  @ error/blob.c/OpenBlob/2587.
convert: missing an image filename `/home/scott/Pictures/201902.jpg' @ error/convert.c/ConvertImageCommand/3011.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: using convert with find in script

Post by glennrp »

Maybe it will help to add the "-print" option:

Code: Select all

find /home/scott/Pictures -mtime -2 -print | xargs convert ...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using convert with find in script

Post by anthony »

The problem is that "xargs" adds the file name as an argument -- too the end of the command!
that means the original filename becomes the OUTPUT image, and the given '%[filename:original].JPEG' becomes and input image!


You need to insert the filename as one of the first arguments to convert... for example...

Code: Select all

   .... | xargs -r -I FILE   convert FILE ....test/ FILE.JPG
OR...

have xargs call a shell to handle the argument ($0) in the shell when using a '-c' on command line script. Not this has extra quoting!

Code: Select all

   .... | xargs -n1 sh -c 'convert "$0" ....  "test/$0.JPEG"'
The advantage of these is you have the flename being set from an external source, so you don't need to play with %[...]. The shell version can even do other shell things (like filename modifications, looping, conditional processing).

See IM Examples... Batch Processing Alternatives
http://www.imagemagick.org/Usage/basics/#mogrify_not
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using convert with find in script

Post by anthony »

glennrp wrote:Maybe it will help to add the "-print" option:

Code: Select all

find /home/scott/Pictures -mtime -2 -print | xargs convert ...

The -print is added by default by find when no other 'output' option is given.

A better way is to use -print0 and in xargs add a -0 option (if you system supports it - and likely it does).
This avoids problems with spaces, quotes, and other weird characters in filenames, as long as your command (or script) also 'handles' such problems (few scripting programs can, perl can, shell can not, convert may not handle all filenames).

Under UNIX filename can have any character except NULL (the 0 character code) and '/' (directory separator). The above -print0 thus can use NULL as a separator without causing weird filename problems.


PS: Scripting is MY area of expertise.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

Re: using convert with find in script

Post by zollsa »

anthony wrote:You need to insert the filename as one of the first arguments to convert... for example...

Code: Select all

   .... | xargs -r -I FILE   convert FILE ....test/ FILE.JPG
OR...

have xargs call a shell to handle the argument ($0) in the shell when using a '-c' on command line script. Not this has extra quoting!

Code: Select all

   .... | xargs -n1 sh -c 'convert "$0" ....  "test/$0.JPEG"'
I tried both methods and I am still getting errors. If I use the first method, I get the below errors.

Code: Select all

convert: no decode delegate for this image format `/home/scott/Pictures' @ error/constitute.c/ReadImage/532.
convert: no decode delegate for this image format `/home/scott/test' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/scott/Pictures.JPG' @ error/convert.c/ConvertImageCommand/3011.
convert: no decode delegate for this image format `/home/scott/Pictures' @ error/constitute.c/ReadImage/532.
convert: no decode delegate for this image format `/home/scott/test' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/scott/Pictures_L.JPG' @ error/convert.c/ConvertImageCommand/3011.
If I use the second method, I get the below errors.

Code: Select all

convert: no decode delegate for this image format `/home/scott/Pictures' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/scott/test//home/scott/Pictures.JPG' @ error/convert.c/ConvertImageCommand/3011.
convert: no decode delegate for this image format `/home/scott/Pictures' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/scott/test//home/scott/Pictures.JPG' @ error/convert.c/ConvertImageCommand/3011.
I feel like I am getting more lost than I origionally was. Where do I go from here?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using convert with find in script

Post by anthony »

Your find is not only reporting images, but also sub-directories.

Add to the find something like...

Code: Select all

    -type f  \( -name '*.JPG' -o -name '*.jpg' \)
The -type limits output to just plain files (not directories) while the parenthesis uses '-o' (OR option) to look for either of two specific suffixes. Adjust to suit.

Also one trick is to add "echo" before "convert" to just see what commands will be exectuted

The find command is powerful, you just need to use that power.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

Re: using convert with find in script

Post by zollsa »

You got me going somewhere now. I decided to use the shell method you posted. I am getting some new errors now. Here is what my script looks like now and the errors for it.

script

Code: Select all

find /home/scott/Pictures/ -mtime -2 -type f \( -name '*.JPG' -o -name '*.jpg' \)| xargs -n1 sh -c 'convert "$0" -background white +matte -thumbnail 400x400 "/home/scott/test/$0.JPG"'
find /home/scott/Pictures/ -mtime -2 -type f \( -name '*.JPG' -o -name '*.jpg' \)| xargs -n1 sh -c 'convert "$0" -background white +matte -thumbnail 160x160 "/home/scott/test/$0_L.JPG"'
error

Code: Select all

convert: unable to open image `sh':  @ error/blob.c/OpenBlob/2587.
convert: no decode delegate for this image format `sh' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/scott/test/sh.JPG' @ error/convert.c/ConvertImageCommand/3011.
convert: unable to open image `sh':  @ error/blob.c/OpenBlob/2587.
convert: no decode delegate for this image format `sh' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/scott/test/sh_L.JPG' @ error/convert.c/ConvertImageCommand/3011.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using convert with find in script

Post by anthony »

Strange... your "sh" shell may be working a little odd...

try this test

Code: Select all

sh -c 'echo 0=$0 1=$1' first second
I get

Code: Select all

0=first 1=second
Hmmm... You sure you used single quotes around the shell command. that is important, otherwise the launching shell expands the $ arguments..

For example

Code: Select all

sh -c "echo 0=$0 1=$1" first second
used the wrong quotes so I get

Code: Select all

0=-bash 1=
on my system as the command line shell expanded the args and not the shell I ran.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

Re: using convert with find in script

Post by zollsa »

I am getting the same results as you are. In my script, I have single quotes around the convert command and I also have double quotes around the patch name and the $0 where the filename is being passed too.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using convert with find in script

Post by anthony »

Well the only other problem I see is that $0_L.png references variable $0_L not $0
that should be ${0}_L to seperate the variable name from the static part of the string.

Alternative to the sh method..

Code: Select all

 xargs -iNAME   convert NAME .... NAME_L.png
For testing add "echo" before the "convert"
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

Re: using convert with find in script

Post by zollsa »

I added the echo and I get two blank lines. Does that help any?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using convert with find in script

Post by anthony »

It means no filenames were being found by find!

please list your exact line.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zollsa
Posts: 9
Joined: 2013-03-15T09:18:09-07:00
Authentication code: 6789

Re: using convert with find in script

Post by zollsa »

Here is the code with the echo statement

Code: Select all

find /home/scott/Pictures/ -mtime -2 -type f \( -name '*.JPG' -o -name '*.jpg' \)| xargs -n1 sh -c echo convert "$0" -background white +matte -thumbnail 400x400 "/home/scott/test/${0}.JPG"
find /home/scott/Pictures/ -mtime -2 -type f \( -name '*.JPG' -o -name '*.jpg' \)| xargs -n1 sh -c echo convert "$0" -background white +matte -thumbnail 160x160 "/home/scott/test/${0}_L.JPG"
Post Reply