Page 1 of 1

Having problem setting background color

Posted: 2014-03-03T06:37:59-07:00
by nastrofaction
I have an image that I need to crop and then pad with a color.
The command I have been using is
convert $sourceImage -resize x450 -gravity center -background $colorCode -extent 500x631 $targetImage;
where the colorCode is #19191B.

This works perfectly for 95% cases but for few, the background color starts to deviate from the one mentioned.
Like to #1A1A1C.

sample image
Image

I have tried setting colorspace but nothing seems to work.

Can anyone help.

Re: Having problem setting background color

Posted: 2014-03-03T06:44:21-07:00
by snibgo
Your links don't work.

What is your target image? If jpeg, than that's the problem.

Re: Having problem setting background color

Posted: 2014-03-03T06:46:51-07:00
by nastrofaction
Thanks for responding mate, I have been fighting since morning to resolve this.

Try this link : http://postimg.org/image/ao9onepyx/3a9c6680/

yes, the image is jpeg but so are the other 95% cases.
What could be the problem here ?

Re: Having problem setting background color

Posted: 2014-03-03T06:56:40-07:00
by snibgo
Jpeg compression is lossy. This means that it changes the values of pixels. The compression algorithm is designed for photographs, not blocks of flat colour. So it tends to be most lossy on areas of flat colour.

If the colour shift is not acceptable, the cure not to use jpeg. (You might find that "-quality 100" solves some cases, but it isn't a guaranteed cure.)

Re: Having problem setting background color

Posted: 2014-03-03T07:08:10-07:00
by nastrofaction
Bingo !. The quality level did the trick in this case but has increased the size above specified limit, so we may be back to zero post compression.
Will find a workaround for this.

This brings me to another situation, when the input file has colorspace as CMYK, again the background color gets distorted.
Though, if I introduce -colorspace RGB before the processing, it works fine.

Is this the right way of resolving it or an improper hack ?

Re: Having problem setting background color

Posted: 2014-03-03T07:40:38-07:00
by snibgo
For the output filetype, jp2 may be a solution for you. It gives good compression (for photographs), and is loss-less.

I haven't noticed that jpeg has particular problems with cmyk. But conversions between sRGB and cmyk are lossless (side from minor rounding errors), so if that helps, that's fine.

Re: Having problem setting background color

Posted: 2014-03-04T23:32:01-07:00
by nastrofaction
yes, have read on jp2, but considering the dependent complexity of the overall system I won't want to make that change straight ahead but will for sure do research around it.
The colorspace swtich for cmyk and max quality setting seem to erase the 5% error cases. Will do more concrete changes during next design phase.

Thanks for all your help mate !, for sure won't have been able to resolve this on my own.