Uncategorized

arrays – Adding transparency to the program written in Python


I found the code below from one of the posts here very useful, but would someone, please, tell me how to add an alpha channel to that?

I just need to add opacity from 0 to 100%. I know ppm file supports it and I am completely new to Python.

I tried adding the 4-th parameter as RGB(A), but it didn’t work.

I need exactly a *.ppm file generated by the program below with transparency added to it.

import array
width,height = 800,600

PPMheader="P6\n" +str(width) + ' ' +str(height) + '\n255\n'

# Create and fill a red PPM image
image = array.array('B', [255, 0, 0] * width * height)

# Save as PPM image
with open('result.ppm', 'wb') as f:
   f.write(bytearray(PPMheader, 'ascii'))
   image.tofile(f)



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *