forked from timoschwarzer/tunic-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImgUtil.gd
53 lines (38 loc) · 1.23 KB
/
ImgUtil.gd
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
extends Node
func texture_from(image: Image):
var texture = ImageTexture.new()
texture.create_from_image(image)
return texture
func calculate_threshold(src: Image, threshold: float = 0.5, invert = false):
var dst = Image.new()
dst.create(src.get_width(), src.get_height(), false, Image.FORMAT_L8)
src.lock()
dst.lock()
var foreground = Color.black if invert else Color.white
var background = Color.white if invert else Color.black
for x in range(src.get_width()):
for y in range(src.get_height()):
var pixel = src.get_pixel(x, y)
dst.set_pixel(x, y, foreground if pixel.v > threshold else background)
src.unlock()
dst.unlock()
return dst
func get_value_at(img: Image, pos: Vector2, neighbors = 2):
var values = PoolRealArray()
var x = pos.x
var y = pos.y
img.lock()
for x_offset in range(-neighbors, neighbors):
for y_offset in range(-neighbors, neighbors):
var coords = Vector2(x + x_offset, y + y_offset)
var size = img.get_size()
if coords.x < 0 || coords.y < 0 || coords.x >= size.x || coords.y >= size.y:
continue
values.append(img.get_pixelv(coords).v)
img.unlock()
if values.size() == 0:
return 0
var total = 0.0
for value in values:
total += value
return total / values.size()