ImageMagick v7 conversion of 8bpp images - imagemagick

On Windows 10, I need to crop and resize whole folders of images.
Following the documentation and some answers here i've built this batch:
magick mogrify -path ./cropped -crop 1300x1940+85+130 +repage -resize "800x800^" *.jpg
It mostly works, but when a folder has mixed image formats (some are 24bpp, some are 8bpp, grayscale) this batch skips all the 8bpp ones.
How can i change my command so it accepts and modify every image, no matter what?
Note: i used mogrify as i understand it is needed to process a whole folder and save the result in another one, but i'm unsure it is necessary.

Related

ImageMagick - Overlay one image onto many

I have a folder of images labelled: test.png and img000.png-img480.png
How can I overlay test.png onto every imgxxx.png individually?
Currently "magick test.png img000.png -compose over -composite result.png" works for a single .png, and I'm not sure how/if mogrify can be used for all of them.
Any help is greatly appreciated (non-imagemagick solutions would also be great!) thanks :)
Make a copy of a few test files in a separate directory then change to that directory and try this:
magick mogrify -draw 'image Over 180,100 0,0 test.png' img*png
The 180,100 specifies where it will be drawn, and the 0,0 says to use the current size of test.png, i.e. no resizing. You can change the blend mode (i.e. Over here) for different effects. Get a list of available blend modes with:
magick identify -list compose
If you would like the results written in a different directory called RESULTS, use:
mkdir -p RESULTS
magick mogrify -path RESULTS -draw 'image SrcOver 180,100 0,0 test.png' img*png

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

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.

Convert image from one format to another sent to STDOUT

I'd like to convert an image from .jpg to .png. This works just fine:
convert input.jpg output.png
But, I'm trying to have my output go to STDOUT instead of a file, which the manual says to use "-".
I tried using:
convert input.jpg -.png
But it just creates a file called -.png.
Is it possible to convert an image from one format to another and have it go to STDOUT?
Yes, just use a command like this to convert a JPEG to a PNG on stdout:
magick input.jpg PNG:-
These specifiers work on input as well as output. So, if you have a TIFF on stdin and want a 32-bit RGBA PNG on stdout:
magick TIFF:- PNG32:-
You often need these specifiers to ensure a specific filetype when it is not explicitly given, or you want to use a different extension. So, say you have some CCD device that produces RGB data in a raw binary file called image.bin and you want ImageMagick to read it. You can tell ImageMagick the format without having to change the filename (to image.rgb) like this:
magick -size WxH RGB:image.bin result.png
The possible formats are documented here.
The king of all of these formats is MIFF which is guaranteed to be able to hold any and all things you might throw at it, including floating-point values, transparency, masks, concatenated streams... so if you need a format to pass between ImageMagick commands, MIFF is a good option. An example, just to demonstrate because it is not optimal, might to be to concatenate two images from 2 separate ImageMagick commands into a third command that makes an animated GIF:
{ magick -size 100x60 xc:red miff:- ; magick -size 100x60 xc:blue miff:- ; } | magick -delay 80 miff:- result.gif

Image magick (windows) - How do I perform batch resizing for several images type in a folder?

How do I perform batch-resizing for many images in a folder?
for example I have 4 images in folder C:\tried
image1.jpeg
image2.png
image3.bmp
image4.tiff
and I want to
1. convert them to JPG
2. resize them to 575px
3. lower the quality of each image to 90%
4. move them into different folder
5. delete every picture that converted
Can I do this using batch file (*.bat) in Windows?
start from number 1,
There are plenty of examples on Stack Overflow:
magick mogrify -path OUTPUTDIRECTORY -format JPEG -resize 575 -quality 90 *.tif *.jpg *.bmp
You'll have to delete them yourself.

How can I merge or convert multiple ImageMagick command into one single command

a) I have multiple ImageMagick commands and I need to convert those multiple commands into one. I tried it by putting all the parameters in single command, but somehow it was not working and I had to discard it.
magick -density 300 cheque25.jpg -depth 8 -strip -background white -alpha off cheque25.png
magick convert cheque25.png -resize 150% res_cheque25.png
magick convert -brightness-contrast 10x30 res_cheque25.png b_res_cheque25.png
magick convert b_res_cheque25.png -threshold 45% bin_res_cheque25.png
b) Also, is there any chance that merged commands will give any different output than multiple single command?
Your ImageMagick syntax is not correct in several ways. In ImageMagick 7, you replace convert with magick. Also your input should come right after magick. ImageMagick 6 is forgiving of syntax, but ImageMagick 7 is not. See http://imagemagick.org/script/porting.php#cli
Try the following:
magick cheque25.jpg -depth 8 -strip -alpha off -background white -resize 150% -brightness-contrast 10x30 -threshold 45% -density 300 bin_res_cheque25.png
If that does not work, then provide a link to your input image, so others can test your commands and verify against mine.
The combined commands should give the same as a properly formatted set of commands, provided no syntax errors are present and settings are reset where needed and parenthesis processing is properly used when and where needed. I make no guarantees, since your set of commands is not using proper syntax.

Resources