How to resolve the algorithm Bitmap/Write a PPM file step by step in the Python programming language
How to resolve the algorithm Bitmap/Write a PPM file step by step in the Python programming language
Table of Contents
Problem Statement
Using the data storage type defined on this page for raster images, write the image to a PPM file (binary P6 preferred). (Read the definition of PPM file on Wikipedia.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitmap/Write a PPM file step by step in the Python programming language
This code implements a Bitmap class that can be used to draw images and save them in the PPM (Portable Pixmap) format. The PPM format is a simple image format that stores the image data in a text file.
The writeppmp3()
method is used to save the bitmap in the PPM P3 format. The P3 format is a text-based format that stores the image data as a list of RGB values, with each value represented by a single byte.
The writeppm()
method is a more general method that can be used to save the bitmap in either the P3 or P6 format. The P6 format is a binary format that stores the image data as a series of bytes, with each byte representing a single RGB value.
The code first creates a bitmap object and then uses the fillrect()
method to draw a rectangle on the bitmap. The set()
method is then used to set the color of a single pixel on the bitmap.
The writeppmp3()
method is then used to save the bitmap to a file. The print()
statement is used to print the contents of the file to the console.
The output of the print()
statement shows that the PPM file contains the following data:
P3
# generated from Bitmap.writeppmp3
4 4
255
0 0 0 0 0 0 0 0 0 127 0 63
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 255 255 255 0 0 0 0 0 0
0 0 0 255 255 255 0 0 0 0 0 0
The first line of the file contains the magic number P3
, which indicates that the file is a PPM file in the P3 format. The second line of the file contains a comment that was generated by the writeppmp3()
method. The third line of the file contains the width and height of the image, which are both 4. The fourth line of the file contains the maximum value of the RGB values, which is 255. The remaining lines of the file contain the RGB values for each pixel in the image.
The code then creates a new file called tmp.ppm
and uses the writeppm()
method to save the bitmap to the file. The close()
method is then used to close the file.
Source code in the python programming language
# String masquerading as ppm file (version P3)
import io
ppmfileout = io.StringIO('')
def writeppmp3(self, f):
self.writeppm(f, ppmformat='P3')
def writeppm(self, f, ppmformat='P6'):
assert ppmformat in ['P3', 'P6'], 'Format wrong'
magic = ppmformat + '\n'
comment = '# generated from Bitmap.writeppm\n'
maxval = max(max(max(bit) for bit in row) for row in self.map)
assert ppmformat == 'P3' or 0 <= maxval < 256, 'R,G,B must fit in a byte'
if ppmformat == 'P6':
fwrite = lambda s: f.write(bytes(s, 'UTF-8'))
maxval = 255
else:
fwrite = f.write
numsize=len(str(maxval))
fwrite(magic)
fwrite(comment)
fwrite('%i %i\n%i\n' % (self.width, self.height, maxval))
for h in range(self.height-1, -1, -1):
for w in range(self.width):
r, g, b = self.get(w, h)
if ppmformat == 'P3':
fwrite(' %*i %*i %*i' % (numsize, r, numsize, g, numsize, b))
else:
fwrite('%c%c%c' % (r, g, b))
if ppmformat == 'P3':
fwrite('\n')
Bitmap.writeppmp3 = writeppmp3
Bitmap.writeppm = writeppm
# Draw something simple
bitmap = Bitmap(4, 4, black)
bitmap.fillrect(1, 0, 1, 2, white)
bitmap.set(3, 3, Colour(127, 0, 63))
# Write to the open 'file' handle
bitmap.writeppmp3(ppmfileout)
# Whats in the generated PPM file
print(ppmfileout.getvalue())
'''
The print statement above produces the following output :
P3
# generated from Bitmap.writeppmp3
4 4
255
0 0 0 0 0 0 0 0 0 127 0 63
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 255 255 255 0 0 0 0 0 0
0 0 0 255 255 255 0 0 0 0 0 0
'''
# Write a P6 file
ppmfileout = open('tmp.ppm', 'wb')
bitmap.writeppm(ppmfileout)
ppmfileout.close()
You may also check:How to resolve the algorithm Graph colouring step by step in the 11l programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Tcl programming language
You may also check:How to resolve the algorithm Permutations/Rank of a permutation step by step in the Quackery programming language
You may also check:How to resolve the algorithm Array length step by step in the Nu programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Clojure programming language