Printing image on the file

So I got stuck at Image printing. Does the pixel that printed = red + blue + green /3? If not may I ask for hint


can you please specify your question?

We print the image to a file using fprintf(). The format of the file is specified in the project descripition.


yes, i was confused earlier on what to print. But it seems i figures it out. THanks !

You use the PPM format from the project descripition, so your first few lines of the image should look like this:

<width> <height>

Where <width> and <height> are replaced by the corresponding integer.
Then you have to fill in the RGB values of the pixels (where ... is in the example above).


yes, i know the format, but i tought what you print should be for every pixel = red + green + blue / 3. But that is brightness :frowning:

And I’ve found what i should print. Thanks!

Does the image need to be formatted?
Is it sufficient if it looks like this:

255 0 255
255 255 255

or does it for some test need to look like this:

255   0 255
255 255 255

I tested it without formatting, and it seems like the image_read_from_file function can handle this. But I am not quiet sure about this.

The Wikipedia article linked in the comment of ‘image_write_to_file’ states that

“It is not required that pixels are nicely lined up, the format ignores whitespaces and linefeeds in the data section, although it’s recommended that no line is longer than 76 characters”

Not sure if it’s a problem for the test though, I assume it is not.

1 Like

would you please correct me for my concluded steps for that:

  1. open the file with fopen, do we need to rename it in a certain form?
  2. if the file is == NULL, then exit
  3. otherwise, we use fprint to write the image dates in the file:

    …about the r,g,b von every pixel here, we only need to use two nested loops to print theses three dates for every pixels out…should the printed in order r,g,b…? do wee need to add \n for every pixel?
  4. last close the file?

Hey @Lux,

In general, you’ve got the idea right. Yeah, the order should be R G B for every pixel.

p1_r p1_g p1_b p2_r p2_g p2_b
p3_r p3_g p3_b p4_r p4_g p4_b
p5_r p5_g p5_b p6_r p6_g p6_b

You don’t need to rename any file, since you will create a new file named with the passed argument filename. This also explains, why you don’t need to exit, if the file does not exist, since it will usually not exist before your code runned successfully.

I got it, thanks David :+1:

1 Like

you are welcome! :smiley: