Can you remove the alpha channel in a PNG with SIPS? - macos

Trying to stay native in SIPS when removing the alpha channel from images I am familiar with the process in ImageMagick with:
convert -flatten test.png test-white.png
or:
convert test.png -background white -alpha remove test.png
but when I reference the man page on ss4 and Library it tells me that hasAlpa is a boolean read only when I run:
sips -g hasAlpha test.png
Per searching under the tag sips and with:
sips transparency
sips remove
there wasn't anything mentioned for removing transparency. Can you remove transparency with SIPS?

Using ImageMagick or GraphicsMagick would be better idea, but if you really want to use SIPS, you could try to remove transparency by converting image to BMP, and then back to PNG again:
sips -s format bmp input.png --out tmp.bmp
sips -s format png tmp.bmp --out output.png
Unfortunately you cannot choose background color, transparent parts of image will be replaced with black.

You could use the following little script, which uses OSX's built-in PHP which includes GD by default, so you would be staying native and not need ImageMagick or anything extra installed:
#!/usr/bin/php -f
<?php
// Get start image with transparency
$src = imagecreatefrompng('start.png');
// Get width and height
$w = imagesx($src);
$h = imagesy($src);
// Make a blue canvas, same size, to overlay onto
$result = imagecreatetruecolor($w,$h);
$blue = imagecolorallocate($result,0,0,255);
imagefill($result,0,0,$blue);
// Overlay start image ontop of canvas
imagecopyresampled($result,$src,0,0,0,0,$w,$h,$w,$h);
// Save result
imagepng($result,'result.png',0);
?>
So, if I start with this, which is transparent in the middle:
I get this as a result:
I made the canvas background blue so you can see it on StackOverflow's white background, but just change lines 12 & 13 for a white background like this:
...
...
// Make a white canvas, same size, to overlay onto
$result = imagecreatetruecolor($w,$h);
$white = imagecolorallocate($result,255,255,255);
imagefill($result,0,0,$white);
...
...
I did another answer here in the same vein to overcome another missing feature in sips.

Another option, which is much lighter weight than installing the entire ImageMagick might be to use the NetPBM suite:
pngtopam -background=rgb:ff/ff/ff -mix start.png | pnmtopng - > result.png
You can easily install NetPBM using homebrew with:
brew install netpbm

Related

Crop an image using a specific x, y coordinate in AppleScript

I was able to crop using the top - bottom padding and left - right padding, but how do we crop a specific region of interest from the image using AppleScript, like for example , if the total dimension of the original image is 1000*1000 , and I want the region from {200, 150 , 600 , 600 }
I do not believe it is possible to crop an arbitrary part out of an image with either Image Events or sips "Scriptable Image Processing System".
If anyone knows different, kindly ping me and I am happy to stand corrected.
If you don't want to install any software on your Mac, you can achieve what you want with a small PHP script as it comes with GD installed to do the image processing. That will look something like this:
#!/usr/bin/php -f
<?php
$im = imagecreatefromjpeg("image.jpg");
$crop_area = array('x'=>200,'y'=> 100,'width'=>600,'height'=>600);
$result = imagecrop($im, $crop_area);
imagejpeg($result,"result.jpg");
?>
Of course, you can put that in a script, say "cropper.php" and call it from Applescript with:
do shell script cropper.php
and you can also accept parameters so that you can pass in the name of the image, the crop geometry and the output filename.
Another option might be to install ImageMagick which you can do with homebrew by running:
brew install imagemagick
You can then use ImageMagick like this:
magick input.jpg -crop 600x600+200+150 result.jpg
You can call that from Applescript with:
do shell script "magick ..."
just the same as the PHP version above.

How to resize images using ImageMagick? (mac os)

There is a pack of images and I want to reduce the height and width on 10 px of each image. The problem is that every image has a different size (i mean height and width). I found out how to resize images in terminal using ImageMagick, but it could resize images only to a fixed size (for example: convert example.png -resize 200x100 example.png). I need resizing to ((current width)-10px)x((current height)-10px) for every image. I am new to programming, be patient, please :)
If using Imagemagick 7, you can write a loop over all your images. In Unix syntax (assuming your images names have no spaces in them):
cd to your current directory
list=$(ls)
for img in $list; do
magick $img -resize "%[fx:w-10]x%[fx:h-10]" $img
done
If you do not want to overwrite your images, then create a new directory and put the path to that directory before the output.
If you want to do more than one image at a time, you can do:
cd to your current directory
magick * -resize "%[fx:s.w-10]x%[fx:s.h-10]" result.suffix
This will make the resulting images all with the same name, but with numbers appended, such as result-0.suffix, result-1.suffix, etc.
If you are using Imagemagick 6, then you will have to precompute the sizes in a separate command.
cd to your current directory
list=$(ls)
for img in $list; do
dimensions=$(convert image -format "%[fx:w-10]x%[fx:h-10]" info:)
convert $img -resize $dimensions $img
done
Note that resizing will not necessarily give you the result you want, since Imagemagick will try to keep the aspect ratio. So if you want to force the exact sizes, then you need to add ! to your resize argument. However, that will cause some distortion, though probably not too much for only resizing by 10 pixels.
An easier way would be to just shave off 5 pixels all around. Then you could do a whole folder using mogrify:
cd to current directory after creating a new directory for the output if desired:
mogrify -path path/to/new_directory -shave 5x6 *
In Imagemagick 7, that would be magick mogrify ...

How to crop 486 pixels from the bottom of each JPG image in the folder with ImageMagick?

How to crop 486 pixels from the bottom of each JPG image in the folder with ImageMagick?
The following command
magick -crop -0-486 *.jpg
says
magick.EXE: no images found for operation `-crop' at CLI arg 1 # error/operation.c/CLIOption/524
magick.EXE: no image to apply a property "%w" # warning/property.c/GetMagickPropertyLetter/2561.
magick.EXE: unknown image property "%w" # warning/property.c/InterpretImageProperties/3499.
magick.EXE: no image to apply a property "%h" # warning/property.c/GetMagickPropertyLetter/2449.
magick.EXE: unknown image property "%h" # warning/property.c/InterpretImageProperties/3499.
magick.EXE: no image to apply a property "%m" # warning/property.c/GetMagickPropertyLetter/2480.
magick.EXE: unknown image property "%m" # warning/property.c/InterpretImageProperties/3499.
Please, give specific example, internet in controversal (various names like mogrify, convert, various commands etc). Also don't point to ImageMagick "Talmud". Need just a simple example.
OS is Windows, Magick is installed with Chocolatey.
Please make a backup of your images before using the following commands.
The command for a single image is convert or if you have ImageMagick 7+, it is magick.
The command for multiple images is mogrify, or if you have ImageMagick 7+, it is magick mogrify.
The command you want is as follows and it will chop 486 pixels off the bottom of each image in the current directory:
magick mogrify -gravity south -chop x486 *.jpg
The main ImageMagick command command used to be called convert but there is a Microsoft tool with the same name that has caused confusion for years, so all the ImageMagick commands were prefixed with magick, followed by the old name. So,
animate ...
becomes:
magick animate ...
And
mogrify ...
becomes:
magick mogrify ...
In the case of convert, which is the most common usage, you can now use
magick convert ...
or simply
magick ...
where convert is implied.

Converting tiff to mode 1 bit images

Is it possible to convert a tiff image to mode 1-bit image using command line tools. I saw it can be done with gimp but I need to run a script so I prefer a solution using packages like imagemagick etc
If the image contents is already black and white, and you just need to convert, use:
convert input.tif -depth 1 output.tif
If you also require to threshold the image, use something like:
convert input.tif -separate -black-threshold 128 -depth 1 output.tif

Batch convert/resize different image formats to JPEG of certain resolution with ImageMagick

How do I use mogrify to batch-convert a ton of files (.TIF, .EPS, .JPG but most annoyingly of all-) .PSD files and only keep their first layer?
I found a solution on how to convert all images, but I kept getting filenames in my output-directory which were different from my originals (they had -0, -1, -2 etc. attached to the original filename).
I have 2 folders, ./original/ (which contains my PSDs, EPS, TIF, GIF, JPGs and other images) + the folder ./converted/ (which is the target folder for my conversion)
The command I use to convert my images are:
mogrify -verbose -path ./converted/ \
-alpha off -strip -mattecolor white -background white \
-resize 512x512 -format jpg -quality 75% -interlace Plane ./original/*.*
But no matter what I try, I keep getting this garbage which mangles my filenames. There is one mode however which just merges every interpretation of the files into one, but that's also quite useless as I am getting black backgrounds on my transparent files.
After 2 hours of searching I finally found the answer, and since I love stackoverflow, I'm sharing it here:
In order to select the best image of PSD's or first frame of .GIF's, all you need to add is an index of 0 (litterally, without the double quotes: "[0]") to the input file.
So the command to convert all images (you need to install ImageMagick with mogrify first), with a white background for anything transparant, within a bounding box of 512x512 pixels, outputted at jpg with 50% compression quality, the layers Disposed and the first index used, you need to execute the following command:
mogrify -verbose -path ./converted/ \
-alpha off -strip -mattecolor white -background white -layers Dispose \
-resize 512x512 -format jpg -quality 75% -interlace Plane ./original/*.*[0]
Hopefully this will serve as an easy to use template for anyone who needs to batch convert a lot of images.
I found several image converters but none were as easy to use as mogrify, and of course, it being a linux executable, it gives near infinite possibilities about what you can do with it... everything should be scriptable.

Resources