Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Z
zhichan
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
蒋勇
zhichan
Commits
a543bf4a
Commit
a543bf4a
authored
May 11, 2020
by
高宇强
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gyq
parent
b777802c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
130 additions
and
12 deletions
+130
-12
ailogo/LogoManage.py
+4
-2
ailogo/LogoTemplatesmall.py
+58
-0
ailogo/customizelogosmall.py
+68
-10
No files found.
ailogo/LogoManage.py
View file @
a543bf4a
...
...
@@ -135,7 +135,7 @@ def SaveSingleLogo(inputdata):
json_str
=
LogoGenerator
.
gen_main
(
gen_json
)
else
:
#因目前只有八个模板,且是画的,需要把数据库的模板id对应到1-8的模板上
model_id
=
1
model_id
=
0
if
inputdata
[
"logo_id"
]
>
76
and
inputdata
[
"logo_id"
]
<
83
:
model_id
=
inputdata
[
"logo_id"
]
-
76
elif
inputdata
[
"logo_id"
]
>
84
and
inputdata
[
"logo_id"
]
<
87
:
...
...
@@ -151,7 +151,7 @@ def SaveSingleLogo(inputdata):
'slogan_font'
:
inputdata
[
"font_url"
],
# 字体路径
'slogan_color'
:
inputdata
[
'sec_color'
],
# 标语颜色
'logo_url'
:
inputdata
[
'log_url'
],
# 图片地址
'model_id'
:
model_id
# 目前是数据库id减76,用于满足1-8的要求
'model_id'
:
model_id
# 目前是数据库id减76,用于满足1-8的要求
,其他计算的id都传0
}
json_str
=
customizelogosmall
.
gen_main
(
gen_json
)
if
json_str
[
"status"
]:
...
...
@@ -204,6 +204,8 @@ def ChangeABatch(inputdata):
select_sql
=
"select * from logo_res where msg_id =
%
s and (pic_url is not null and pic_url <> '') "
%
mgs_id
cur
.
execute
(
select_sql
)
data
=
cur
.
fetchall
()
if
len1
>
len
(
data
):
len1
=
len
(
data
)
random_list
=
random
.
sample
(
data
,
len1
)
for
dl
in
random_list
:
dict1
=
{
...
...
ailogo/LogoTemplatesmall.py
0 → 100644
View file @
a543bf4a
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020-05-11 15:12
# @Author : zhangyunfei
# @File : LogoTemplatesmall.py
# @Software: PyCharm
from
PIL
import
Image
,
ImageDraw
class
tenplateGenerator
():
def
__init__
(
self
):
self
.
width
=
380
self
.
height
=
280
# 品牌语和标语生成函数,该函数是品牌语和标语都存在的情况下使用
def
generator
(
self
,
brand_text
,
brand_font
,
brand_color
,
location
,
slogan_text
,
slogan_font
,
slogan_color
,
logo_img
):
# 生成空白logo图像,指定背景颜色和大小
logo_gen_image
=
Image
.
new
(
'RGB'
,
(
self
.
width
,
self
.
height
),
(
255
,
255
,
255
))
# logo_gen_image = Image.open("data/alpha.png")
# 定义画笔
draw
=
ImageDraw
.
Draw
(
logo_img
)
# 获取品牌语font的大小
brand_width
,
brand_height
=
brand_font
.
getsize
(
brand_text
)
# print('品牌语字体大小:', brand_width, brand_height)
# 获取标语font的大小
slogan_width
,
slogan_height
=
slogan_font
.
getsize
(
slogan_text
)
# print('标语字体大小:', slogan_width, slogan_height)
#空白区域的大小为
location_width
=
location
[
2
]
-
location
[
0
]
location_height
=
location
[
3
]
-
location
[
1
]
# 中心点坐标
center_x
=
int
((
location
[
0
]
+
location
[
2
])
/
2
)
center_y
=
int
((
location
[
1
]
+
location
[
3
])
/
2
)
#生成文字的总长度和宽度为
text_width
=
max
(
brand_width
,
slogan_width
)
text_height
=
brand_height
+
slogan_height
# print('location----------:', location_width, location_height)
# print('text------------:', text_width, text_height)
if
text_width
<
location_width
and
text_height
<
location_height
:
brand_x
=
center_x
-
int
(
brand_width
/
2
)
brand_y
=
center_y
-
int
((
brand_height
+
slogan_height
)
/
2
)
# print('品牌语开始位置:', brand_x, brand_y)
slogan_x
=
center_x
-
int
(
slogan_width
/
2
)
slogan_y
=
brand_y
+
brand_height
# print('标语开始位置:', slogan_x, slogan_y)
draw
.
text
((
brand_x
,
brand_y
),
brand_text
,
brand_color
,
brand_font
)
draw
.
text
((
slogan_x
,
slogan_y
),
slogan_text
,
slogan_color
,
slogan_font
)
scale
=
1.2
scale_width
,
scale_height
=
int
(
380
/
scale
),
int
(
280
/
scale
)
logo_img_resize
=
logo_img
.
resize
((
scale_width
,
scale_height
),
Image
.
ANTIALIAS
)
# logo_gen_image.paste(logo_img, (0, 0))
logo_gen_image
.
paste
(
logo_img_resize
,
(
190
-
int
(
scale_width
/
2
),
140
-
int
(
scale_height
/
2
)))
return
logo_gen_image
else
:
return
None
ailogo/customizelogosmall.py
View file @
a543bf4a
...
...
@@ -7,8 +7,8 @@
import
os
,
random
,
time
,
oss2
from
PIL
import
Image
,
ImageDraw
,
ImageFont
from
LogoUtil
import
put2oss
,
add_watermark
from
LogoUtil
import
put2oss
,
add_watermark
,
alpha2white
,
downlogo_path
,
downlogo
from
LogoTemplatesmall
import
tenplateGenerator
'''
实现自定义模板生成logo
'''
...
...
@@ -434,18 +434,18 @@ def gen_main(gen_json):
slogan_size
=
12
slogan_font
=
ImageFont
.
truetype
(
"font/cn/微软vista雅黑.ttf"
,
slogan_size
)
gen_img
=
vertical_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
2
:
# 实现品牌语被矩形框包围
el
if
customize_model_id
==
2
:
# 实现品牌语被矩形框包围
gen_img
=
rectangle_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
3
:
# 实现logo被矩形颜色框包围(涉及半透明图像alpha通道)
el
if
customize_model_id
==
3
:
# 实现logo被矩形颜色框包围(涉及半透明图像alpha通道)
gen_img
=
rectanglecolor_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
4
:
# 实现对角线对角样式
el
if
customize_model_id
==
4
:
# 实现对角线对角样式
gen_img
=
cornerline_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
5
:
# 实现两个品字型的样式
el
if
customize_model_id
==
5
:
# 实现两个品字型的样式
gen_img
=
twopin_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
6
:
# # 实现线条半包围logo字体
el
if
customize_model_id
==
6
:
# # 实现线条半包围logo字体
gen_img
=
line_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
7
:
# 实现竖排字体加上帽子
el
if
customize_model_id
==
7
:
# 实现竖排字体加上帽子
if
len
(
brand_text
)
<
7
:
brand_size
=
42
# brand_font = ImageFont.truetype('font/cn/华文行楷.ttf', brand_size)
...
...
@@ -457,14 +457,72 @@ def gen_main(gen_json):
slogan_size
=
12
slogan_font
=
ImageFont
.
truetype
(
"font/cn/微软vista雅黑.ttf"
,
slogan_size
)
gen_img
=
verticalline_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
if
customize_model_id
==
8
:
# 实现一半文字被梯形包围
el
if
customize_model_id
==
8
:
# 实现一半文字被梯形包围
gen_img
=
polygon_right_template
(
brand_text
,
brand_font
,
brand_color
,
slogan_text
,
slogan_font
,
slogan_color
)
elif
customize_model_id
==
0
:
# 图标下载
logo_url
=
gen_json
[
'logo_url'
]
# logo名字
logo_name
=
time
.
strftime
(
'
%
Y
%
m
%
d
%
H
%
M
%
S'
)
+
str
(
random
.
randint
(
100
,
10000
))
+
'.png'
logo_down_path
=
'logodir/'
+
logo_name
logo_down_result
=
downlogo
(
logo_down_path
,
logo_url
)
# 线上读取logo样式方法
#logo_down_result = downlogo_path(logo_down_path, logo_url) # downlogo_path(logo_down_path, logo_url)
# 定义生成的图像接受对象
if
logo_down_result
:
lg_gen
=
tenplateGenerator
()
# 判断标语是否存在
# logo_img = Image.open('data/template/6.png')
logo_img
=
Image
.
open
(
logo_down_path
)
# location = [110, 93, 271, 187]
location
=
[
int
(
i
)
for
i
in
gen_json
[
'brand_location'
]
.
split
(
','
)]
brand_size
=
40
slogan_size
=
16
brand_font
=
ImageFont
.
truetype
(
gen_json
[
"brand_font"
],
brand_size
)
# brand_font = ImageFont.truetype('font/cn/汉仪秦川飞影.ttf', brand_size)
slogan_font
=
ImageFont
.
truetype
(
"font/cn/微软vista雅黑.ttf"
,
slogan_size
)
brand_width
,
brand_height
=
brand_font
.
getsize
(
brand_text
)
# 获取标语font的大小
slogan_width
,
slogan_height
=
slogan_font
.
getsize
(
slogan_text
)
# 空白区域的大小为
location_width
=
location
[
2
]
-
location
[
0
]
location_height
=
location
[
3
]
-
location
[
1
]
# 生成文字的总长度和宽度为
text_width
=
max
(
brand_width
,
slogan_width
)
text_height
=
brand_height
+
slogan_height
flag
=
text_width
<
location_width
and
text_height
<
location_height
while
not
flag
:
brand_size
-=
1
slogan_size
-=
1
if
slogan_size
<=
12
:
slogan_size
=
12
brand_font
=
ImageFont
.
truetype
(
gen_json
[
"brand_font"
],
brand_size
)
# brand_font = ImageFont.truetype('font/cn/汉仪秦川飞影.ttf', brand_size)
slogan_font
=
ImageFont
.
truetype
(
"font/cn/微软vista雅黑.ttf"
,
slogan_size
)
brand_width
,
brand_height
=
brand_font
.
getsize
(
brand_text
)
# 获取标语font的大小
slogan_width
,
slogan_height
=
slogan_font
.
getsize
(
slogan_text
)
# 空白区域的大小为
location_width
=
location
[
2
]
-
location
[
0
]
location_height
=
location
[
3
]
-
location
[
1
]
# 生成文字的总长度和宽度为
text_width
=
max
(
brand_width
,
slogan_width
)
text_height
=
brand_height
+
slogan_height
# print('location:', location_width, location_height)
# print('text:', text_width, text_height)
flag
=
text_width
<
location_width
and
text_height
<
location_height
# print(flag,'-----------------------------------------',brand_size,slogan_size)
gen_img
=
lg_gen
.
generator
(
brand_text
,
brand_font
,
brand_color
,
location
,
slogan_text
,
slogan_font
,
slogan_color
,
alpha2white
(
logo_img
))
# 调用水印和版权图标
gen_img_new
=
add_watermark
(
gen_img
,
'www.gongsibao.com'
)
gen_image_path
=
'generatordir/'
+
'generator_'
+
logo_name
gen_img_new
.
save
(
gen_image_path
)
#
gen_img_new.show()
gen_img_new
.
show
()
# 上传阿里云oss
mosaicurl
=
put2oss
(
'generator_'
+
logo_name
,
gen_image_path
)
return
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment