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.
Related
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 ...
Image Magick version in use is 6.8.8.1.
Via command line on windows I am just trying to convert a *.pdf file into .jpg file using the following command
convert -density 100 -colorspace RGB "input.pdf" "output.jpg"
But the resulting output.jpg file is with a black image (there is no content). Any one out there could please guide on this ?
When you open this PDF-file by using a text-editor and it's header contains something like this:
%PDF-1.5
%¦éÏÄ
4 0 obj
<</Length 5 0 R/Filter /FlateDecode>>stream
....
....
then you have to decode this flat-encoded stream first before you can convert it to an image.
To solve this problem:
You can use the GUI-Tool pdftk free, set your environment-path to the bin-folder within the pdftk-folder and execute:
pdftk ENCODED_FILENAME.pdf output DECODED_FILENAME.pdf uncompress
in the shell to deflate/unzip this file.
Create a new file that is not encoded or zipped.
Hope this helps.
Set alpha flag to off in convert command.
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
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
From within a Makefile I like to reduce the size of PNGs.
I tried first ImageMagick, but while I could resize (down) the images and reduce their colours to 32 (or the depth to 5 bits), the actual file size was bigger than the original in most cases.
With GraphicsMagick the results are similar, sometimes slightly better, sometimes worse.
[gm] convert input.png -trim -resize 600 -depth 5 -quality 100 output.png
With the Gimp, the results are always perfect. After down-scaling the image and reducing the colours to 32, the resulting images are always much smaller than the originals. Unfortunately, using the Gimp from a Makefile is a little bit difficult and I don't know lisp, only Python.
Questions:
- Is there a way to make ImageMagick or GraphicsMagick reduce the PNG size?
- Is there an easy way of performing this transformations with the Gimp instead, preferable using Python?
- Are there other free tools around to help with this task?
I answer my own question. In the Makefile one has to create and use a temporary Gimp directory, so that one uses the script from the version control system, not an arbitrary local copy. One should batch process image files, because Gimp is relatively slow in startup.
mkdir -p gimp/plug-ins
cp downsize.py gimp/plug-ins/
GIMP2_DIRECTORY=`pwd`/gimp gimp --no-interface \
--batch '(python-fu-downsize RUN-NONINTERACTIVE 600 32 "origdir" "copydir")' \
--batch '(gimp-quit 0)'
The script downsize.py is a normal Gimp Python script, which mainly contains something like:
def downsize(img_w, img_d, in_dir, out_dir):
for fn in glob.glob(os.path.join(in_dir, "*.png")):
img = pdb.gimp_file_load(fn, '1')
if img.width > img_w:
aspect = float(img.width)/float(img.height)
h = int(float(img_w)/aspect)
pdb.gimp_image_scale(img, img_w, h)
if img.base_type != RGB:
pdb.gimp_convert_rgb(img)
if img.base_type != INDEXED:
pdb.gimp_convert_indexed(img, NO_DITHER, MAKE_PALETTE, img_d, False, True, "")
new_path = os.path.join(out_dir, os.path.basename(fn))
pdb.gimp_file_save(img, pdb.gimp_image_active_drawable(img), new_path, fn)
This code may not be correct, it's just the basic idea.
Try pngcrush: http://pmt.sourceforge.net/pngcrush/