サンプルコード
# coding=UTF-8 import pygal chart = pygal.Pie(title=u"部門別売上") chart.add(u'部門1', 200) chart.add(u'部門2', 170) chart.add(u'部門3', 50) chart.render_to_file('chart13.svg') chart.render_to_png(filename='chart13.png')
出力画像

# coding=UTF-8 import pygal chart = pygal.Line(x_title=u'年度', y_title=u'百万', show_dots=False) chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart12.svg') chart.render_to_png(filename='chart12.png')
# coding=UTF-8 import pygal chart = pygal.Line(x_title=u'年度', y_title=u'百万', show_legend=False) chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart11.svg') chart.render_to_png(filename='chart11.png')
# coding=UTF-8 import pygal chart = pygal.Line(x_title=u'年度', y_title=u'百万', legend_at_bottom=True) chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart10.svg') chart.render_to_png(filename='chart10.png')
# coding=UTF-8 import pygal chart = pygal.Line(x_title=u'年度', y_title=u'百万', x_label_rotation=-45, y_label_rotation=-90) chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart9.svg') chart.render_to_png(filename='chart9.png')
# coding=UTF-8 import pygal chart = pygal.StackedLine(x_title=u'年度', y_title=u'百万', fill=True) chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart8.svg') chart.render_to_png(filename='chart8.png')
# coding=UTF-8 import pygal chart = pygal.Line(x_title=u'年度', y_title=u'百万') chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300],stroke_style={'width':7}) chart.add(u'部門2', [150, 170, 320],stroke_style={'width':5}) chart.add(u'部門3', [None, 50, 1000],stroke_style={'width':3}) chart.render_to_file('chart7.svg') chart.render_to_png(filename='chart7.png')
# coding=UTF-8 import pygal chart = pygal.Line(x_title=u'年度', y_title=u'百万') chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart6.svg') chart.render_to_png(filename='chart6.png')
# coding=UTF-8 import pygal chart = pygal.HorizontalBar(x_title=u'年度', y_title=u'百万') chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart5.svg') chart.render_to_png(filename='chart5.png')
# coding=UTF-8 import pygal chart = pygal.StackedBar(x_title=u'年度', y_title=u'百万') chart.x_labels = map(str, range(2013, 2016)) chart.add(u'部門1', [100, 200, 300]) chart.add(u'部門2', [150, 170, 320]) chart.add(u'部門3', [None, 50, 1000]) chart.render_to_file('chart4.svg') chart.render_to_png(filename='chart4.png')
# coding=UTF-8 import pygal chart = pygal.Bar(title=u'部門別売上', x_title=u'年度', y_title=u'百万') chart.x_labels = map(str, range(2013, 2016)) chart.add(u'サンプル1', [100, 200, 300]) chart.add(u'サンプル2', [150, 170, 320]) chart.render_to_file('chart3.svg') chart.render_to_png(filename='chart3.png')
# coding=UTF-8 import pygal chart = pygal.Bar() chart.add(u'サンプル1', [100, 200, 300]) chart.add(u'サンプル2', [150, 170, 320]) chart.render_to_file('chart1.svg') chart.render_to_png(filename='chart1.png')出力画像
// 0.3.10-RC以上が必要 // https://github.com/sarxos/webcam-capture/issues/34 //@Grab(group='com.github.sarxos', module='webcam-capture', version='0.3.9') import java.awt.* import java.awt.image.* import javax.imageio.* import com.github.sarxos.webcam.* def webcam = Webcam.getDefault() if( webcam == null )return println "Webcam: " + webcam.getName() webcam.setViewSize(new Dimension(640, 480)) def detector = new WebcamMotionDetector(webcam) detector.setInterval(500) // 500ms detector.start() while(true){ if( detector.isMotion() ){ println "detected motion..." BufferedImage image = webcam.getImage() ImageIO.write(image, "PNG", new File("motion.png")) return } Thread.sleep(500) }
@Grab(group='com.github.sarxos', module='webcam-capture', version='0.3.9') import java.awt.* import javax.swing.* import groovy.swing.* import com.github.sarxos.webcam.* import com.github.sarxos.webcam.WebcamPanel def webcam = Webcam.getDefault() if( webcam == null)return webcam.setViewSize(new Dimension(640, 480)) WebcamPanel pnl = new WebcamPanel(webcam) pnl.setFillArea(true) sb = new SwingBuilder() sb.edt { frame( title: "example - webcam capture", show: true, resizable: true, size: [640, 480], defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE ){ widget(pnl) } }
@Grab(group='com.github.sarxos', module='webcam-capture', version='0.3.9') import java.awt.* import java.awt.image.* import javax.imageio.* import com.github.sarxos.webcam.* def webcam = Webcam.getDefault() if( webcam == null )return println "Webcam: " + webcam.getName() webcam.setViewSize(new Dimension(640, 480)) webcam.open() BufferedImage image = webcam.getImage() ImageIO.write(image, "PNG", new File("test.png"))
@Grab(group='com.jhlabs', module='filters', version='2.0.235') import java.awt.* import java.awt.image.* import java.io.* import javax.imageio.* import com.jhlabs.composite.* import com.jhlabs.image.* // 画像を万華鏡のように変換する def img = ImageIO.read(new File("sf2.jpg")) KaleidoscopeFilter kf = new KaleidoscopeFilter() // 面数 kf.setSides(7) // 角度1 kf.setAngle((float)40f/180f*ImageMath.PI) // 角度2 kf.setAngle2((float)10f/180f*ImageMath.PI) // 中心X kf.setCentreX(0.5f) // 中心Y kf.setCentreY(0.5f) // 半径 kf.setRadius((int)(img.getWidth()>img.getHeight()?img.getWidth()/2:img.getHeight()/2)) img = kf.filter(img, null) ImageIO.write(img, "png", new File("kaleidoscope.png"))
@Grab(group='com.jhlabs', module='filters', version='2.0.235') import java.io.* import java.awt.* import java.awt.geom.* import java.awt.image.* import javax.imageio.* import com.jhlabs.image.* // チェック柄の布の模様を生成する def img = new BufferedImage( 200, 200, BufferedImage.TYPE_INT_ARGB ) def rectsize = 20 Color color1 = new Color((int)0x588F27) Color color1l = color1.darker() Color color2 = new Color((int)0xBF372A) Color color2l = color2.darker() def gr = img.createGraphics() for(int ly=0;ly<img.getHeight();ly+=rectsize){ def cc=((int)ly/rectsize)%2==0?0:1 for(int lx=0;lx<img.getWidth();lx+=rectsize){ if( cc%2 == 0 ){ for(int py=0;py<=rectsize;py+=2){ gr.setColor(color1) gr.drawLine(lx, ly+py, lx+rectsize, ly+py) gr.setColor(color1l) gr.drawLine(lx, ly+py+1, lx+rectsize, ly+py+1) } } else { for(int px=0;px<=rectsize;px+=2){ gr.setColor(color2) gr.drawLine(lx+px, ly, lx+px, ly+rectsize) gr.setColor(color2l) gr.drawLine(lx+px+1, ly, lx+px+1, ly+rectsize) } } cc++ } } def sf = new SmearFilter() sf.setShape(SmearFilter.CROSSES) sf.setDensity(0.9f) sf.setDistance(7) sf.setScatter(0.8f) //sf.setAngle((float)30*ImageMath.PI/180) img = sf.filter(img, null) ImageIO.write(img, "png", new File("checkerfabric.png"))
@Grab(group='com.jhlabs', module='filters', version='2.0.235') import java.awt.* import java.awt.image.* import java.io.* import javax.imageio.* import com.jhlabs.composite.* import com.jhlabs.image.* // 写真を夕暮れ風に加工する def img = ImageIO.read(new File("sf2.jpg")) def imga = new BufferedImage( img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB ) def gr = imga.createGraphics() gr.drawImage(img,0,0,null) gr.dispose() // 閾値で2色画像を作成 def threshold = 127 def white = (int)0x50F24405 def black = (int)0x503E606F def wr = imga.getRaster() def buf = new int[4] for(int ly=0;ly<wr.getHeight();ly++){ for(int lx=0;lx<wr.getWidth();lx++){ wr.getPixel(lx, ly, buf) if( PixelUtils.brightness((buf[0]<<16)+(buf[1]<<8)+buf[2]) > threshold ){ buf[0] = (white >> 16) & 0xff buf[1] = (white >> 8) & 0xff buf[2] = white & 0xff buf[3] = (white >> 24) & 0xff } else { buf[0] = (black >> 16) & 0xff buf[1] = (black >> 8) & 0xff buf[2] = black & 0xff buf[3] = (black >> 24) & 0xff } wr.setPixel(lx, ly, buf) } } // ぼかす def gf = new GaussianFilter() gf.setRadius(3f) imga = gf.filter(imga, null) // 重ね合わせ def gr2 = img.createGraphics() gr2.drawImage(imga,0,0,null) gr2.dispose() ImageIO.write(img, "png", new File("sunset.png"))