forked from sophsaviors/HalftoneGlyph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalftoneGlyph.pde
70 lines (64 loc) · 2.35 KB
/
HalftoneGlyph.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*****************************
* HALFTONE GLYPH
* Created by: Sofia Salvatori
* License: GPL-3.0
******************************/
/*
Configuration
*/
String imputImage = "demo.png"; // image to covert
String outputImage = "output-demo.png"; // name of the converted image
String fontName = "Arial"; // font used
String gliphs = "inserthereyourtext"; // characters to draw the image
int tile = 20; // size (pixel) of the biggest character
boolean whiteOnBlack = true; // true: white characters on black background
// false: black characters on white background
PFont myFont = createFont(fontName, tile);
PImage img = loadImage(imputImage);
// convert the image in grayscale to increase the contrast
img.filter(GRAY);
// decrease the pixel amount to have one pixel per tile
img.resize(img.width/tile, img.height/tile);
// create the new image whose size is rounded down
PGraphics pg = createGraphics(img.width*tile, img.height*tile);
pg.beginDraw();
// set the color of the characters and the background
if (whiteOnBlack == true) {
pg.background(0);
pg.fill(255);
} else {
pg.background(255);
pg.fill(0);
}
int gliphPosition = 0;
// for every x, y pixel of the downsized image
for (int y=0; y<img.height; y+=1) {
for (int x=0; x<img.width; x+=1) {
// get the color of the pixel in position x, y
color pixelColor = img.get(x, y);
// get the brightness (0-255) of the pixel
float value = brightness(pixelColor);
int gliphSize;
//and map it to the size of the character
if (whiteOnBlack == true) {
// the white character is bigger when the brightness is higher
gliphSize = round(map(value, 0, 255, 0, tile));
} else {
// the black character is bigger when the brightness is lower
gliphSize = round(map(value, 255, 0, 0, tile));
}
// configure the character font, size and position it inside the tile
pg.textAlign(LEFT, TOP);
pg.textFont(myFont, gliphSize);
// if all the characters of the string have been used, start over
if (gliphPosition == gliphs.length()) {
gliphPosition=0;
}
// get one character of the string and draw it in a tile
char carattere = gliphs.charAt(gliphPosition);
pg.text(carattere, x*tile, y*tile);
gliphPosition +=1;
}
}
pg.endDraw();
pg.save(outputImage);