Exemplo n.º 1
0
def main():
    ap = ArgumentParser()
    ap.add_argument('P')

    P = int(sys.argv[1])
    tau = float(sys.argv[2])
    delta = float(sys.argv[3])
    pic = Picture(sys.argv[4])
    prevBeads = BlobFinder(pic, tau).getBeads(P)
    # for every frame:
    for i in sys.argv[5:]:
        currBeads = BlobFinder(Picture(i), tau).getBeads(P)
        # for every bead in that frame:
        for currBead in range(len(currBeads)):
            # intialize shortest_dist with largest possible dimension:
            shortest_dist = max([pic.width(), pic.height()])
            # search for currBead closest to prevBead:
            for v in range(min([len(currBeads), len(prevBeads)])):
                d = prevBeads[v].distanceTo(currBeads[currBead])
                if d < shortest_dist:
                    shortest_dist = d
            # confirm that displacement is within delta:
            if shortest_dist <= delta:
                # if yes, then show the distance:
                stdio.writef('%.4f\n', shortest_dist)

        stdio.writeln()
        prevBeads = currBeads
Exemplo n.º 2
0
from charge import Charge
import stddraw
import stdarray
from picture import Picture
from color import Color

a = stdarray.create1D(3)
a[0] = Charge(.4, .6, 50)
a[1] = Charge(.5, .5, -5)
a[2] = Charge(.6, .6, 50)

MAX_GRAY_SCALE = 255
p = Picture()
stddraw.setCanvasSize(p.width(),p.height())
for t in range(100):
    # Compute the picture p.
    for col in range(p.width()):
        for row in range(p.height()):
            # Compute pixel color.
            x = 1.0 * col / p.width()
            y = 1.0 * row / p.height()
            v = 0.0

            for i in range(3):
                v += a[i].potentialAt(x, y)    
            v = (MAX_GRAY_SCALE / 2.0)  + (v / 2.0e10)
            if v < 0:
                grayScale = 0
            elif v > MAX_GRAY_SCALE:
                grayScale = MAX_GRAY_SCALE
            else:
Exemplo n.º 3
0
import sys
import stddraw
import luminance
from picture import Picture

pic = Picture(sys.argv[1])

for col in range(pic.width()):
    for row in range(pic.height()):
        pixel = pic.get(col, row)
        gray = luminance.toGray(pixel)
        pic.set(col, row, gray)

stddraw.setCanvasSize(pic.width(), pic.height())
stddraw.picture(pic)
stddraw.show()
Exemplo n.º 4
0
MAX_GRAY_SCALE = 255

# Read charges from standard input into an array.
n = stdio.readInt()
charges = stdarray.create1D(n)
for i in range(n):
    x0 = stdio.readFloat()
    y0 = stdio.readFloat()
    q0 = stdio.readFloat()
    charges[i] = Charge(x0, y0, q0)

# Create a Picture depicting potential values.
pic = Picture()
for col in range(pic.width()):
    for row in range(pic.height()):
        # Compute pixel color.
        x = 1.0 * col / pic.width()
        y = 1.0 * row / pic.height()
        v = 0.0

        for i in range(n):
            v += charges[i].potentialAt(x, y)    
        v = (MAX_GRAY_SCALE / 2.0)  + (v / 2.0e10)
        if v < 0:
            grayScale = 0
        elif v > MAX_GRAY_SCALE:
            grayScale = MAX_GRAY_SCALE
        else:
            grayScale = int(v)            
        color = Color(grayScale, grayScale, grayScale)
Exemplo n.º 5
0
# Accept the name of a JPG or PNG image file, an integer w, and
# an integer h as command line arguments. Read an image from the
# file, and display the image scaled to width w and height h.

fileName = sys.argv[1]
w = int(sys.argv[2])
h = int(sys.argv[3])

source = Picture(fileName)
target = Picture(w, h)

for tCol in range(w):
    for tRow in range(h):
        sCol = tCol * source.width() // w
        sRow = tRow * source.height() // h
        target.set(tCol, tRow, source.get(sCol, sRow))

stddraw.setCanvasSize(w, h)
stddraw.picture(target)
stddraw.show()

#-----------------------------------------------------------------------

# python scale.py mandrill.jpg 800 800

# python scale.py mandrill.jpg 600 300

# python scale.py mandrill.jpg 200 400

# python scale.py mandrill.jpg 200 200
Exemplo n.º 6
0
# Read charges from standard input into an array.
#n = stdio.readInt()
n = eval(sys.argv[1])
charges = stdarray.create1D(n)
for i in range(n):
    x0 = round(stdrandom.uniformFloat(0, 1), 2)
    y0 = round(stdrandom.uniformFloat(0, 1), 2)
    q0 = stdrandom.gaussian(50, 150)
    charges[i] = Charge(x0, y0, q0)
    print(str(charges[i]))

# Create a Picture depicting potential values.
pic = Picture()
for col in range(pic.width()):
    for row in range(pic.height()):
        # Compute pixel color.
        x = 1.0 * col / pic.width()
        y = 1.0 * row / pic.height()
        v = 0.0

        for i in range(n):
            v += charges[i].potentialAt(x, y)
        v = (MAX_GRAY_SCALE / 2.0) + (v / 2.0e10)
        if v < 0:
            grayScale = 0
        elif v > MAX_GRAY_SCALE:
            grayScale = MAX_GRAY_SCALE
        else:
            grayScale = int(v)
        color = Color(grayScale, grayScale, grayScale)
#从命令行接收参数,旋转图片
#ti = (si-ci)*costha-(sj-cj)sintha+ci
#tj = (si-ci)*sintha+(sj-cj)costha+cj

import sys
from picture import Picture
import stddraw
import math

p = sys.argv[1]
degree = sys.argv[2]

tha = math.radians(eval(degree))  #radian(弧度)转角度
source = Picture(p)
w = source.width()
h = source.height()
ci = h / 2
cj = w / 2
target = Picture(w, h)

for si in range(h):
    for sj in range(w):
        ti = (si - ci) * math.cos(tha) - (sj - cj) * math.sin(tha) + ci
        tj = (si - ci) * math.sin(tha) + (sj - cj) * math.cos(tha) + cj
        #target.set(int(tj),int(ti),source.get(sj,si))
        if 0 <= tj < w and 0 <= ti < h:
            target.set(int(sj), int(si), source.get(int(tj), int(ti)))

stddraw.picture(target)
stddraw.show()
Exemplo n.º 8
0
# wave.py
#-----------------------------------------------------------------------

import sys
import math
import stddraw
from picture import Picture 

# Accept a file name as a command-line argument. Read the image from
# the specified file, wave the image, and display the original image
# and the waved image.

pic1 = Picture(sys.argv[1])

width  = pic1.width()
height = pic1.height()

pic2 = Picture(width, height)

# Apply the wave filter.
for col in range(width):
    for row in range(height):
        cc = col
        rr = int(row + 20.0 * math.sin(col * 2.0 * math.pi / 64.0))
        if (rr >= 0) and (rr < height):
            pic2.set(col, row, pic1.get(cc, rr))

stddraw.setCanvasSize(width, height)
stddraw.picture(pic2)
stddraw.show()
Exemplo n.º 9
0
#不知道理解是否正确,输出一个不包含白色的矩形,未完成
import sys
import luminance
import stdstats
import stddraw
from picture import Picture
from color import Color

#pic = Picture(sys.argv[1])
pic = Picture('me.jpg')
h = pic.height()
w = pic.width()
WEIGHT = str(Color(255,255,255))
tb = [[0,0] for i in range(h)]  #每行非白色起点
te = [[0,0] for i in range(h)]  #每行非白色终点

row = 0
col = 0
while row < pic.height():
    while col < pic.width():
        pixel = pic.get(col,row)
        if col-1 >= 0 and str(pic.get(col-1,row)) == WEIGHT and str(pixel) != WEIGHT: 
            tb[row] = [row,col]
            while str(pixel) != WEIGHT:
                pixel = pic.get(col,row)
                col += 1
            te[row] = [row,col]
        col += 10
    row += 10
    col = 0
Exemplo n.º 10
0
import sys
import luminance
import stdstats
import stddraw
from picture import Picture

pic = Picture(sys.argv[1])

freq = [0 for i in range(16)]  #0-255,每16为一个区间,因为256/16=16
norm = [0 for i in range(16)]  #不只用freq是为了除以总数,控制图大小

for col in range(pic.width()):
    for row in range(pic.height()):
        pixel = pic.get(col, row)
        lum = int(round(luminance.luminance(pixel)))
        freq[lum // 16] += 1

for i in range(16):
    norm[i] = freq[i] / (pic.width() * pic.height())

stdstats.plotBars(norm)
stddraw.show()
import sys
import stddraw
import luminance
from picture import Picture

source = Picture(sys.argv[1])
target = Picture(source.width(), source.height())
for col in range(source.width()):
    for row in range(source.height()):
        target.set(col, source.height()-row-1, source.get(col, row))

stddraw.setCanvasSize(target.width(), target.height())
stddraw.picture(target)
stddraw.show()
Exemplo n.º 12
0
#
# I Wayan Sudiarta
# Fisika, FMIPA, Universitas Mataram
# last updated: 14 Oktober 2018
#
# % python EMWave2D.py
#

import stddraw
import math
from picture import Picture
from color import Color

MAX_GRAY_SCALE = 255
pic = Picture(201, 201)
stddraw.setCanvasSize(pic.width(), pic.height())
width = pic.width()
height = pic.height()
i, j = 0, 0
n = 0
dx = 0.04  # dx = dy
dt = 0.3 * dx  # untuk stabil dt < dx/2^1/2
dtdx = dt / dx

ex = [[0.0] * 201 for i in range(201)]
ey = [[0.0] * 201 for i in range(201)]
hz = [[0.0] * 201 for i in range(201)]

# Materi
NM = 2
media = [[0] * 201 for i in range(201)]
Exemplo n.º 13
0
import sys
import stddraw
import luminance
from picture import Picture

#-----------------------------------------------------------------------

# Accept the name of a JPG or PNG file as a command-line argument.
# Read an image from the file with that name. Create and show a
# gray scale version of that image.

pic = Picture(sys.argv[1])

for col in range(pic.width()):
    for row in range(pic.height()):
        pixel = pic.get(col, row)
        gray = luminance.toGray(pixel)
        pic.set(col, row, gray)

stddraw.setCanvasSize(pic.width(), pic.height())
stddraw.picture(pic)
stddraw.show()

#-----------------------------------------------------------------------

# python grayscale.py mandrill.jpg

# python grayscale mandrill.png

# python grayscale.py darwin.jpg
Exemplo n.º 14
0
import sys
import luminance
import stdstats
import stddraw
from picture import Picture

pic = Picture(sys.argv[1])
h = pic.height()
w = pic.width()
flipPic = Picture(h, w)

for col in range(pic.width()):
    for row in range(pic.height()):
        pixel = pic.get(col, row)
        flipPic.set(row, col, pixel)

stddraw.picture(flipPic)
stddraw.show()
Exemplo n.º 15
0
# Accept strings sourceFile and targetFile and integer frameCount
# as command-line arguments. Read images from sourceFile and targetFile.
# Then, over the course of frameCount frames, gradually replace the
# image from sourceFile with the image with the image from targetFile.
# Display to standard draw each intermediate image. The images in the
# files can be in either JPG or PNG formats.

sourceFile = sys.argv[1]
targetFile = sys.argv[2]
n = int(sys.argv[3])       # number of intermediate frames

source = Picture(sourceFile)
target = Picture(targetFile)

width = source.width()
height = source.height()

stddraw.setCanvasSize(width, height)

pic = Picture(width, height)

for t in range(n+1):
    for col in range(width):
        for row in range(height):
            c0 = source.get(col, row)
            cn = target.get(col, row)
            alpha = float(t) / float(n)
            c = blend(c0, cn, alpha)
            pic.set(col, row, c)
    stddraw.picture(pic)
    stddraw.show(1000.0)