PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How can I create a bunch of images overlaying two images quickly?

 July 28, 2022     c++, cimg, image, imagemagick, performance     No comments   

Issue

I'm trying with ImageMagick GraphicsMagick and CImg but it takes 10 minutes to complete 10,000 images.

I need to do this in 3 minutes.

How I could do this task faster?

This is my code:

#define cimg_use_png
#define cimg_display 0
#include "CImg.h"
#include <iostream>
#include <vector>

using namespace cimg_library;
int main() {
for (int i = 0; i < 10000; ++i){
   CImg<unsigned char> gradient("gradient.png");
   CImg<unsigned char> overlay("overlay.png");
   gradient.draw_image(80,150,overlay);
   gradient.save_png("result.png");

}
}

How I could do this faster using other libraries or another language? I have a 2 Ghz processor and 4 GB of RAM.

Please Help Me


Solution

The slowest part is the decoding & encoding of the PNG i/o operations. If we can move the read operations outside of the for loop, and clone the image data with the copy constructor, then we should be able to improve the speed under 3 minutes.

#define cimg_use_png
#define cimg_display 0
#include "CImg.h"
#include <iostream>
#include <vector>

using namespace cimg_library;

int main() {
    CImg<unsigned char> parent_gradient("gradient.png");
    CImg<unsigned char> parent_overlay("overlay.png");
    for (int i = 0; i < 10000; ++i){
        CImg<unsigned char> gradient(parent_gradient);
        CImg<unsigned char> overlay(parent_overlay);
        gradient.draw_image(80,150,overlay);
        gradient.save_png("result.png");
    }
}

We can go farther by implementing OpenMP to distribute the work load across the CPU threads. On my mac, this drops the 10000 iterations under 20 secs.

#define cimg_use_png
#define cimg_display 0
#include "CImg.h"
#include <iostream>
#include <vector>

using namespace cimg_library;

int main() {
    CImg<unsigned char> parent_gradient("gradient.png");
    CImg<unsigned char> parent_overlay("overlay.png");
    #pragma omp parallel for
    for (int i = 0; i < 10000; ++i){
        CImg<unsigned char> gradient(parent_gradient);
        CImg<unsigned char> overlay(parent_overlay);
        gradient.draw_image(80,150,overlay);
        gradient.save_png("result.png");
    }
}


Answered By - emcconville
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing