import java.io.* import java.awt.* import java.awt.geom.* import java.awt.image.* import javax.imageio.* // 画像の左右をモノトーンにする def img = ImageIO.read(new File("sf2.jpg")) def wr = img.getRaster() def img2 = new BufferedImage( img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB ) def wr2 = img2.getRaster() def img3 = new BufferedImage( img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB ) def gr = img3.createGraphics() gr.drawImage(img, 0, 0, null) gr.dispose() // 半透明切取 def buf = new int[wr.getNumDataElements()] def buf2 = new int[4] for(int ly=0;ly<wr.getHeight();ly++){ for(int lx=0;lx<wr.getWidth();lx++){ wr.getPixel(lx, ly, buf) buf2[0] = buf2[1] = buf2[2] = 0 buf2[3] = Math.sin(Math.PI*lx/img.getWidth())*255 wr2.setPixel(lx, ly, buf2) } } gr = img2.createGraphics() gr.setComposite(AlphaComposite.SrcIn) gr.drawImage(img,0,0,null) gr.dispose() // モノトーン画像作成 def wr3 = img3.getRaster() def buf3 = new int[4] def color1 = 0x7799dd def red = (color1 >> 16) & 0xff def green = (color1 >> 8) & 0xff def blue = color1 & 0xff for(int ly=0;ly<wr.getHeight();ly++){ for(int lx=0;lx<wr.getWidth();lx++){ wr3.getPixel(lx, ly, buf3) def mono = (buf3[0] * 77 + buf3[1] * 151 + buf3[2] * 28) >> 8; buf3[0] = (int)(mono * red / 255) buf3[1] = (int)(mono * green / 255) buf3[2] = (int)(mono * blue / 255) wr3.setPixel(lx, ly, buf3) } } // 重ね合わせ gr = img3.createGraphics() gr.drawImage(img2, 0, 0, null) gr.dispose() ImageIO.write(img3, "png", new File("monotoneleftright.png"))
元画像
出力画像
動作環境
groovy 1.8.6, JDK7 update4
No comments:
Post a Comment