snh_nl
snh_nl

Reputation: 2965

Mogrify / ImageMagick change jpeg background from grey to white

we run a mogrify batch job like mogrify -path "WEBREADY\DONE" -layers merge -trim +repage -resize "1200x1200>" -define jpeg:extent=500kb -format jpg *.*

This works fine for images with a white background. But recently we saw a 2% error rate and discovered the error. Mogrify trims as expected only on the web the images is surrounded by white so this looks quite silly with grey-ish in corners.

THe solution would be to either

Can this be done in my 1 command above for the commandline? (so find the grey background first and change it to white)

example - original images is very highres

Upvotes: 0

Views: 891

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207728

You can get the colour of the top-left pixel like this:

convert shirt.jpg -crop 1x1+0+0 txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (57311,57311,57311)  #DFDFDF  srgb(223,223,223)

So, it is #DFDFDF.

If you now try and replace that shade with yellow (I know you want white but you can't see that on here):

convert shirt.jpg -fill yellow -opaque "#DFDFDF" result.png

enter image description here

and you can now see the problem. The background is not uniform and its colour also appears in your shirt. If you add some fuzz, it makes it more pronounced:

convert shirt.jpg -fill yellow -fuzz 10% -opaque "#DFDFDF" result.png

enter image description here

Even if you try flood filling from the top-left, you still have to hope that there are no pixels in the edges of your shirt that are similar to that corner:

convert shirt.jpg -fill yellow -fuzz 5% -floodfill +0+0 "#dfdfdf" result.png

enter image description here

Upvotes: 1

Related Questions