Image file to vector of pixels with CImg?
Question: I have this in python:

    
    
    import Image
    import numpy as np
    import random
    
    img = Image.open('img.jpg')
    
    #turn img to list of rgb tuples and scramble 
    pixels = list(img.getdata())
    pixels.reverse()
    random.shuffle(pixels)
    
    #make new image using scrambled pixels
    img2 = Image.new(img.mode, img.size)
    img2.putdata(pixels)
    
    img2.save('newimg.png')
    

I figured I should be working in c++ to keep stuff I learned last semester
fresh in my head and to prepare for the class I have next semester which also
revolves around c++. So, I found CImg and got a bit overwhelmed by the
documentation. So, what would be CImg's equivalent of line 8?

My end goal is to be able to scramble an image using a known pattern, then use
that pattern to unscramble later. I don't know if this is possible though. To
me its a bit like asking the following:

given:

    
    
    srand(x);
    int rand_num = rand() % 10;
    

and rand_num = 7

find x.


Answer: As far as know `CImg` provides
[iterators](http://cimg.sourceforge.net/reference/structcimg__library_1_1CImg.html)
to loop through every pixel. As such and provided that your compiler support
C++11, you could use
[std::shuffle](http://www.cplusplus.com/reference/algorithm/shuffle/) to
shuffle the pixels of your image (see example below).

    
    
    CImg<float> img("lena.jpg"); // Load image from file.
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::shuffle(img.begin(), img.end(), std::default_random_engine(seed));
    

