How can I specify a background in Imagemagick mogrify based on the colour of an existing pixel? - imagemagick

I want to use mogrify to set the background colour of a large number of images to be whatever colour is at a specific pixel, to make them square.
The vast majority have a single colour in the image background, or are photos in front of a single colour (so with only slight variations from shadows, etc.).
(The specific purpose in this case is to make the images all the same size and square for StyleGAN2-ADA training, so I want to avoid big "letterbox" rectangles where possible as it would be seen by the discriminator as relevant to the image, where a more faded-in background that approximately matches would be more likely to be ignored. Specifically, I have thousands of pictures of single dolls and action figures from various sources, some of which are "trimmed out" to have a truly solid colour background, others of which are against solid colour tables/walls/etc, for instance, from eBay images and such.)
Since they do not all have the same colour in the image background (the colour in the image, as opposed to the 'background colour' setting as per ImageMagick's jargon), I need to sample a pixel and set the background, but I can't figure out how. I tried things based on methods used to set the whole image to one colour, to no avail.
I have tried:
mogrify -verbose -resize 1024x1024 -gravity center -background 'p{10,10}' -extent 1024x1024 -resize 256x256 *.jpg
and
mogrify -verbose -resize 1024x1024 -gravity center -background "%[pixel:p{10,10}]" -extent 1024x1024 -resize 256x256 *.jpg
and neither works. I can't find any other possibilities in the documentation.

EDITED TO ADD: While testing various commands I came across a way to modify your original command to make it work on ImageMagick versions as far back as IMv6.8.
mogrify -resize 1024x1024 -set background "%[pixel:p{10,10}]" \
-gravity center -extent 1024x1024 -resize 256x256 *.jpg
The significant difference is setting the background color in an unusual way. Instead of the normal option -background <color>, this command uses -set background <color>. Then it behaves as expected using that +10+10 color as the background in the "mogrify" command.
For ImageMagick v7 use magick mogrify instead of just mogrify.
The following was my original answer. The suggestion for IMv6 "convert" may be quite useful for some workflows, but the answer above seems to be the simplest, most direct route.
PREVIOUS ANSWER:
ImageMagick v6 won't do that inline parsing of the color, but there are ways to get the same result, usually with IM's "convert" in a "for" loop in your shell. I don't know which shell you're using so I don't know how you'd write a "for" loop, but running this command inside the loop on each image should give you the results you described...
convert $image -resize 1024x1024 ( +clone -crop 1x1+10+10 ) +swap \
-resize 1024x1024 -gravity center -composite -resize 256x256 $image
That reads in the image, resizes it, makes a clone inside the parentheses, and extracts that pixel at +10+10. After the parentheses that single pixel get resized to a 1024x1024 square. Then setting the gravity to "center" and compositing the input image over that colored square gives you the result you described.

Related

Resizing animated GIF in image creates artifacts

I'm trying to add a blank area around some animated GIFs using imagemagick, but in many cases the resulting larger animations have artifacts. For example, if I start with this image
And run the following command (which has the dispose and coalesce flags which I've thrown in after seeing them online in the hopes they'll help)
convert input.gif -gravity center -dispose previous -extent 500x500 -coalesce output.gif
The result is the following
Is there a different command I can run that wouldn't gave the green splotches invade the original image?
You have your syntax wrong for ImageMagick and also need to use -dispose none. You are adding a new color to your animation which likely already used 256 colors.
Input:
convert -dispose none input.gif -coalesce -gravity center -background green -extent 500x500 -layers optimize output.gif
Change the background color as desired, which you left out of your command.

Preserving PNG transparency during a simple transformation in imagemagick

Task: i have an input png file (many actually, but i'll just loop the solution). It is 16x16 PNG, 32bit with partial transparency along edges.
It so happens that toolbar of a certain stupid platform requires 17x17 files. My problem is that imagemagick kills transparency when doing simple transformations.
So:
Sanity check:
convert add.png PNG32:add_COPIED.png
creates another 16x16#32bpp file. So far so good.
Transformation (gravity is fine):
convert add.png -extent 17x17 PNG32:add_17.png
creates a file with solid white background. That's not good.
What doesn't work:
I tried a serious number of combinations of transparent, transparent-color, background, alpha and flatten. Got nowhere.
What does work:
convert address_book.png -alpha Extract address_book_MASK.png
convert address_book.png -extent 17x17 PNG32:address_book_17.png
convert address_book_MASK.png -background black -extent 17x17 address_book_MASK17.png
composite -compose CopyOpacity address_book_MASK17.png address_book_17.png PNG32:address_book_FIN.png
While i have a working set of commands and I can get through the day, I honestly believe that this is the wrong way to do things - four commands that create 3 intermediate files that i need to delete later. Surely it can be done in a better way?
Set the background colour before changing the extent:
convert input.png -background none -extent WxH result.png

Crop and merge two images using ImageMagick

I am trying to use ImageMagick to place one image (Poseidon_map01.jpg in my code below) in the top-left corner of another (zzOcean Backdrop.png) then crop the resulting image down to 1280x720.
This is the command I currently have:
".\ImageMagick-7.0.8-12-portable-Q16-x64\magick.exe" ".\Raw\zzOcean Backdrop.png" ".\Raw\Poseidon_map01.jpg" -gravity northwest -composite -crop 1280x720>! +repage ".\1280x720\Poseidon_map01.jpg"
The problem is that this command is cutting the composited image into 1280x720 pieces then saving all of those pieces with the names Poseidon_map01-1.jpg to Poseidon_map01-48.jpg. Poseidon_map01-1.jpg is the top-left corner of the composited image and this is the piece that I want to keep. I want to discard the rest of the composite. Does anyone know what I have to change in my command to make this happen? Thanks.
In Imagemagick crop, if you do not include the +X+Y offsets, it will crop as many pieces as it can given the size you specify. That is called tiled cropping. So use -crop WxH+X+Y>!
See https://imagemagick.org/Usage/crop/#crop
".\ImageMagick-7.0.8-12-portable-Q16-x64\magick.exe" ".\Raw\zzOcean Backdrop.png" ".\Raw\Poseidon_map01.jpg" -gravity northwest -composite -crop 1280x720+0+0>! +repage ".\1280x720\Poseidon_map01.jpg"
I am not a windows user and syntax may vary some. You may need to escape the > or ! with ^ on windows or put double quotes around the whole crop set of arguments. Keep that in mind, if this does not work. See https://imagemagick.org/Usage/windows/

Imagemagick: Create a diff image with transparent background

I want to create a diff from two images, that can be applyed on top of first image, resulting on the same second image.
I'm trying to do like this:
convert -composite -compose difference img1.png img2.png img-diff.png
The resulting img-diff.png shows a black background where img1.png is equal to img2.png.
I want to generate the diff with a transparent background, to allow me to create an animation by combining it with the first image.
I think you need to add -transparent black before the output filename. You may, or may not, want to add a -fuzz 5% to make near black also become transparent.
So, in concrete terms, if you start off with these two images
and you then run this command:
convert 1.jpg 2.jpg -compose difference -composite -fuzz 5% -transparent black out.png
you end up with this (it shows transparent as white because SO uses JPEGs which can't show transparency).
I think the problem with your (valiant) attempt is that you need to set -compose first to tell IM how to compose the images before it actually goes ahead and does it with -composite and also you were missing the -transparent black part.

How to replace white background color with transparent of an image in ImageMagick?

I have an image in .jpg format with white background color. I want to remove the white background color to transparent in Imagemagick. I tried many ways but still the white background can not be removed. Can some one help me to solve this.
You cannot have transparent background colors in your JPEGs. The JPEG file format doesn't support transparency.
If you need transparent background, you need to convert the JPEG to
either PNG (high quality, filesize possibly larger than JPEG)
or GIF (in case you can tolerate low quality and a range of maximally 255 colors).
Example command:
convert your.jpg -transparent white your.png
First, you need to convert the image format from .jpg to .png format, because JPEG does not support transparency. Then use this command:
convert image1.png -fuzz 20% -transparent white result.png
The -fuzz option allows the specified percentage deviation from the pure white colour to be converted to transparent as well. This is useful, for example, when your image contains noise or subtle gradients.
I just found a very neat thing!
magicwand 1,1 -t 20 -f image -r outside -m overlay -o 0 image.jpg imgOutput.png
It is a Fred Weinhaus bash script that can be downloaded from here (for non commercial use only). Also there has about 250 scripts!! and this one is amazing! it did exactly the trick, to remove all background while keeping the inner image dots untouched!
At his page, there are several images as examples so you pick what you need to put on the command line!
The initial position 1,1 is a general guesser saying all the contour is background.
Pay attention that the output must be ".png"
This is my solution without magicwand (replace magick by convert for im < 7.0):
magick img.png -fuzz 20% -fill none -draw "alpha 1x1 floodfill" result.png
Get the background automatically and remove it :
bg=$(convert input.png -format "%[pixel:p{0,0}]" info:)
convert input.png -fuzz 20% -transparent "$bg" output.png

Resources