예제 #1
0
파일: rotation.py 프로젝트: Swartacus/IP
def rotate(filename, angle):
    # open file for use
    [size, img_mxclr, img_in] = reader.ppmreader(filename)
    # get cos and sin of angle
    rad = math.radians(float(angle))
    cosine = math.cos(rad)
    sine = math.sin(rad)

    cen = [int(size[0] / 2), int(size[1] / 2)]

    # calculate new bounding rectangle
    nsize = [
        int(math.ceil(abs(size[0] * cosine) + abs(size[1] * sine))),
        int(math.ceil(abs(size[1] * cosine) + abs(size[0] * sine)))
    ]
    ncen = [int(nsize[0] / 2), int(nsize[1] / 2)]
    offset = [
        int(math.ceil((size[0] - nsize[0]) / 2)),
        int(math.ceil((size[1] - nsize[1]) / 2))
    ]
    cdif = [ncen[0] - cen[0], ncen[1] - cen[1]]

    out = [[0, 0, 0]] * nsize[0] * nsize[1]

    for i in range(nsize[1]):
        for j in range(nsize[0]):
            # calculate new pixel x,y index
            xn = int(
                math.cos(rad) * (j - ncen[0]) - math.sin(rad) *
                (i - ncen[1])) + ncen[0] - 1
            yn = int(
                math.sin(rad) * (j - ncen[0]) + math.cos(rad) *
                (i - ncen[1])) + ncen[1] - 1

            # only move pixels that are within the limits of the image.
            xdif = xn - cdif[0]
            ydif = yn - cdif[1]
            if (xdif < size[0]) and (xdif >= 0) and (ydif <
                                                     size[1]) and (ydif >= 0):
                out[i * nsize[0] + j] = img_in[ydif * size[0] + xdif]

    rot = open('rotated.ppm', 'w')

    header = "P3\n# CREATOR: rotation.py\n{0} {1}\n{2}\n".format(
        nsize[0], nsize[1], img_mxclr)

    rot.write(header)

    for p in out:
        rot.write("{0} {1} {2}\n".format(p[0], p[1], p[2]))

    rot.close
예제 #2
0
def main(filename):
	[size,mx,pixels] = image_in.ppmreader(filename)

	out = open('average.pgm','w')

	header = "P2\n# CREATOR: average.py\n{0} {1}\n{2}\n".format(size[0],size[1],mx)

	out.write(header)

	#Apply grayscale
	for i in pixels:
		g = (i[0]+i[1]+i[2])/3
		out.write("%d\n"%g)

	out.close()
예제 #3
0
def main(filename):
    [size, mx, pixels] = reader.ppmreader(filename)

    header = "P2\n# CREATOR: intensity.py\n{0} {1}\n{2}\n".format(
        size[0], size[1], mx)

    ppm_out = open('intensity.pgm', 'w')

    ppm_out.write(header)

    #Apply grayscale
    for i in pixels:
        g = (max(i[0], i[1], i[2]) + min(i[0], i[1], i[2])) / 2
        ppm_out.write("%d\n" % (g))

    ppm_out.close()
예제 #4
0
파일: weighted.py 프로젝트: Swartacus/IP
def main(filename):
    #open and extract headers from .ppm file.
    [size, mx, pixels] = reader.ppmreader(filename)

    header = "P2\n# CREATOR: weighted.py\n{0} {1}\n{2}\n".format(
        size[0], size[1], mx)

    #create output file to print to.
    ppm_out = open('weighted.pgm', 'w')

    ppm_out.write(header)

    #Apply transformation to pixels and print
    for i in pixels:
        g = i[0] * 0.299 + i[1] * 0.587 + i[2] * 0.114
        ppm_out.write("%d\n" % (g))

    ppm_out.close()
예제 #5
0
파일: channels.py 프로젝트: Swartacus/IP
def main(filename):
    [size, mx, pixels] = image_in.ppmreader(filename)

    header = "P2\n# CREATOR: channels.py\n{0} {1}\n{2}\n".format(
        size[0], size[1], mx)

    r = open('rchannel.pgm', 'w')
    g = open('gchannel.pgm', 'w')
    b = open('bchannel.pgm', 'w')

    r.write(header)
    g.write(header)
    b.write(header)

    #Apply grayscale
    for i in pixels:
        r.write("%d\n" % (i[0]))
        g.write("%d\n" % (i[1]))
        b.write("%d\n" % (i[2]))

    r.close()
    g.close()
    b.close()