|
| 1 | +### Obtaining and Using contextfreeart ### |
| 2 | + |
| 3 | +For debian users it should be as simple as `sudo apt-get install cfdg` installs commandline version. |
| 4 | + |
| 5 | +For others check [contextfreeart website][download], if you get the command line version you could easily automate running the cfdg program, once the files have been created. |
| 6 | + |
| 7 | +### Black and White Demo |
| 8 | + |
| 9 | +```ruby |
| 10 | +#!/usr/bin/env jruby -v -W2 |
| 11 | +# frozen_string_literal: true |
| 12 | +require 'picrate' |
| 13 | +# Creates files that you can use with context free art (Gray Scale) |
| 14 | +class GrayScale < Processing::App |
| 15 | + load_library :file_chooser |
| 16 | + attr_reader :img, :data, :skip, :invert |
| 17 | + |
| 18 | + def settings |
| 19 | + size 500, 500 |
| 20 | + end |
| 21 | + |
| 22 | + def setup |
| 23 | + sketch_title 'Pixellator for CheChe' |
| 24 | + color_mode(HSB, 360, 1.0, 1.0) |
| 25 | + fill 0, 0, 200 |
| 26 | + text('Click Window to Load Image', 10, 100) |
| 27 | + @skip = 5 # controls apparent resolution |
| 28 | + @data = [] |
| 29 | + @invert = true |
| 30 | + end |
| 31 | + |
| 32 | + def draw |
| 33 | + unless img.nil? |
| 34 | + img.filter(INVERT) if invert |
| 35 | + @invert = false |
| 36 | + image(img, 0, 0) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def write_data(name, data) |
| 41 | + df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n" |
| 42 | + open(data_path('data.cfdg'), 'w') do |pw| |
| 43 | + pw.puts format("shape %s{\n", name) |
| 44 | + data.each do |row| |
| 45 | + pw.puts format(df, *row) |
| 46 | + end |
| 47 | + pw.puts "}\n" |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def write_start(start, data) |
| 52 | + open(data_path(format('%s.cfdg', start)), 'w') do |pw| |
| 53 | + pw.puts 'CF::Background = [b 1]' |
| 54 | + pw.puts format("startshape %s\n", start) |
| 55 | + pw.puts "shape dot{CIRCLE[]}\n" |
| 56 | + pw.puts "import data.cfdg\n" |
| 57 | + end |
| 58 | + write_data start, data |
| 59 | + end |
| 60 | + |
| 61 | + def file_selected(selection) |
| 62 | + if selection.nil? |
| 63 | + puts 'Nothing Chosen' |
| 64 | + else |
| 65 | + @img = load_image(selection.get_absolute_path) |
| 66 | + surface.set_size(img.width, img.height) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def mouse_clicked |
| 71 | + @img = nil |
| 72 | + # java_signature 'void selectInput(String, String)' |
| 73 | + select_input('Select Image File', 'file_selected') |
| 74 | + end |
| 75 | + |
| 76 | + def key_pressed |
| 77 | + case key |
| 78 | + when 'p', 'P' |
| 79 | + export = Thread.new do |
| 80 | + pixellate |
| 81 | + end |
| 82 | + export.join |
| 83 | + puts 'done' |
| 84 | + when 's', 'S' |
| 85 | + save_frame(data_path('original.png')) |
| 86 | + else |
| 87 | + puts format('key %s was pressed', key) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def pixellate |
| 92 | + load_pixels |
| 93 | + shp = 'dot' |
| 94 | + (skip...img.width).step(skip) do |x| |
| 95 | + (skip...img.height).step(skip) do |y| |
| 96 | + pix = pixels[x + y * width] |
| 97 | + sz = brightness(pix) * skip |
| 98 | + data << [ |
| 99 | + shp, -width / 2 + x, height / 2 - y, sz.round(2) |
| 100 | + ] if sz > 0.4 |
| 101 | + end |
| 102 | + end |
| 103 | + write_start 'cheche', data |
| 104 | + end |
| 105 | +end |
| 106 | + |
| 107 | +GrayScale.new |
| 108 | +``` |
| 109 | + |
| 110 | +If for example the chosen sketch was 590 * 600 pixels you might use the following to generate the pixellated image using the `cli` assuming `haddock.cfdg` is in the local folder. Windows users might need to use different escapes for path, but then they've got a GUI to use if they want. |
| 111 | + |
| 112 | +```bash |
| 113 | +cfdg haddock.cfdg -w 1180 -h 1200 -o phil.png |
| 114 | +``` |
| 115 | + |
| 116 | +But there is much more you can do (look up che che for a much more sophisticated cfdg file). |
| 117 | + |
| 118 | + |
| 119 | +NB: You could add a grayscale filter if you start with a coloured image. Here we set hue to 0 ie red colour 20 is yellow etc, read more from contextfreeart tutorial (plus have a go at creating a standalone cfdg sketch it is a lot of fun). |
| 120 | + |
| 121 | + |
| 122 | +[download]:http://www.contextfreeart.org/mediawiki/index.php/Download_page |
0 commit comments