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

时间:2019-04-10 15:18:05

标签: java image-processing javax.imageio marvin-framework

我有2个jpeg,约16000 x 24000 px。我必须旋转第二个并将其附加在第一个顶部,像这样

enter image description here  

我已经在文档中找到了如何旋转(MarvinImage.rotate),但是我还没有找到可以附加2张图像的方法。

此外,也非常感谢其他图书馆提供的建议。到目前为止,我一直在尝试:

  • BufferedImage和ImageIO:占用大量内存,如果写操作可行,则可能工作(JPEGImageWriter基本上抱怨图像太大-整数溢出)

  • ImageMagick和im4java-可以运行,但是非常慢(13分钟和100%的磁盘使用率)

谢谢!

博格丹

2 个答案:

答案 0 :(得分:2)

libvips可以在很少的内存中快速完成此操作,但是不幸的是,没有方便的Java绑定。您需要使用pyvips之类的代码编写几行,然后对此进行解释。

例如:

access=

twonew_from_file上的two提示意味着我们计划从上至下阅读第二张图像。像素在jpg文件中的显示顺序相同。这将使libvips流传输该图像,因此它可以将$ 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 的解码与输出图像的写入重叠。

在此2015年笔记本电脑上,我看到了:

one

因此,峰值内存为115MB,它以28s的实时速度运行。

这将为one = pyvips.Image.new_from_file(sys.argv[2], memory=True) 创建一个临时文件,以便可以进行轮换。如果确定可以使用大量内存,则可以尝试:

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

这将强制libvips通过存储区打开。我现在看到了:

static class Program
{

    #region Private variable
    static readonly bool IsDebugMode = false;
    #endregion Private variable

    #region Constrcutors
    static Program()
    {
 #if DEBUG
        IsDebugMode = true;
 #endif
    }
    #endregion

    #region Main

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {

        if (IsDebugMode)
        {
            MyService myService = new MyService(args);
            myService.OnDebug();             
        }
        else
        {
            ServiceBase[] services = new ServiceBase[] { new MyService (args) };
            services.Run(args);
        }
    }

    #endregion Main        
}

只有15s的实时时间,但峰值内存使用量高达1.2GB。

答案 1 :(得分:0)

在ImageMagick 6中,这很容易做到。

输入1(lena.jpg):

enter image description here

输入2(mandril3.jpg):

enter image description here

Unix语法:

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


enter image description here

对于Windows语法,请删除\ s。对于ImageMagick 7,将convert替换为magick。

ImageMagick随Linux发行版一起提供。它还适用于Mac OSX和Windows。