Page 1 of 1

Get image background color and create a new image

Posted: 2019-03-07T12:46:43-07:00
by sabyamon
Hi All,

I am very new to Image Magick and have used the basic image magick commands.
I am currently working in a use case where I want to get the background color of a given image and use that to create a placeholder image (low quality very small image but with the same dimension of the actual image).

To find the background color of an image, I am using the following command -

Code: Select all

convert original.jpeg -resize 1x1 txt:-
And to create a new image, I am using this

Code: Select all

convert -size 2400x1474 xc:#2E2B2D -define jpeg:extent=20kb flat.jpeg
Is there any way to do this in one command (like piping the output of the first one into the second one)?
As I will be running these command from a Java program, its an overkill for me if I need to write the background color of the image in a text file --> parse/read that text file --> run another command to create the placeholder image.


Image Magick Version - Version: ImageMagick 7.0.8-28 Q16 x86_64 2019-02-19
OS - Mac OS - 10.13.3


Thanks in advance,
Sabya

Re: Get image background color and create a new image

Posted: 2019-03-07T13:04:31-07:00
by snibgo
For v7, I suggest you use "magick", not "convert".

How about:

Code: Select all

magick original.jpeg -resize 1x1 -scale 2400x1474 out.png

Re: Get image background color and create a new image

Posted: 2019-03-07T13:10:24-07:00
by sabyamon
Thanks snibgo!
This works perfectly.

Few things I am missing with this

Code: Select all

magick original.jpeg -resize 1x1 -scale 2400x1474 out.png
Is there any way to know/calculate the dimension of original.jpeg and pass it on to scale instead of hardcoding 2400x1474?

Re: Get image background color and create a new image

Posted: 2019-03-07T13:20:08-07:00
by snibgo

Code: Select all

magick toes.png -set option:MYSIZE %wx%h -resize 1x1 -scale %[MYSIZE] x.png
This will work with "magick" but not with "convert". That's why I encourage all v7 users to use "magick".

Re: Get image background color and create a new image

Posted: 2019-03-07T14:11:14-07:00
by sabyamon
Sorry. Being dumb here.
It says
no matches found: %[MYSIZE]
when I ran the command.

Re: Get image background color and create a new image

Posted: 2019-03-07T14:20:49-07:00
by sabyamon
Also,
I think I need to downgrade IM version to ImageMagick 6.7.8-9 (for some organizational and OS requirements).
So, want to make sure the magick command will work on that version too.

Re: Get image background color and create a new image

Posted: 2019-03-07T14:24:08-07:00
by fmw42

Code: Select all

Is there any way to know/calculate the dimension of original.jpeg and pass it on to scale instead of hardcoding 2400x1474?
Only in IM 7. With IM 6, you need to do the computation in one convert command, store it in a variable and then use the variable in a second convert command to do the processing. Variable storing and usage will differ depending upon OS.

CORRECTION: See GeeMack's solution for IM 6 using -distort SRT and the viewport define.

Re: Get image background color and create a new image

Posted: 2019-03-07T14:40:52-07:00
by GeeMack
sabyamon wrote: 2019-03-07T14:20:49-07:00I think I need to downgrade IM version to ImageMagick 6.7.8-9 (for some organizational and OS requirements).
So, want to make sure the magick command will work on that version too.
If you're limited using IM6, a command like this will create the solid colored output the same dimensions as the input image...

Code: Select all

convert input.png ( +clone -resize 1x1 -write mpr:tile +delete ) -tile mpr:tile -draw "color 0,0 reset" out.png

Re: Get image background color and create a new image

Posted: 2019-03-07T14:46:56-07:00
by sabyamon
Thanks, fmw42. I see the limitations.
For now, I can deal with passing the image dimension as a parameter to the command.

With

Code: Select all

convert original.jpeg -resize 1x1 -scale 2400x1474 out-1.jpeg
.

I can get the placeholder image created with a size ~26kb for JPEG and below 1kb for a PNG.
Is there any way to get the file size down to less than a kb or in bytes for JPEGs?

As this is a placeholder image, the lower the size the better it will be for me.

Thanks,
Sabya

Re: Get image background color and create a new image

Posted: 2019-03-07T14:55:13-07:00
by sabyamon
I have tried to compress the size of the output file with defining an extent for jpeg. But that does not seem to be honored it seems.

Code: Select all

convert original.jpeg -resize 1x1 -scale 2400x1474 -define jpeg:extent=1kb out-3.jpeg

Re: Get image background color and create a new image

Posted: 2019-03-07T14:58:34-07:00
by GeeMack
sabyamon wrote: 2019-03-07T14:46:56-07:00For now, I can deal with passing the image dimension as a parameter to the command.
You can use the image dimensions in a single command with IM6 using a command like this...

Code: Select all

convert input.png -set option:distort:viewport %[w]x%[h] -resize 1x1 -distort SRT 0 out.png
That will output an image the same dimensions as the input, and filled with the color that results from resizing it to 1x1.

Re: Get image background color and create a new image

Posted: 2019-03-07T15:02:02-07:00
by sabyamon
Thanks GeeMack.

I am getting this when I run this my terminal.
convert original.png -set option:distort:viewport %[w]x%[h] -resize 1x1 -distort SRT 0 otter.png
zsh: no matches found: %[w]x%[h]

Re: Get image background color and create a new image

Posted: 2019-03-07T15:03:41-07:00
by GeeMack
sabyamon wrote: 2019-03-07T15:02:02-07:00I am getting this when I run this my terminal.
convert original.png -set option:distort:viewport %[w]x%[h] -resize 1x1 -distort SRT 0 otter.png
zsh: no matches found: %[w]x%[h]
You might have to put quotes around the "%[w]x%[h]". I only tested it on Windows.

Re: Get image background color and create a new image

Posted: 2019-03-07T15:43:45-07:00
by sabyamon
Cool. Thanks. It works.

I'm probably stuck in the last part of the issue where I am trying to get the size down for the generated JPEG image.