I have a Makefile that is used to automatically process some image files. The basic idea behind this is
- copy everything from
col-noborder
andcol-rewnew
tocol
- use ImageMagick
convert
to copy the files fromcol-addborder
tocol
and add a pre-defined border - use ImageMagick
convert
to copy the files fromcol
tobw
while applying a grayscale (greyscale?) filter
Due to the layout of the project, I have to apply this filter for five sets of identically structured subdirectories. Since the project contains quite a number of image files, I want to use parallel processing and update only the files whose sources have changed (hence the use of make
in the first place).
The current implementation does what it is supposed to do, but it is quite ugly. It basically generates a secondary Makefile.raster
based on the input files and then defers to the targets in that generated file. The generation of that file takes over 15 seconds alone, which leaves quite a bit of room for improvement.
I'm sure that there are various ways to improve this script. Can I get the same process (that is, including parallel processing and update-only) using a single, more dynamic Makefile? If not, can I somehow speed up the generation of the secondary Makefile? Is there anything else I should correct?
IMGPATH1 = content/part1/raster
IMGPATH2 = content/part2/raster
IMGPATH3 = content/part3/raster
IMGPATH4 = content/part4/raster
IMGPATH5 = content/part5/raster
IMGPATHS = $(IMGPATH1) $(IMGPATH2) $(IMGPATH3) $(IMGPATH4) $(IMGPATH5)
RASTERMAKEFILE = Makefile.raster
rastercoladdborderfiles := $(wildcard $(addsuffix /col-addborder/*, $(IMGPATHS)))
rastercolnoborderfiles := $(wildcard $(addsuffix /col-noborder/*, $(IMGPATHS)))
rastercolrenewfiles := $(wildcard $(addsuffix /col-renew/*, $(IMGPATHS)))
rastercolfiles := $(wildcard $(addsuffix /col/*, $(IMGPATHS)))
rasterbwfiles := $(wildcard $(addsuffix /col/*, $(IMGPATHS)))
rastermakefile: force
rm -f $(RASTERMAKEFILE)
printf "all: allcolnoborder allcolrenew allcoladdborder allbw\n\n" >> $(RASTERMAKEFILE)
# copy /col-noborder/ to /col/
for infile in $(rastercolnoborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-noborder/col/'`; \
printf "$$outfile: $$infile\n" >> $(RASTERMAKEFILE); \
printf "\tcp $$infile $$outfile\n" >> $(RASTERMAKEFILE); \
done; \
printf "allcolnoborder: " >> $(RASTERMAKEFILE)
for infile in $(rastercolnoborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-noborder/col/'`; \
printf "$$outfile " >> $(RASTERMAKEFILE); \
done
printf "\n\n" >> $(RASTERMAKEFILE)
# copy /col-renew/ to /col/
for infile in $(rastercolrenewfiles); \
do \
outfile=`echo $$infile | sed 's/col-renew/col/'`; \
printf "$$outfile: $$infile\n" >> $(RASTERMAKEFILE); \
printf "\tcp $$infile $$outfile\n" >> $(RASTERMAKEFILE); \
done
printf "allcolrenew: " >> $(RASTERMAKEFILE)
for infile in $(rastercolrenewfiles); \
do \
outfile=`echo $$infile | sed 's/col-renew/col/'`; \
printf "$$outfile " >> $(RASTERMAKEFILE); \
done
printf "\n\n" >> $(RASTERMAKEFILE)
# add border to images in /col-addborder/, copying them to /col/
for infile in $(rastercoladdborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-addborder/col/'`; \
printf "$$outfile: $$infile\n" >> $(RASTERMAKEFILE); \
printf "\tconvert $$infile -bordercolor \"rgb(234,241,246)\" -border 10x10 $$outfile\n" >> $(RASTERMAKEFILE); \
done; \
printf "allcoladdborder: " >> $(RASTERMAKEFILE)
for infile in $(rastercoladdborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-addborder/col/'`; \
printf "$$outfile " >> $(RASTERMAKEFILE); \
done
printf "\n\n" >> $(RASTERMAKEFILE)
# create the grayscale images from the color images
for infile in $(rastercoladdborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-addborder/bw/'`; \
colfile=`echo $$infile | sed 's/col-addborder/col/'`; \
printf "$$outfile: $$colfile\n" >> $(RASTERMAKEFILE); \
printf "\tconvert $$colfile -colorspace Gray -level -15%%,100%% $$outfile\n" >> $(RASTERMAKEFILE); \
done
for infile in $(rastercolnoborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-noborder/bw/'`; \
colfile=`echo $$infile | sed 's/col-noborder/col/'`; \
printf "$$outfile: $$colfile\n" >> $(RASTERMAKEFILE); \
printf "\tconvert $$colfile -colorspace Gray -level -15%%,100%% $$outfile\n" >> $(RASTERMAKEFILE); \
done
for infile in $(rastercolrenewfiles); \
do \
outfile=`echo $$infile | sed 's/col-renew/bw/'`; \
colfile=`echo $$infile | sed 's/col-renew/col/'`; \
printf "$$outfile: $$colfile\n" >> $(RASTERMAKEFILE); \
printf "\tconvert $$colfile -colorspace Gray -level -15%%,100%% $$outfile\n" >> $(RASTERMAKEFILE); \
done
printf "allbw: " >> $(RASTERMAKEFILE)
for infile in $(rastercoladdborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-addborder/bw/'`; \
printf "$$outfile " >> $(RASTERMAKEFILE); \
done
for infile in $(rastercolnoborderfiles); \
do \
outfile=`echo $$infile | sed 's/col-noborder/bw/'`; \
printf "$$outfile " >> $(RASTERMAKEFILE); \
done
for infile in $(rastercolrenewfiles); \
do \
outfile=`echo $$infile | sed 's/col-renew/bw/'`; \
printf "$$outfile " >> $(RASTERMAKEFILE); \
done
printf "\n\n" >> $(RASTERMAKEFILE)
rasterfiles: rastermakefile
make -j 2 -f $(RASTERMAKEFILE)
rasterfiles-force: rastermakefile
rm -f $(rastercolfiles)
rm -f $(rasterbwfiles)
make -j 2 -f $(RASTERMAKEFILE)
force: ;