Argument too long even with quotes

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
coldkingnowhere
Posts: 7
Joined: 2011-11-01T13:36:39-07:00
Authentication code: 8675308

Argument too long even with quotes

Post by coldkingnowhere »

I've been using the shell below to take a series of images and make two, differently edited images of each original. Until now it worked fine but upon trying it on a batch of ~2000 images now, I'm getting the error "Argument list too long". I have since edited the code to include the quotes you see around the wildcards as you can see now but the error is still appearing (no longer for the first mogrify, but now for the covert operations so it looks like it has something to do with the 'f in "*.png"' business).

I suppose I could break the operation into smaller chunks but I would prefer to do it all at once and just leave the computer while it works. Any thoughts? Thanks for the help.

Code: Select all

#!/bin/bash

if [ ! -d ./Hero ]; then mkdir ./Hero;
fi
if [ ! -d ./Gallery ]; then mkdir ./Gallery;
fi

	echo "Converting .jpg's..."
	mogrify -format png "*.jpg"

	echo "Converting .psd's..."
	mogrify -format png *.psd[1]
	
	echo "Converting .gif's..."
	mogrify -format png *.gif
	
	echo "Converting .tif's..."
	mogrify -format png *.tif


echo "Conversion Complete; now formatting..."

for f in "*.png";
do
	echo "Formatting image as hero shot: $f"
	convert	-set density 38 -units PixelsPerCentimeter \
		-bordercolor white -border 1x1 \
        -alpha set -channel RGBA -fuzz 3% \
        -fill none -floodfill +0+0 white \
        -shave 1x1 \
		-blur 0x.3 \
		-trim +repage \
		-resize 230x375 \
			$f ./Hero/$f
done

echo "Process complete; Hero shots created in folder: Hero"

for f in "*.png";
do
	echo "Formatting gallery image: $f"
	convert	-set density 38 -units PixelsPerCentimeter \
        -bordercolor white -border 1x1 \
		-alpha set -channel RGBA -fuzz 1% \
		-trim +repage \
		-resize 950x800 \
		-background white -gravity center -extent 1920x1080+1-50 \
		$f ./Gallery/$f
done

echo "Images formatted; now converting to .jpg"

cd Gallery

for f in "*.png";
do
	echo "Converting gallery image: $f"
	mogrify -format jpg "*.png"
done

echo "Removing temporary files..."

rm *.png

cd

rm *.png
rm *.jpg
rm *.gif
rm *.psd

echo "Process complete; Gallery images created in folder: Gallery; Hero shots created in folder: Hero"
echo -e "\a"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Argument too long even with quotes

Post by fmw42 »

Why can you not do all the "convert" processing right in the mogrify commands. As long as you are taking one input at a time, have no other inputs and are making one corresponding output, mogrify should be able to handle all the commands you have. Also why not just process directly to jpg in such a mogrify. You don't need to save as png and all processing will be done first before doing the jpg compression for output, so you don't lose any quality.

Also I am not sure why you are converting psd[1]. The first layer is [0] and is generally the flattened image.


I have also found by experiment that the following works.

create directories Test1 and Test2. in Test1

convert rose: rose1.png
convert rose: rose2.gif

Then cd to Test1

mogrify -path ../Test2 -format jpg *.png *.gif

will create

rose1.jpg
rose2.jpg

in Test2

So you should be able to list all the input formats *.png *.gif *.tif *.psd[0] in your mogrify. If this works, then you only need to have two mogrify's to create the processed output to two sizes.


mogrify -path ../Size1Folder -format jpg <all your formatting commands> *.png *.gif *.tif *.psd[0]


mogrify -path ../Size2Folder -format jpg <all your formatting commands> *.png *.gif *.tif *.psd[0]
coldkingnowhere
Posts: 7
Joined: 2011-11-01T13:36:39-07:00
Authentication code: 8675308

Re: Argument too long even with quotes

Post by coldkingnowhere »

Thanks for the input; I tried to emulate what you suggested but with a few tweaks because of the result I want but I've got one more issue.

(Since you were wondering, I don't want the flattened image for the .psd's, on all my source files, I do just want the first layer.)

I want one folder to have the edited files in .png with a transparent background; the other folder should have the images in .jpg on a larger white canvas.

When I do the following, sort of based on your suggestions, since I'm using the mogrify -format command, I still have the source files in the folder. However, since they're all in the same place and some will share file extensions with the final images, I can't automatically separate and remove the old from the new (at least given my admittedly limited knowledge of unix).

Thanks again for the help!

Code: Select all

echo "Creating directories..."
cp -r Edit Hero
cp -r Edit Gallery

cd Hero

echo "Creating Hero shots..."
mogrify -format png -set density 38 -units PixelsPerCentimeter -bordercolor white -border 1x1 -alpha set -channel RGBA -fuzz 3% -fill none -floodfill +0+0 white -shave 1x1 -blur 0x.3 -trim +repage -resize 230x375 *.jpg *.png *.gif *.tif *.psd[1]

cd
cd Gallery

echo "Creating Gallery shots"
mogrify -format jpg -set density 38 -units PixelsPerCentimeter -bordercolor white -border 1x1 -alpha set -channel RGBA -fuzz 1% -trim +repage -resize 950x800 -background white -gravity center -extent 1920x1080+1-50 *.jpg *.png *.gif *.tif *.psd[1]

echo "Process complete; Gallery images created in folder: Gallery; Hero shots created in folder: Hero"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Argument too long even with quotes

Post by fmw42 »

Yes, I had a typo about the png for your transparent files. I meant to put png. Looks like I also missed the fact that you want to use *.jpg as well.

Do those modifications now work as you wrote them? I am not sure what your remaining issue is? Can you explain it again? Does it have to do with writing over files or with removing old files? I am not sure exactly what the problem is? As long as you don't have the same names with different suffixes as input, you should be ok.
coldkingnowhere
Posts: 7
Joined: 2011-11-01T13:36:39-07:00
Authentication code: 8675308

Re: Argument too long even with quotes

Post by coldkingnowhere »

It does work now. I was a little unclear on how the mogrify -format command worked but I've got it now (specifically, I knew that it created a new file if the format was changing but didn't know that if the format doesn't change that there will be no duplication; I just added a few rm commands to clean up the directory after).

Thanks for all the help! As someone who is inexperienced in both unix and imagemagick and often just guessing at what to do, it is so helpful to have people out there to point me in the right directions.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Argument too long even with quotes

Post by anthony »

Note about creating directories. with mkdir. You can just apply it, junk any error, and check its status.

However be warned that just because the directory does not exist [ -d ./Heros ] does not mean it will succeed! What if you have a file, or named pipe called heroes. What if the parent directory '.' or the file system itself is not writeable by you. In other words just 'how controlled' is the environment you are working in.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
EthanGreg
Posts: 1
Joined: 2012-03-10T04:11:41-07:00
Authentication code: 8675308

Re: Argument too long even with quotes

Post by EthanGreg »

I had the similar problem and faced it a lot of times. I often solve this type of problems using "Long Path Tool". Try it and it may surely help you.
Post Reply