Skip to content

Commit

Permalink
gif counter: rendertextoutlines
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyongzhi committed Aug 23, 2020
1 parent 8fdafba commit ff6d046
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "WordCloud"
uuid = "6385f0a0-cb03-45b6-9089-4e0acc74b26b"
authors = ["guoyongzhi <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ wordcloud in Julia
* [x] 引入动量加速训练
* [x] 分代调整以优化性能
* [x] 控制字体大小和填充密度的策略
* [ ] 重新放置、旋转和缩放的策略
* [x] 重新放置、旋转和缩放的策略
* [x] 文字颜色和方向
* [ ] 并行计算

Expand All @@ -22,7 +22,7 @@ texts = [string(c) for c in texts];
weights = rand(length(texts)) .^ 2 .* 100 .+ 30;
wc = wordcloud(texts, weights, filling_rate=0.45)
generate(wc)
paint(wc)
paint(wc, "wordcloud.png")
```

# 训练过程
Expand Down
23 changes: 14 additions & 9 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ end
## kargs example
### style kargs
colors = "black" #all same color
colors = ("black", (0.5,0.5,0.7), "yellow", "#ff0000", 0.2) #choose randomly
colors = ["black", (0.5,0.5,0.7), "yellow", "red", (0.5,0.5,0.7), 0.2, ......] #use sequentially in cycle
colors = ("black", (0.5,0.5,0.7), "yellow", "#ff0000", 0.2) #choose entries randomly
colors = ["black", (0.5,0.5,0.7), "yellow", "red", (0.5,0.5,0.7), 0.2, ......] #use entries sequentially in cycle
angles = 0 #all same angle
angles = (0, 90, 45) #choose randomly
angles = 0:180 #choose randomly
angles = [0, 22, 4, 1, 100, 10, ......] #use sequentially in cycle
angles = (0, 90, 45) #choose entries randomly
angles = 0:180 #choose entries randomly
angles = [0, 22, 4, 1, 100, 10, ......] #use entries sequentially in cycle
filling_rate = 0.5
border = 1
### mask kargs
Expand Down Expand Up @@ -99,7 +99,11 @@ function wordcloud(texts::AbstractVector{<:AbstractString}, weights::AbstractVec
maskcolor = "white"
try
# maskcolor = RGB(1,1,1) - RGB(sum(colors_o)/length(colors_o)) #补色
maskcolor = sum(Gray.(parsecolor.(colors_o)))/length(colors_o)<0.7 ? "white" : "black" #黑白
if sum(Gray.(parsecolor.(colors_o)))/length(colors_o)<0.7 #黑白
maskcolor = rand((1.0, (rand(0.9:0.01:1.0), rand(0.9:0.01:1.0), rand(0.9:0.01:1.0))))
else
maskcolor = rand((0.0, (rand(0.0:0.01:0.1), rand(0.0:0.01:0.1), rand(0.0:0.01:0.1))))
end
# @show sum(colors_o)/length(colors_o)
catch
@show "colors sum failed",colors_o
Expand Down Expand Up @@ -153,9 +157,10 @@ function paint(wc::wordcloud, file)
ImageMagick.save(file, paint(wc))
end

function record(wc::wordcloud, ep::Number, gif_callback)
resultpic = overlay!(paint(wc), rendertext(string(ep), 32, color="black"), 10, 10)
resultpic = overlay!(resultpic, rendertext(string(ep), 35, color="white"), 10, 10)
function record(wc::wordcloud, ep::Number, gif_callback=x->x)
# @show size(n1)
resultpic = overlay!(paint(wc),
rendertextoutlines(string(ep), 32, color="black", linecolor="white", linewidth=1), 20, 20)
gif_callback(resultpic)
end

Expand Down
34 changes: 28 additions & 6 deletions src/rendering.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Render
export rendertext, textmask, overlay!, shape, ellipse, box, GIF, generate, parsecolor
export rendertext, textmask, overlay!, shape, ellipse, box, GIF, generate, parsecolor, rendertextoutlines

using Luxor
using Colors
Expand All @@ -13,19 +13,19 @@ parsecolor(gray::Real) = Gray(gray)
function backgroundclip(p::AbstractMatrix, bgcolor; border=0)
a = c = 1
b = d = 0
while all(p[a,:] .== bgcolor) && a < size(p, 1)
while a < size(p, 1) && all(p[a,:] .== bgcolor)
a += 1
end
while all(p[end-b,:] .== bgcolor) && b < size(p, 1)
while b < size(p, 1) && all(p[end-b,:] .== bgcolor)
b += 1
end
a = max(1, a-border)
b = min(size(p, 1), max(size(p, 1)-b+border, a))
p = p[a:b, :]
while all(p[:,c] .== bgcolor) && c < size(p, 2)
while c < size(p, 2) && all(p[:,c] .== bgcolor)
c += 1
end
while all(p[:, end-d] .== bgcolor) && d < size(p, 2)
while d < size(p, 2) && all(p[:, end-d] .== bgcolor)
d += 1
end
# @show a,b,c,d,border,bgcolor
Expand Down Expand Up @@ -55,7 +55,29 @@ function rendertext(str::AbstractString, size::Real; color="black", bgcolor=(0,0
return mat
end
end


function rendertextoutlines(str::AbstractString, size::Real; color="black", bgcolor=(0,0,0,0),
linewidth=3, linecolor="white", font="")
l = length(str)
Drawing(ceil(Int, 2l*(size + 2linewidth) + 2), ceil(Int, 2*(size + 2linewidth) + 2), :image)
origin()
bgcolor = parsecolor(bgcolor)
bgcolor = background(bgcolor)
bgcolor = Luxor.ARGB32(bgcolor...)
setcolor(parsecolor(color))
# setfont(font, size)
fontface(font)
fontsize(size)
setline(linewidth)
textoutlines(str, O, :path, halign="center", valign="center")
fillpreserve()
setcolor(linecolor)
strokepath()
mat = image_as_matrix()
finish()
mat = backgroundclip(mat, mat[1])
end

function dilate(mat, r)
mat2 = copy(mat)
mat2[1:end-r, :] .|= mat[1+r:end, :]
Expand Down

0 comments on commit ff6d046

Please sign in to comment.