def scale(source, w, h): 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)) return target
def read(): w = stdio.readInt() h = stdio.readInt() p = Picture(w, h) for col in range(w): for raw in range(h): r = stdio.readInt() g = stdio.readInt() b = stdio.readInt() c = Color(r, g, b) p.set(col, raw, c) return p
def slideOne(source,target,n,w,h): pic = Picture(w,h) for t in range(n): for col in range(w): for row in range(h): 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(2000.0/n) #2秒一张为2000/n
def slideShow(p,n,w,h): l = len(p) #创建黑色图像 blackP = Picture(w,h) for col in range(w): for row in range(h): blackP.set(col,row,color.BLACK) for i in range(l-1): slideOne(p[i],blackP,n,w,h) slideOne(blackP,p[i+1],n,w,h) stddraw.picture(p[l-1]) stddraw.show()
#原版 #原版错误原因在于题中写明初始值收敛与4个根之一,而此处coloring函数的i的范围(0-200),j的范围(0-200) #正确的coloring范围为x(-1,1),y(-1,1) ''' #n = eval(sys.argv[1]) n = 200 p = Picture(n,n) for i in range(n): for j in range(n): p.set(i,j,coloring(i,j)) ''' #根据JAVA版改编 #n = eval(sys.argv[1]) xmin = -1.0 ymin = -1.0 width = 2.0 height = 2.0 n = 200 p = Picture(n, n) for i in range(n): for j in range(n): x = xmin + i * width / n y = ymin + j * height / n p.set(i, j, coloring(x, y)) stddraw.picture(p) stddraw.show()
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()
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) pic.set(col, pic.height()-1-row, color) # Draw the Picture. stddraw.setCanvasSize(pic.width(), pic.height()) stddraw.picture(pic) stddraw.show() #----------------------------------------------------------------------- # python potential.py < charges.txt
#从命令行接收参数,旋转图片 #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()
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()
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()): pic.set(col, pic.height()-row-1, pic.get(col, row)) stddraw.setCanvasSize(pic.width(), pic.height()) stddraw.picture(pic) stddraw.show()
#print('end:',z) for i in range(len(l)): if l[i] == z: return c[i] return Color(0, 0, 0) #原版 #原版错误原因在于题中写明初始值收敛与4个根之一,而此处coloring函数的i的范围(0-200),j的范围(0-200) #正确的coloring范围为x(-1,1),y(-1,1) #n = eval(sys.argv[1]) n = 200 p = Picture(n, n) for i in range(n): for j in range(n): p.set(i, j, coloring(i, j)) ''' #根据JAVA版改编 #n = eval(sys.argv[1]) xmin = -1.0 ymin = -1.0 width = 2.0 height = 2.0 n = 200 p = Picture(n,n) for i in range(n): for j in range(n): x = xmin + i * width / n y = ymin + j * height / n p.set(i,j,coloring(x,y))
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) stddraw.show() #----------------------------------------------------------------------- # python fade.py mandrill.jpg darwin.jpg 7 # python fade.py mandrill.jpg darwin.jpg 5 # python fade.py mandrill.jpg darwin.png 7 # python fade.py mandrill.png darwin.jpg 7
import sys import luminance import stdstats import stddraw from picture import Picture from color import Color pic = Picture(sys.argv[1]) h = pic.height() w = pic.width() redPic = Picture(w, h) greenPic = Picture(w, h) bluePic = Picture(w, h) for col in range(pic.width()): for row in range(pic.height()): pixel = pic.get(col, row) redPic.set(col, row, Color(pixel.getRed(), 0, 0)) greenPic.set(col, row, Color(0, pixel.getRed(), 0)) bluePic.set(col, row, Color(0, 0, pixel.getRed())) #stddraw.setXscale(-10,10000) #stddraw.setYscale(-10,10000) stddraw.picture(redPic) stddraw.show(1000) stddraw.picture(greenPic) stddraw.show(1000) stddraw.picture(bluePic) stddraw.show()
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()): pic.set(col, row, pic.get(col, row)) stddraw.setCanvasSize(pic.width(), pic.height()) stddraw.picture(pic) stddraw.show()
# iterations before the Mandelbrot sequence for the corresponding # complex number grows past 2.0, up to 255. MAX = 255 n = int(sys.argv[1]) xc = float(sys.argv[2]) yc = float(sys.argv[3]) size = float(sys.argv[4]) pic = Picture(n, n) for col in range(n): for row in range(n): x0 = xc - (size / 2) + (size * col / n) y0 = yc - (size / 2) + (size * row / n) z0 = complex(x0, y0) gray = MAX - mandel(z0, MAX) color = Color(gray, gray, gray) pic.set(col, n-1-row, color) stddraw.setCanvasSize(n, n) stddraw.picture(pic) stddraw.show() #----------------------------------------------------------------------- # python mandelbrot.py 512 -.5 0 2 # python mandelbrot.py 512 .1015 -.633 .01
# Hitung Ex for i in range(1, 200): for j in range(1, 200): ex[i][j] = ce1[media[i][j]] * ex[i][j] + ce2[media[i][j]] * ( hz[i][j] - hz[i][j - 1]) # Hitung Ey for i in range(1, 200): for j in range(1, 200): ey[i][j] = ce1[media[i][j]] * ey[i][j] + ce2[media[i][j]] * ( hz[i - 1][j] - hz[i][j]) hz[100][100] += math.sin(omega * n * dt) if n % 20 == 0: stddraw.clear() for i in range(201): for j in range(201): v = (MAX_GRAY_SCALE / 2.0) + (500 * hz[i][j]) if v < 0: grayScale = 0 elif v > MAX_GRAY_SCALE: grayScale = MAX_GRAY_SCALE else: grayScale = int(v) color = Color(grayScale, grayScale, grayScale) pic.set(i, j, color) stddraw.picture(pic) stddraw.show(20)
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()
p = 'me.jpg' source = Picture(p) #压缩或放大图片到w*h def scale(source, w, h): 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)) return target #缩小后的图片 scPicture = scale(source, w, h) #大图片,也就是m*n的小图片的集合 TaPicture = Picture(w * m, n * h) for i in range(m): for j in range(n): for col in range(w): for row in range(h): TaPicture.set(i * w + col, j * h + row, scPicture.get(col, row)) stddraw.picture(TaPicture) stddraw.show()
from color import Color import stddraw import sys from picture import Picture r = eval(sys.argv[1]) g = eval(sys.argv[2]) b = eval(sys.argv[3]) c = Color(r, g, b) pic = Picture(256, 256) for col in range(256): for row in range(256): pic.set(col, row, c) stddraw.picture(pic) stddraw.show()
# 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() #----------------------------------------------------------------------- # python wave.py mandrill.jpg # python wave.py mandrill.png # python wave.py darwin.jpg # python wave.py darwin.png
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 # python grayscale.py darwin.png
# 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) pic.set(col, pic.height() - 1 - row, color) # Draw the Picture. stddraw.setCanvasSize(pic.width(), pic.height()) stddraw.picture(pic) stddraw.show() #----------------------------------------------------------------------- # python potential.py < charges.txt
# accomplished by plotting each pixel the color of a random # neighboring pixel. Display the original image and the new one. pic1 = Picture(sys.argv[1]) width = pic1.width() height = pic1.height() pic2 = Picture(width, height) for col in range(width): for row in range(height): cc = (width + col + stdrandom.uniformInt(-5, 6)) % width rr = (height + row + stdrandom.uniformInt(-5, 6)) % height c = pic1.get(cc, rr) pic2.set(col, row, c) stddraw.setCanvasSize(width, height) stddraw.picture(pic2) stddraw.show() #----------------------------------------------------------------------- # python glass.py mandrill.jpg # python glass.py mandrill.png # python glass.py darwin.jpg # python glass.py darwin.png
# 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
w = source.width() h = source.height() print(w,h) #目标图像 target = Picture(w,h) x0 = x-int(w*s/2) x1 = x+int(w*s/2) y0 = y-int(h*s/2) y1 = y+int(h*s/2) #中间图像 tw = x1-x0 th = y1-y0 mp = Picture(tw,th) for i in range(x0,x1): for j in range(y0,y1): mp.set(i-x0,j-y0,source.get(i,j)) for colT in range(w): for rowT in range(h): colS = colT * tw // w rowS = rowT * th // h target.set(colT,rowT,mp.get(colS,rowS)) stddraw.picture(target) stddraw.show()
MAX = 255 # n = int(sys.argv[1]) # xc = float(sys.argv[2]) # yc = float(sys.argv[3]) # size = float(sys.argv[4]) n = 512 xc = -.5 yc = 0 size = 2 pic = Picture(n, n) for col in range(n): for row in range(n): x0 = xc - (size / 2) + (size * col / n) y0 = yc - (size / 2) + (size * row / n) z0 = complex(x0, y0) gray = MAX - mandel(z0, MAX) color = Color(gray, gray, gray) pic.set(col, n - 1 - row, color) stddraw.setCanvasSize(n, n) stddraw.picture(pic) stddraw.show() #----------------------------------------------------------------------- # python mandelbrot.py 512 -.5 0 2 # python mandelbrot.py 512 .1015 -.633 .01
for i, r in enumerate(m): # generates the actual image. pizel coordinates are given by position in the array. # probably some room for optimization here #if all(map(lambda c: not c, r)): continue # stdio.write('\r{:02.2f}%'.format(100*float(i)/ny)) for j, c in enumerate(r): s = min(c*2, 255) #int(256*2*atan(c/10)/pi) # no idea if it still works, but this is the original text display code # from before i'd figured out graphics. I should add a text mode #stdio.writeln(s) #stdio.write('{:2d}'.format(c)) #stdio.write(('*' if c == 0 else ' ') + ' ') if c == -1: # the point is in the set pic.set(j, i, color.BLACK) elif c == 0: # outside the circle of radius 2 (maybe not if i changed the algorithm) pic.set(j, i, color.BLACK) else: # this genreates the color based on c, the number of iterations. # i just strung math functions together until it spit out a path # through hsv space that looked nice r, g, b = (int(255*i) for i in hsv_to_rgb( ((-sqrt(10*c)-90) % 100) / 100, .75 + .25*cos(c*pi/20), # 1 exp(c/2-1)/(exp(c/2-1)+10) )) pic.set(j, i, color.Color(r, g, b)) #stdio.writeln()
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: grayScale = int(v) color = Color(grayScale, grayScale, grayScale) p.set(col, p.height()-1-row, color) stddraw.clear() stddraw.picture(p) stddraw.show(0) a[1].increaseCharge(-2.0) #print(a[1]._q)
# Compute the swirl. for sCol in range(width): for sRow in range(height): dCol = float(sCol) - col0 dRow = float(sRow) - row0 r = math.sqrt(dCol * dCol + dRow * dRow) angle = math.pi / 256.0 * r tCol = int(dCol * math.cos(angle) - dRow * math.sin(angle) + col0) tRow = int(dCol * math.sin(angle) + dRow * math.cos(angle) + row0) # Plot pixel (sx, sy) the same color as (tx, ty) if it's # in bounds if (tCol >= 0) and (tCol < width) and \ (tRow >= 0) and (tRow < height): pic2.set(sCol, sRow, pic1.get(tCol, tRow)) stddraw.setCanvasSize(width, height) stddraw.picture(pic2) stddraw.show() #----------------------------------------------------------------------- # python swirl.py mandrill.jpg # python swirl.py mandrill.png # python swirl.py darwin.jpg # python swirl.py darwin.png