# 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
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()
# 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
#从命令行接收参数,旋转图片 #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()
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 temp1 = 0 temp2 = 0 for i in range(h): if temp1 < tb[i][0]: temp1 = tb[i][0]
# like the image is being seen through glass. This effect is # 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
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()
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
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()
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) stddraw.show() #----------------------------------------------------------------------- # python fade.py mandrill.jpg darwin.jpg 7 # python fade.py mandrill.jpg darwin.jpg 5
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()
# 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