Page 1 of 1

Loop inside of a loop Batch removing bg

Posted: 2019-04-07T19:23:34-07:00
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..

Re: Loop inside of a loop Batch removing bg

Posted: 2019-04-07T20:51:54-07:00
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.

Re: Loop inside of a loop Batch removing bg

Posted: 2019-04-08T02:18:29-07:00
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".

Re: Loop inside of a loop Batch removing bg

Posted: 2019-04-08T03:45:45-07:00
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

Re: Loop inside of a loop Batch removing bg

Posted: 2019-04-08T04:42:31-07:00
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.

Re: Loop inside of a loop Batch removing bg

Posted: 2019-04-08T06:04:13-07:00
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