Welcome, guest | Sign In | My Account | Store | Cart

It creates image output without using any external library.

PPM image files can be viewed and converted to other image file formats using many applications, GIMP or IrfanView for example.

Python, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Mandelbrot Fractal image output to ppm file
# FB - 20150117
imgx = 512; imgy = 512
xa = -2.0; xb = 1.0
ya = -1.5; yb = 1.5
maxIt = 256
rgbPixels = ""
for ky in range(imgy):
    for kx in range(imgx):
        c = complex(xa + (xb - xa) * kx / imgx, ya + (yb - ya) * ky / imgy)
        z = complex(0.0, 0.0)
        for i in range(maxIt):
            z = z * z + c
            if abs(z) >= 2.0:
                break
        rgbPixels += chr(i % 4 * 64) + chr(i % 8 * 32) + chr(i % 16 * 16)

f = open("ManFr.ppm", "wb")
f.write("P6\n")
f.write("# ManFr.ppm\n") # comment
f.write(str(imgx) + " " + str(imgy) + "\n")
f.write(str(255) + "\n") # max color value
f.write(rgbPixels)
f.close()

2 comments

FB36 (author) 6 years, 10 months ago  # | flag

This is how each pixel can be set directly:

# Mandelbrot Fractal image output to ppm file
# FB - 20150117
imgx = 512; imgy = 512
xa = -2.0; xb = 1.0
ya = -1.5; yb = 1.5
maxIt = 256
rgbPixels = [chr(0) + chr(0) + chr(0) for p in range(imgx * imgy)]
for ky in range(imgy):
    for kx in range(imgx):
        c = complex(xa + (xb - xa) * kx / imgx, ya + (yb - ya) * ky / imgy)
        z = complex(0.0, 0.0)
        for i in range(maxIt):
            z = z * z + c
            if abs(z) >= 2.0:
                break
        rgbPixels[kx + ky * imgy] = chr(i % 4 * 64) + chr(i % 8 * 32) + chr(i % 16 * 16)

f = open("ManFr.ppm", "wb")
f.write("P6\n")
f.write("# ManFr.ppm\n") # comment
f.write(str(imgx) + " " + str(imgy) + "\n")
f.write(str(255) + "\n") # max color value
f.write("".join(rgbPixels))
f.close()
FB36 (author) 6 years, 10 months ago  # | flag

PPM image processing example:

# PPM Image Processing Example
# FB - 20150117
def LoadPPM(filename):
    f = open(filename, "rb")
    lines = []
    while True:
        line = f.readline().strip()
        if line == "255": # max color value line
            break
        if not line.startswith("#"): # skip comment line(s)
            lines.append(line)
    rgbPixels = f.read()
    f.close()
    imgximgy = lines[1].split(" ")
    imgx = int(imgximgy[0]); imgy = int(imgximgy[1])
    return (imgx, imgy, rgbPixels)

def SavePPM(imgx, imgy, rgbPixels, filename):
    f = open(filename, "wb")
    f.write("P6\n")
    f.write(str(imgx) + " " + str(imgy) + "\n")
    f.write(str(255) + "\n") # max color value
    f.write(rgbPixels)
    f.close()

def ProcessImage(imgx, imgy, rgbPixels):
    rgbPixelsList = []
    for i in range(len(rgbPixels) / 3):
        rgbPixelsList.append(rgbPixels[i * 3 : i * 3 + 3])
    for ky in range(imgy):
        for kx in range(imgx):
            rgb = rgbPixelsList[kx + ky * imgy]
            r = ord(rgb[0]); g = ord(rgb[1]); b = ord(rgb[2])
            r = 255 - r; g = 255 - g; b = 255 - b # reverse colors
            a = (r + g + b) / 3; r = a; g = a; b = a # convert to grayscale
            rgbPixelsList[kx + ky * imgy] = chr(r) + chr(g) + chr(b)    
    return "".join(rgbPixelsList)

# TEST
(imgx, imgy, rgbPixels) = LoadPPM("ManFr.ppm")
rgbPixels = ProcessImage(imgx, imgy, rgbPixels)
SavePPM(imgx, imgy, rgbPixels, "ManFr2.ppm")
Created by FB36 on Sat, 17 Jan 2015 (MIT)
Python recipes (4591)
FB36's recipes (148)

Required Modules

  • (none specified)

Other Information and Tasks