Page 1 of 1

Layering Multiple JPEG Images

Posted: 2017-01-31T07:31:37-07:00
by uemon
I want to resize several JPEG images and overlay them on a background image by using Imagemagick.

I can achieve my purpose with the following cords.

Code: Select all

convert -size 500x1000 xc:white \
\( aaa.jpg -resize 50x50 \) -geometry +50+100 -composite \
\( bbb.jpg -resize 100x100 \) -geometry +100+200 -composite \
final.jpg"
But It is written that
"Do not do this with a lossy image format like "JPEG" as the format errors are accumulative, and the base image will quickly degrade."
in a following page.
https://www.imagemagick.org/Usage/layers/#composite

Shouldn't I use the above cord for JPEG images ?

If so, I tried the following codes too.

Code: Select all

convert -size 500x1000 xc:white \
-page +50+100 \( aaa.jpg -resize 50x50 \) \
-page +100+200 \( bbb.jpg -resize 100x100 \) \
-layers flatten flatten_img.jpg"
But the positions of aaa.jpg and bbb.jpg didn't come to the points at (50, 100) and (100, 200).
Maybe, the "-page +50+100" was also affected by "-resize".

I tried the following codes too.

Code: Select all

convert -size 500x1000 xc:white \
-page +50+100 \( aaa.jpg -resize 50x50 +repage \) \
-page +100+200 \( bbb.jpg -resize 100x100 +repage \) \
-layers flatten flatten_img.jpg"
But the positions of aaa.jpg and bbb.jpg came to the point at (0, 0).

If I use the above pattern, that is,

Code: Select all

convert ..... -page ..... -flatten
how should I write codes ?

Please help me.
Thank you in advance.

Re: Layering Multiple JPEG Images

Posted: 2017-01-31T07:55:12-07:00
by snibgo
When you do a number of image processing steps, and you use JPEG format for saving intermediate stages, each save will degrade the image because the compression is lossy -- it changes the data. (It is also just 8 bits/channel.) I suggest you should never use JPEG unless you have to.

With "-layers flatten", I suggest you use "-repage" after each image, instead of "-page" before each image.

Re: Layering Multiple JPEG Images

Posted: 2017-01-31T15:59:59-07:00
by uemon
Thank you for your kind advice.
I tried a following codes.

Code: Select all

convert -size 500x1000 xc:white \
\( aaa.jpg -resize 50x50 \) -repage +50+100 \
\( bbb.jpg -resize 100x100 \) -repage +100+200 \
-layers flatten final.jpg"
My aim was that the "aaa.jpg" was positioned at (50,100) and the "bbb.jpg" was positioned at (100,200).
But the result is that both "aaa.jpg" and "bbb.jpg" were drawn at a position (100,200), which was a position of "bbb.jpg".

How should I fix the codes ?

When you do a number of image processing steps, and you use JPEG format for saving intermediate stages, each save will degrade the image because the compression is lossy -- it changes the data. (It is also just 8 bits/channel.) I suggest you should never use JPEG unless you have to.
I can't understand it well, so please teach me.
If I write the codes as follows

Code: Select all

convert -size 500x1000 xc:white \
\( aaa.jpg -resize 50x50 \) -geometry +50+100 -composite \
\( bbb.jpg -resize 100x100 \) -geometry +100+200 -composite \
final.jpg"
does it use JPEG format for saving intermediate stages ?

And if I write the codes with -flatten as follws,

Code: Select all

convert -size 500x1000 xc:white \
\( aaa.jpg -resize 50x50 \) -repage +50+100 \
\( bbb.jpg -resize 100x100 \) -repage +100+200 \
-layers flatten final.jpg"
doesn't it use JPEG format for saving intermediage stages ?

Thank you in advance.

Re: Layering Multiple JPEG Images

Posted: 2017-01-31T16:22:23-07:00
by snibgo
IM operations apply to all images in the current list. You have a "-repage" when you have one image in the list, and then when you have two images in the list.

You should have each "-repage" when you have only one image in the current list. This happens if you put them inside the parentheses.


You are doing only one step, one convert. There are no intermediate files.

If you have only JPEGs for inputs, and your output must be JPEG, then that is what you must use.

Re: Layering Multiple JPEG Images

Posted: 2017-01-31T16:35:53-07:00
by uemon
You should have each "-repage" when you have only one image in the current list.
Oh, I see.

I fixed the codes as follws,

Code: Select all

convert -size 500x1000 xc:white \
\( aaa.jpg -resize 50x50 -repage +50+100 \)  \
\( bbb.jpg -resize 100x100 -repage +100+200 \) \
-layers flatten final.jpg"
that was perfect !

I learned a lot from you, thank you very much !