Get image background color and create a new image

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
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Get image background color and create a new image

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get image background color and create a new image

Post 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
snibgo's IM pages: im.snibgo.com
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get image background color and create a new image

Post 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".
snibgo's IM pages: im.snibgo.com
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post by sabyamon »

Sorry. Being dumb here.
It says
no matches found: %[MYSIZE]
when I ran the command.
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get image background color and create a new image

Post 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.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Get image background color and create a new image

Post 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
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post 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
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Get image background color and create a new image

Post 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.
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post 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]
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Get image background color and create a new image

Post 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.
sabyamon
Posts: 10
Joined: 2019-03-07T12:34:50-07:00
Authentication code: 1152

Re: Get image background color and create a new image

Post 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.
Post Reply