简体   繁体   English

如何使用Marvin Java库(或任何其他免费库)附加2个BIG图像?

[英]How can I append 2 BIG images using Marvin java library(or any other free lib)?

I have 2 jpegs, about 16 000 x 24 000 px. 我有2个jpeg,大约16000 x 24000 px。 I have to rotate the second and append it on top of the first, something like this 我必须旋转第二个并将其附加在第一个顶部,像这样

在此处输入图片说明 .

I've found in the docs how to rotate (MarvinImage.rotate) but i haven't found a method that can append the 2 images. 我已经在文档中找到了如何旋转(MarvinImage.rotate),但是我还没有找到可以附加2张图像的方法。

Also, any suggestions of other libraries that can do this is also greatly appreciated. 同样,也非常感谢其他图书馆提供的建议。 What I've tried until now: 到目前为止,我一直在尝试:

  • BufferedImage and ImageIO: takes a whole lot of memory, would probably work if the write would work (JPEGImageWriter basically complains about the image being too big - integer overflow) BufferedImage和ImageIO:占用大量内存,如果写入有效,则可能会起作用(JPEGImageWriter基本上抱怨图像太大-整数溢出)

  • ImageMagick and im4java - works but terribly slow (13 minutes and 100% disk usage) ImageMagick和im4java-可以运行,但速度非常慢(13分钟,磁盘使用率100%)

Thanks! 谢谢!

Bogdan 博格丹

libvips can do this quickly and in little memory, but unfortunately there's no convenient Java binding. libvips可以用很少的内存快速完成此操作,但是不幸的是,没有方便的Java绑定。 You'd need to write a few lines using something like pyvips and then shell out to that. 您需要使用诸如pyvips之类的代码编写几行,然后对此进行解释。

For example: 例如:

import sys
import pyvips

one = pyvips.Image.new_from_file(sys.argv[1])
two = pyvips.Image.new_from_file(sys.argv[2], access='sequential')
one.rot180().join(two, 'vertical').write_to_file(sys.argv[3])

The access= hint on new_from_file in two means we plan to read the second image top-to-bottom, ie. new_from_fileaccess=提示有two含义,即我们计划从上至下读取第二张图像,即。 in the same order that pixels appear in the jpg file. 像素在jpg文件中的显示顺序相同。 This will let libvips stream that image, so it can overlap the decode of two with the write of the output image. 这将使libvips流传输该图像,因此它可以将two图像的解码与输出图像的写入重叠。

On this 2015 laptop, I see: 在这款2015年的笔记本电脑上,我看到了:

$ vipsheader ~/pics/top.jpg ~/pics/bot.jpg
/home/john/pics/top.jpg: 16000x24000 uchar, 3 bands, srgb, jpegload
/home/john/pics/bot.jpg: 16000x24000 uchar, 3 bands, srgb, jpegload
$ /usr/bin/time -f %M:%e ./join.py ~/pics/top.jpg ~/pics/bot.jpg x.jpg
115236:27.85
$ vipsheader x.jpg 
x.jpg: 16000x48000 uchar, 3 bands, srgb, jpegload

So a peak of 115MB of memory, and it runs in 28s of real time. 因此,峰值内存为115MB,可以在28秒内实时运行。

That will create a temporary file for one so it can do the rotate. 这将为one创建一个临时文件,以便可以进行轮换。 If you are OK using a lot of memory, you can try: 如果可以使用大量内存,可以尝试:

one = pyvips.Image.new_from_file(sys.argv[2], memory=True)

That will force libvips to open via a memory area. 这将迫使libvips通过存储区打开。 I now see: 我现在看到:

$ /usr/bin/time -f %M:%e ./join.py ~/pics/top.jpg ~/pics/bot.jpg x.jpg
1216812:14.53

Only 15s of real time, but a painful 1.2GB peak memory use. 实时只有15秒,但峰值内存使用量高达1.2GB。

In ImageMagick 6, that is easy to do. 在ImageMagick 6中,这很容易做到。

Input 1 (lena.jpg): 输入1(lena.jpg):

在此处输入图片说明

Input 2 (mandril3.jpg): 输入2(mandril3.jpg):

在此处输入图片说明

Unix Syntax: Unix语法:

convert lena.jpg \( mandril3.jpg -rotate 180 \) +swap -append result.jpg


在此处输入图片说明

For Windows syntax, remove the \\s. 对于Windows语法,请删除\\ s。 For ImageMagick 7, replace convert with magick. 对于ImageMagick 7,将convert替换为magick。

ImageMagick comes with Linux distributions. ImageMagick随Linux发行版一起提供。 It is also available for Mac OSX and Windows. 它还适用于Mac OSX和Windows。

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

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