Having problem setting background color

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
nastrofaction
Posts: 4
Joined: 2014-03-03T06:29:42-07:00
Authentication code: 6789

Having problem setting background color

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

Re: Having problem setting background color

Post by snibgo »

Your links don't work.

What is your target image? If jpeg, than that's the problem.
snibgo's IM pages: im.snibgo.com
nastrofaction
Posts: 4
Joined: 2014-03-03T06:29:42-07:00
Authentication code: 6789

Re: Having problem setting background color

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

Re: Having problem setting background color

Post 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.)
snibgo's IM pages: im.snibgo.com
nastrofaction
Posts: 4
Joined: 2014-03-03T06:29:42-07:00
Authentication code: 6789

Re: Having problem setting background color

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

Re: Having problem setting background color

Post 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.
snibgo's IM pages: im.snibgo.com
nastrofaction
Posts: 4
Joined: 2014-03-03T06:29:42-07:00
Authentication code: 6789

Re: Having problem setting background color

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