简体   繁体   中英

Resize and merge images horizontally

How to resize images (height = average of the height of all images) and merge them horizontally left-to-right? I am using the Ubuntu Linux distro.

I tried with libvips . It's a streaming image-processing library, so it can generate the output image without needing to load all of the input images into memory. This means it can generate very large images on quite modest computers.

#!/usr/bin/env python

import sys
import pyvips

total_height = 0.0
for filename in sys.argv[2:]:
    tile = pyvips.Image.new_from_file(filename)
    total_height += tile.height
average_height = total_height / len(sys.argv[2:])

image = None
for filename in sys.argv[2:]:
    # "sequential" access hints that we want to stream the image
    tile = pyvips.Image.new_from_file(filename, access="sequential")   
    tile = tile.resize(average_height / tile.height)
    image = tile if not image else image.join(tile, "horizontal")

image.write_to_file(sys.argv[1])

I tried on a set of 27 test jpg images I had:

$ time ../avgmerge.py x.tif tiles/*.jpg
loading tiles/ak01.jpg ...
...
loading tiles/curiossmall.jpg ...
writing x.tif ...
real    0m2.742s
user    0m4.800s
sys     0m0.200s
$ vipsheader x.tif
x.tif: 34954x961 uchar, 3 bands, srgb, tiffload

So with this dataset, it made a 35,000 x 960 pixel image in 2.7s on my modest laptop.

This is the imergh.py script, in Python, that does just that. Imagemagick is required. Note that before running the script, you need to cd into the directory with the images. Some image viewers suited to view large images are Viewnior, Nomacs and Gwenview. The script will generate some tmpfXXXX.png images and a file called houtputh.png with the end result.

#!/usr/bin/python

import os

f = os.popen('/bin/ls -1')
fil = f.read()
arfils = fil.split("\n")
arfils.pop()
num = 0
tot = 0

for snc in arfils:
     f = os.popen( "/usr/bin/identify -ping -format '%w %h' " + '\"' + snc + '\"' )
     rslt = f.read()
     woh = rslt.split(" ")
     # 0 for width and 1 for height
     intvl = int(woh[1])
     tot = tot + intvl
     num = num + 1

avg = tot // num

#resize images
num = 1
allfil = ""
for snc in arfils:
    nout = "tmpf" + str(num).zfill(4) + ".png"
    allfil = allfil + nout + " "
    convcmd = "convert " + '\"' + snc + '\"' + " -resize x" + str(avg) + " -quality 100 "
    convcmd = convcmd + '\"' + nout + '\"'
    #print convcmd
    f = os.popen(convcmd)
    num = num + 1

mrg = "convert +append " + allfil + "houtputh.png"
f = os.popen(mrg)

Using a combination of only ImageMagick and bash shell scripting (no Python), you can do:

cd path_to/images_folder
list=$(ls *)
i=0
for img in $list; do
htArr[$i]=$(convert -ping $img -format "%h" info:)
i=$((i+1))
done
num=${#htArr[*]}
total_ht=0
for ((i=0; i<num; i++)); do
ht=${htArr[$i]}
total_ht=$((total_ht+ht))
done
average_ht=$(convert xc: -format "%[fx:round($total_ht/$num)]" info:)
convert $list -resize x$average_ht +append result.jpg

Fill in your path_to/images_folder and copy and paste this into a terminal window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM