Loop inside of a loop Batch removing bg

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
naive
Posts: 9
Joined: 2018-04-26T23:53:58-07:00
Authentication code: 1152

Loop inside of a loop Batch removing bg

Post by naive »

Hey Guys, im have two batches of files:
1. Files with background (jpg)
2. Masks for those files (tif)

I'm trying to write a batch script to iterate over those files and remove the backgrounds for each of them, here is my code:

Code: Select all

for (( i = 10; i <= 19; i++ ))      ### Outer for loop ###
do

    for (( j = 10 ; j <= 19; j++ )) ### Inner for loop ###
    do
          Convert ${i}.jpg image_a_00${j}.tif -alpha off -compose CopyOpacity -composite %03d.png
    done

  echo "" #### print the new line ###
done
It seems to be really slow, is there a better way to do it?
Thanks in advance

Running on MAC Imagemagick 7

EDIT:
Just checked the output and actually it doesnt work, the mask files don't get itterated for some reason..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Loop inside of a loop Batch removing bg

Post by fmw42 »

Post some example images and masks so we can examine them. Also what is your exact IM 7.x.x.x version. Note that IM 7 uses magick, not convert and not magick convert. I do not know of any easier way unless the mask is the same for all images. Then you can use magick mogrify.

You cannot combine one alpha channel with another in the way you are trying. It looks like you are masking with multiply mask images when you loop over j. If so, then composite multiply all masks together and use just that one in a single loop over i for each of your input images.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Loop inside of a loop Batch removing bg

Post by snibgo »

naive wrote:it doesnt work
The output filename is "%03d.png". If there were many outputs within the convert, the number would auto-increment. But as there is only one output per convert, with many converts, they will all have the same name.

I suggest you make the name something like "myfile_${i}_${j}.png".
snibgo's IM pages: im.snibgo.com
naive
Posts: 9
Joined: 2018-04-26T23:53:58-07:00
Authentication code: 1152

Re: Loop inside of a loop Batch removing bg

Post by naive »

fmw42 wrote: 2019-04-07T20:51:54-07:00 Post some example images and masks so we can examine them. Also what is your exact IM 7.x.x.x version. Note that IM 7 uses magick, not convert and not magick convert. I do not know of any easier way unless the mask is the same for all images. Then you can use magick mogrify.

You cannot combine one alpha channel with another in the way you are trying. It looks like you are masking with multiply mask images when you loop over j. If so, then composite multiply all masks together and use just that one in a single loop over i for each of your input images.
IM:
ImageMagick 7.0.8-26 Q16 x86_64 2019-02-09

Ah interesting i was always using using convert for the past 2 years.....haha

I'm actually just trying to apply one mask per one image

Images:
Image
Image
Image
Image

Masks:
Image
Image
Image
Image
snibgo wrote: 2019-04-08T02:18:29-07:00
naive wrote:it doesnt work
The output filename is "%03d.png". If there were many outputs within the convert, the number would auto-increment. But as there is only one output per convert, with many converts, they will all have the same name.

I suggest you make the name something like "myfile_${i}_${j}.png".
ah yeah actually my latest code in the loop was this,which was iterating correctly over the images but not over the masks

Code: Select all

convert ${i}.jpg image_a_00${j}.tif -alpha off -compose CopyOpacity -composite ${i}j.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Loop inside of a loop Batch removing bg

Post by snibgo »

naive wrote:I'm actually just trying to apply one mask per one image
But you are applying all the masks to all the input images, a loop within a loop.
snibgo's IM pages: im.snibgo.com
naive
Posts: 9
Joined: 2018-04-26T23:53:58-07:00
Authentication code: 1152

Re: Loop inside of a loop Batch removing bg

Post by naive »

oops, so i guess i can just solve it with one loop

Code: Select all

for (( i = 1; i <= 77; i++ )) 
do

          magick ${i}.jpg image_a_00${i}.tif -alpha off -compose CopyOpacity -composite ${i}small.png

done 
Post Reply