简体   繁体   English

水平调整和合并图像

[英]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. 我正在使用Ubuntu Linux发行版。

I tried with libvips . 我尝试了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: 我尝试了一组27张测试jpg图像:

$ 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. 因此,有了这个数据集,它在我普通的笔记本电脑上以2.7s的速度拍摄了35,000 x 960像素的图像。

This is the imergh.py script, in Python, that does just that. 这是用Python编写的imergh.py脚本。 Imagemagick is required. 必须使用Imagemagick。 Note that before running the script, you need to cd into the directory with the images. 请注意,在运行脚本之前,您需要将cd放入包含映像的目录中。 Some image viewers suited to view large images are Viewnior, Nomacs and Gwenview. Viewnior,Nomacs和Gwenview是一些适合查看大图像的图像查看器。 The script will generate some tmpfXXXX.png images and a file called houtputh.png with the end result. 该脚本将生成一些tmpfXXXX.png图像和一个名为houtputh.png的文件, houtputh.png带有最终结果。

#!/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: 结合使用ImageMagick和bash shell脚本(不使用Python),可以执行以下操作:

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. 填写path_to / images_folder并将其复制并粘贴到终端窗口中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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