2

I'm trying to transform an image into a matrix of it's rbg values in c++, i really like the simplicity of PIL on handling different images extensions, so i currently have two codes

from PIL import Image

img=Image.open("oi.png")

pixel=img.load()

width,height=img.size

print(height)
print(width)

def rgb(r, g, b):
    return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)

for x in range(width):
        for y in range(height):
                print(rgb(pixel[x,y][0],pixel[x,y][1],pixel[x,y][2]))

and to recieve in C++

#include <bits/stdc++.h>
using namespace std;
#define __ ios_base::sync_with_stdio(false);cin.tie(NULL);
int main(){__
        long long height,width;cin>>height>>width;
        unsigned long img[width][height];        
        for(long long j=0; j<height;j++) for (long long i=0; i<width;i++){
                cin>>img[i][j];
        }
return 0;}

and i am connecting both trough terminal withpython3 code.py | ./code it works for really small images, but for bigger ones it returns BrokenPipeError: [Errno 32] Broken pipe

What should i do? is there a better way to achieve what i am trying to achieve?

I want to connect python output to c++ input even with big outputs without Broken pipe error

5

1 Answer 1

1

"Broken pipe" means that a program tried to write to a pipe that no longer had any programs reading from it. This means that your C++ program is exiting before it should.

For a 1000x1000 image, assuming you're running this on x86_64 Linux, img is 8MB. I suspect that's too big for the stack, which is causing your C++ program to crash. You can fix that by allocating img on the heap instead.

Not the answer you're looking for? Browse other questions tagged or ask your own question.