In my first trial with Cygwin, I managed a simple montage of 2 image files into a final one.
However, I would like to batch montage all .jpg files in a directory so to combine them Two by Two, and thus have like half the original number of images.
Could you help?
I generally try to avoid Windows, but you can do it something like this:
GO.BAT
DIR /B *.JPG | CSCRIPT /NOLOGO PAIRIMAGES.VBS
PAIRIMAGES.VBS
cnt=1
Do
' Read in first image name
Im1 = WScript.StdIn.ReadLine()
If WScript.StdIn.AtEndOfStream Then
Wscript.Echo "WARNING: Unpaired file left over."
Exit Do
End If
' Read in second image name
Im2 = WScript.StdIn.ReadLine()
' Work out ImageMagick command, something like:
' convert im1.jpg im2.jpg +append result1.png
cmd="convert " & Im1 & " " & Im2 & " +append " & result & cnt & ".png"
' Show user the command, for debug purposes
WScript.echo cmd
' Now execute it
Set objShell = wscript.createobject("wscript.shell")
Set oExec = objShell.Exec(cmd)
If WScript.StdIn.AtEndOfStream Then
Wscript.Echo "Done"
Exit Do
End If
cnt = cnt + 1
Loop
Sample Output
E:\>DIR /B *.JPG | CSCRIPT /NOLOGO PAIRIMAGES.VBS
convert 1.jpg 2.jpg +append 1.png
convert 3.jpg 4.jpg +append 2.png
Done
If you want to align the tops of images with dissimilar sizes, use:
convert -gravity North ...
and change North to East if you want the centres aligned and to -South if you want the bottoms aligned.
Related
I have two folders, A and B, with image files that have corresponding names.
For example, each contain files labelled 01.png, 02.png, 03.png, etc.
How can I merge the corresponding files such that I have a third folder C that contains all merged photos so that both of the originals are side by side.
I am on Linux, if that changes anything.
I am not near a computer to thoroughly test, but this seems easiest to me:
#!/bin/bash
# Goto directory A
cd A
# For each file "f" in A
for f in *.png; do
# Append corresponding file from B and write to AB
convert "$f" ../B/"$f" +append ../AB/"$f"
done
Or use GNU Parallel and do them all at once!
cd A
parallel convert {} ../B/{} +append AB/{} ::: *.png
Using ImageMagick version 6, if your images are all the same dimensions, and if your system memory can handle reading all the input images into a single command, you can do that with a command like this...
convert FolderA/*.jpg -set filename:f "%[f]" \
-set option:distort:viewport %[fx:w*2] -distort SRT 0 null: \
FolderB/*.jpg -gravity east -layers composite FolderC/"%[filename:f]"
That starts by reading in all the images from FolderA and extending their viewport to double their width to the right.
Then it adds the special built-in "null:" to separate the lists of images before reading in the second list. Then it reads in all the images from FolderB.
Then after setting the gravity to "east", it composites each image from FolderB over the extended right half of each corresponding image from FolderA. That creates the effect of appending the images side by side.
The command sets a variable at the beginning to hold the filenames of the first list of input files, then uses those as the names of the output files and writes them to FolderC.
If you're using ImageMagick version 7, use the command "magick" instead of "convert".
You can do that with some bash scripting code. Assume you have two folders A and B with the corresponding image names in them. Also you have an empty folder AB to hold the results. Then using ImageMagick with the bash looping code, you can do something like this:
Collect the names of all the files in folder A and put into an array
Collect the names of all the files in folder B and put into an array
Loop over the number of images in the folders
Process them with ImageMagick +append and save to folder AB
outdir="/Users/fred/desktop/AB"
aArr=(`find /Users/fred/desktop/A -type f -iname "*.jpg" -o -iname "*.png"`)
numA="${#aArr[*]}"
bArr=(`find /Users/fred/desktop/B -type f -iname "*.jpg" -o -iname "*.png"`)
numB="${#bArr[*]}"
if [ $numA -eq $numB ]; then
for ((i=0; i<numA; i++)); do
nameA=`basename "${aArr[$i]}"`
nameA=`convert "$nameA" -format "%t" info:`
nameB=`basename "${bArr[$i]}"`
nameB=`convert "$nameB" -format "%t" info:`
convert "${aArr[$i]}" "${aArr[$i]}" +append ${outdir}/${nameA}_${nameB}.jpg
done
fi
When converting an image, ImageMagick's default behavior seems to be to overwrite any existing file. Is it possible to prevent this? I'm looking for something similar to Wget's --no-clobber download option. I've gone through ImageMagick's list of command-line options, and the closest option I could find was -update, but this can only detect if an input file is changed.
Here's an example of what I'd like to accomplish: On the first run, convert input.jpg output.png produces an output.png that does not already exist, and then on the second run, convert input.jpg output.png detects that output.png already exists and does not overwrite it.
Just test if it exists first, assuming bash:
[ ! -f output.png ] && convert input.png output.png
Or slightly less intuitively, but shorter:
[ -f output.png ] || convert input.png output.png
Does something like this solve your problem?
It will write to output.png but if the file already exists a new file will be created with a random 5 character suffix (eg. output-CKYnY.png then output-hSYZC.png, etc.).
convert input.jpg -resize 50% $(if test -f output.png; then echo "output-$(head -c5 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c5).png"; else echo "output.png"; fi)
We have a storage of images where some images are optimized and some are not.
I'm working on 'script' that will go via every image in the storage and run optimize process.
I'm wondering:
Is there a way to check if image has been optimized?
Will I lose quality if image has been optimized with -quality 85% few times?
I have tried to run -quality 85% few times on same image and I could not see any lose in quality (after 3-th run the image's size was not changed). However I did not find a proof on official documentation.
Thanks!
You can check if the quality setting is already 75 before optimising:
identify -format %Q fred.jpg
92
Then optimise and check again:
convert fred.jpg -quality 75 optimised.jpg
identify -format %Q optimised.jpg
75
If you are using bash, this is easy:
q=$(identify -format %Q fred.jpg)
[ $q -ne 75 ] && mogrify -quality 75 fred.jpg
Another way to mark images as optimised might be to set a comment in the file to the word "optimised", like this:
# Set comment
mogrify -set comment "optimised" fred.jpg
# Get comment
identify -format %c fred.jpg
optimised
So, you would test if an image comment contains the word "optimised", and if not, optimise the image and change the comment to show as much:
[[ $(identify -format %c fred.jpg) != *optimised* ]] && { echo Optimising fred.jpg; mogrify -set comment "optimised" -quality 75 fred.jpg; }
Another possibility might be to use Extended File Attributes to tag files (with setfattr) as you reduce their quality and then use getfattr to check their quality rather than doing them again.
If you had hundreds or thousands of images to process, I would suggest GNU Parallel. Please try the example below on a small copy of a few files in a directory:
parallel '[[ $(identify -format "%c" {}) != *optimised* ]] && { echo Optimising {}; mogrify -set comment "optimised" -quality 75 {}; }' ::: *jpg
You will find it will process them all in parallel the first pass, and then do nothing on the second pass.
If you have too many images, and get "ERROR: Too many args", you can pass the filenames in on stdin like this, instead of globbing after the ::::
find /some/where -iname \*.jpg -print0 | parallel -0 ...
I would like to mogrify -resize 500 my files and append the new filesize to the filename ie: image-500*500.png
Can someone help me out please.
I don't think you can do that with mogrify because it overwrites the original filename rather than creating a new one. So, I think you will need to do it with convert.
So, for one file called input.png:
convert input.png -resize 400x500\! -set filename:f "%[t]-%[w]x%[h]" "%[filename:f].png"
and your output file will be input-400x500.png
If you wanted to do a whole directory full of PNG files on a Mac, you would make a backup and, then on a spare copy do:
for f in *.png; do
convert "$f" -resize 400x500\! -set filename:f "%[t]-%[w]x%[h]" "%[filename:f].png"
done
I have done a bit of digging and i havn't been able to find any feasible way of adding watermarks to my 1000+ images automatically. Is this possible with irfanview?? What im looking for is just some basic transparent text overlaying across each image. Can this be done using command line? Is it possible to go one step further and add a logo watermark?
Can you recommend any other programs rather than irfanview to do this, if its not possible to do it in this program.
I recommend using ImageMagick, which is open source and quite standard for manipulating images on the command line.
Watermarking with an image is as simple as
composite -dissolve 30% -gravity south watermark.jpg input-file.jpg output-file.jpg
With text, it's a little more complicated but possible.
Using the above command as an example, a Bash command for doing this to all files in folder would be:
for pic in *.jpg; do
composite -dissolve 30% -gravity south watermark.jpg $pic ${pic//.jpg}-marked.jpg
done
For more information about watermarking with ImageMagick, see ImageMagick v6 Examples.
Here's a quick python script based on the ImageMagik suggestion.
#!/usr/bin/env python
# encoding: utf-8
import os
import argparse
def main():
parser = argparse.ArgumentParser(description='Add watermarks to images in path')
parser.add_argument('--root', help='Root path for images', required=True, type=str)
parser.add_argument('--watermark', help='Path to watermark image', required=True, type=str)
parser.add_argument('--name', help='Name addition for watermark', default="-watermark", type=str)
parser.add_argument('--extension', help='Image extensions to look for', default=".jpg", type=str)
parser.add_argument('--exclude', help='Path content to exclude', type=str)
args = parser.parse_args()
files_processed = 0
files_watermarked = 0
for dirName, subdirList, fileList in os.walk(args.root):
if args.exclude is not None and args.exclude in dirName:
continue
#print('Walking directory: %s' % dirName)
for fname in fileList:
files_processed += 1
#print(' Processing %s' % os.path.join(dirName, fname))
if args.extension in fname and args.watermark not in fname and args.name not in fname:
ext = '.'.join(os.path.basename(fname).split('.')[1:])
orig = os.path.join(dirName, fname)
new_name = os.path.join(dirName, '%s.%s' % (os.path.basename(fname).split('.')[0] + args.name, ext))
if not os.path.exists(new_name):
files_watermarked += 1
print(' Convert %s to %s' % (orig, new_name))
os.system('composite -dissolve 30%% -gravity SouthEast %s "%s" "%s"' % (args.watermark, orig, new_name))
print("Files Processed: %s" % "{:,}".format(files_processed))
print("Files Watermarked: %s" % "{:,}".format(files_watermarked))
if __name__ == '__main__':
main()
Run it like this:
./add_watermarks.py --root . --watermark copyright.jpg --exclude marketplace
To create the watermark I just created the text in a Word document then did a screen shot of the small area of the text to end up with a copyright.jpg file.