ImageMagick change image width and height - image

I am using ImageMagick to resize image resolution by using below command-line option
convert abc.png -set units PixelsPerInch -density 75 abc_a.png
I am in need of this: if any images has more than 300 width OR more than 100 height, I want to convert it to width 300 width and 100 height, with changing above dpi (i.e. 75dpi).
Can any one help me on this?

If you are on Linux/OSX, you can get the image dimensions like this:
identify -format "%w %h" input.jpg
So, if you want the width and height in variables w and h, do this:
read w h < <(identify -format "%w %h" input.jpg)
Now you can test the width and height and do further processing if necessary:
[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...
Or, if you want to be more verbose:
if [ $w -gt 300 -o $h -gt 100 ]; then
convert ...
fi
So, the total solution I am proposing looks like this:
#!/usr/bin/bash
read w h < <(identify -format "%w %h" input.jpg)
[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...
JPEG or PNG makes no difference, so just replace my JPG with PNG if that is the format of your choice.
Updated for Windows
Ok, no-one else is helping so I will get out my (very) rusty Windows skills. Get the image width something like this under Windows:
identify -format "%w" input.png > w.txt
set /p w=<w.txt
Now get the height:
identify -format "%h" input.png > h.txt
set /p h=<h.txt
You should now have the width and height of image input.png in 2 variables, w and h, check by typing
echo %w%
echo %h%
Now you need to do some IF statements:
if %w% LEQ 300 GOTO SKIP
if %h% LEQ 100 GOTO SKIP
convert ....
:SKIP
Note:: You may need ^ in front of the percent sign in Windows.
Note: You may need double # signs in scripts because Windows is illogical.

You cannot control the -density and the width plus height at the same time!
Density (or resolution), when creating an image, will automatically resize the image to a certain number of pixels in width and height (or it will have been ignored).
Density (or resolution), when displaying an image (like in a browser window, within a HTML page, or on a PDF page), will not change the original images dimensions: instead it will zoom in or zoom out the respective view on the image.
Density (or resolution), when used in the metadata of an image (which is not supported by every file format), does not change the image dimensions -- it just gives a hint to the displaying software, at what zoom level the image wants to be displayed (which is not supported by every image viewer).
Now to your question...
Try this command:
convert abc.png -scale 300x100\> abc_a.png
This will scale the image only if
either the original image's width is larger than 300 pixels,
or the original image's height is larger than 100 pixels.
The scaling will preserve the aspect ratio of the original image. -- Is this what you are looking for?
If the image is smaller, then no scaling will happen and abc_a.png will have the original dimensions.
If you want to *emphatically scale the image to 300x100, no matter what, and loose the aspect ratio, bearing with some distortion of the original image, the use:
convert abc.png -scale 300x100\! abc_b.png
(However, this will also scale smaller images...)

Related

gimp script to square multiple images, maintaining aspect ratio and minimum dimensions

I have hundreds of jpgs of varying sizes (e.g. 2304px x 2323px).
In gimp I can use a batch filter to change these to certain sizes, relative or absolute. But for some configurations I have to do the following manually, which for all the images takes forever:
Change the size of the shortest side to 500px, maintaining the aspect ratio so the longer side is at least 500px. So if the image was 1000 x 1200, it will now be 500 x 600. The images come in both portrait and landscape.
Change the canvas size so the image is a 500px x 500px square, centered. This will cut off part of the image (which is fine, most images are almost square anyway).
Export the file with a -s appended to the file name.
Is there a script I can use to automate these steps?
Something like this in ImageMagick. It's not as hard as it looks as most of it is comment. Try to on a COPY of your files - it does all the JPEGs in the current directory.
#!/bin/bash
shopt -s nullglob
shopt -s nocaseglob
for f in *.jpg; do
echo Processing $f...
# Get width and height
read w h < <(convert "$f" -format "%w %h" info: )
echo Width: $w, Height: $h
# Determine new name, by stripping extension and adding "s"
new=${f%.*}
new="${new}-s.jpg"
echo New name: $new
# Determine lesser of width and height
if [ $w -lt $h ]; then
geom="500x"
else
geom="x500"
fi
convert "$f" -resize $geom -gravity center -crop 500x500+0+0! "$new"
done
Unless you find your way with gimp, you may want to look into ImageMagick: the mogrify tool allows to modify and resize images.
Beware: mogrify will overwrite your file, unless you use stdin/stdout. You probably want a script like this:
#!/bin/sh
for image in `ls /your/path/*jpg`; do
mogrify -resize ... - < "$image" > "${image%%.png}-s.jpg"
done

How to prevent from overwriting using imagemagick

noobie question:
i try to batch convert some files via imagemagick with
for i in *.jpg; do convert $i -colorspace Gray -rotate -90 -verbose out/%03d.jpg; done
it do convert the right way, but overwrites the output file on each loop instead of
continuing with the progressiv number intended with %03d.
input1.jpg=>out/000.jpg JPEG 2479x3508=>3508x2479 3508x2479+0+0 8-bit Grayscale DirectClass 483KB 0.420u 0:00.339
input2.jpg=>out/000.jpg JPEG 2479x3508=>3508x2479 3508x2479+0+0 8-bit Grayscale DirectClass 1.36MB 0.470u 0:00.390
input3.jpg=>out/000.jpg JPEG 2479x3508=>3508x2479 3508x2479+0+0 8-bit Grayscale DirectClass 1.733MB 0.490u 0:00.410
input4.jpg=>out/000.jpg JPEG 2479x3508=>3508x2479 3508x2479+0+0 8-bit Grayscale DirectClass 2.806MB 0.560u 0:00.480
you see, its always overwriting 000.jpg
I need some hints to go forth...
dearest
lippe
# Define a variable to increment
counter=0
# Iterate over images
for image in *.jpg; do
# Convert number from `1' to pretty format `001'
printf -v pretty_counter "%03d" $counter
# Convert image
convert $image -colorspace Gray -rotate -90 -verbose out/$pretty_counter.jpg
# Increment counter
counter=$(( $counter + 1 ))
done
Or just
convert *.jpg -colorspace Gray -rotate -90 -verbose out/%03d.jpg
Explanation
ImageMagick's escape sequence references the image stack. As your invoking the convert command inside the for loop, only one image will exist in the stack, and thus only file out/000.jpg will be generated. Solution would be to use bash to generate the output filename, or give convert the all files at once.

Batch resize images into new folder using ImageMagick

I have a folder of images over 4MB - let's call this folder dsc_big/. I'd like to use convert -define jpeg:extent=2MB to convert them to under 2MB and copy dsc_big/* to a folder dsc_small/ that already exists.
I tried convert dsc_big/* -define jpeg:extent=2MB dsc_small/ but that produces images called -0, -1, and so on.
What do I do?
convert is designed to handle a single input file as far as I can tell, although I have to admit I don't understand the output you're getting. mogrify is better suited for batch processing in the following style:
mogrify -path ../dsc_small -define jpeg:extent=2MB dsc_big/*
But honestly I consider it dangerous for general usage (it'll overwrite the original images if you forget that -path) so I always use convert coupled with a for loop for this:
for file in dsc_big/*; do convert $file -define jpeg:extent=2MB dsc_small/`basename $file`; done
The basename call isn't necessary if you're processing files in the current directory.
This was the command which helped me after a long try.
I wanted to make same sized thumbnails from a big list of large images which have variable width and height . It was for creating a gallery page.
convert -define jpeg:size=250x200 *.jpg -thumbnail 250x200^ -gravity center -extent 250x200 crop/thumbnail-%d.jpeg
I got re-sized thumbnails which all having same width and height. :) thanks to ImageMagick.
Here's a solution without using for loops on the console
convert *.jpeg -define jpeg:extent=2MB -set filename:f '../dsc_small/%t_small.%e' +adjoin '%[filename:f]'
Although this is an old question, but I'm adding this response for the benefit of anyone else that stumbles upon this.
I had the same exact issue, and being discouraged by the use of mogrify, I wrote a small Python based utility called easymagick to make this process easier while internally using the convert command.
Please note, this is still a work in progress. I'll appreciate any kind of feedback I can get.
I found that cd-ing into the desired folder, and then using the bash global variable $PWD made my convert not throw any errors. I'm utilizing ImageMagick's recently implemented caption: http://www.imagemagick.org/Usage/text/#caption function to label my images with the base filename and place them in another directory within the first.
cd ~/person/photos
mkdir labeled
for f in $PWD/*.JPG; do
width=$(identify -format %w $f)
filename=$(basename $f .JPG)
convert -background '#0008' -colorspace transparent -fill white -gravity center -size ${width}x100 caption:"${filename}" ${f} +swap -gravity south -composite "$PWD/labeled/${filename}.jpg";
done
This works for me
convert -rotate 90 *.png rotate/image.jpg
produces image-0.jpg, image-1.jpg, image-2.jpg ..... in the 'rotate' folder. Don't know of a way to preserve the original filenames though.

imagemagick font metrics -- how to place a text in a grid?

let's have a look at the following image:
I have a horizontal grid and i want to place a text in this grid. The above example is wrong, because what i would like to have is that each character is placed exactly in one of the cells of the grid.
I wonder, if i can adjust the text-output in imagemagick to achieve this, without having to place each of the characters with it's own command.
Some additional facts:
i am using imagemagick from some shell script
i am doing rather complex drawings with imagemagick's MVG -- so it would be nice if the text could be still placed with the MVG commands
i am able to adjust the width of the grid by a few pixel, if this would be required with your solution, but all cells of course need to have the same width
i am always using the same fixed-width font (Courier) for this
i am able parse the font-metrics in my shell script and use this information to apply values to my text-commands
i only care about horizontal placement, vertical placement is not important because i render each row individual
With all this in mind -- is there any solution for my problem?
Thanks a lot!
You can use the kerning option - setting inter-character spacing.
e.g.
for i in 0 3 6 9 12 15
do
convert -kerning $i -font Courier -pointsize 24 label:":Kerning $i:" label_$i.jpg
done
will generate the following images. You must simply find the right kerning value for match the grid. (for monospaced font - like your Courier)
If you have a non mono-typed font that you want to force into a grid, you can use this script:
#!/usr/bin/env bash
rm test*png
font=~/Library/Fonts/WittenbergerFrakturMTStd.otf
gridsize=32x32
chr() {
case "$1" in
64 ) echo '\#' ;;
92 ) echo '\\' ;;
* ) printf \\$(printf '%03o' $1)
esac
}
for i in {32..127}; do
c=$( chr $i )
echo -n "$c: "
convert -background transparent -density 90 -pointsize 12 -gravity center -font "$font" label:"$c" -extent $gridsize test-$i.png
done
convert test-{32..127}.png +append test.png

Shell script for adjusting image size

Is there a way to adjust all image sizes in a directory?
If I set the max size to 800x600 it will make larger ones smaller and leave smaller ones at their original size.
for img in *.png; do
convert "$img" "800x600>" $(basename "$img" .png)_new.png
done
convert is from ImageMagick. ">" says it's only resized if larger. See here for its other options.
image magick package needs to be installed:
mogrify -resize 320x240 *.jpg
where 320 = width, 240 = height
or you can just leave width parameter:
mogrify -resize 320 *.jpg
and rest will be taken care of.
Various packages exist for command line or script driven manipulation of image files.
I'd suggest looking at netpbm, or ImageMagick. Personally I prefer the former as it's far simpler to use.

Resources