肖高铿 2021-12-24 广州市墨灵格信息科技有限公司
祝圣诞快乐!
导入OpenEye Toolkit工具包
import sys
sys.path.append('/public/gkxiao/software/OpenEye-toolkits-python3-linux-x64-2021.1.1')
import math
import random
import os
from openeye import oechem
from openeye import oedepict
设置图片规格
imagewidth, imageheight = 400, 500
image = oedepict.OEImage(imagewidth, imageheight)
画树干
trunk = []
treetop = oedepict.OE2DPoint(imagewidth / 2.0, 100.0)
trunk.append(oedepict.OE2DPoint(treetop.GetX(), treetop.GetY() + 40.0))
trunk.append(oedepict.OE2DPoint(treetop.GetX() - 10.0, treetop.GetY() + 330.0))
trunk.append(oedepict.OE2DPoint(treetop.GetX() + 10.0, treetop.GetY() + 330.0))
trunkpen = oedepict.OEPen(oechem.OEBrown, oechem.OEBrown, oedepict.OEFill_On, 1.0,
oedepict.OEStipple_NoLine)
image.DrawPolygon(trunk, trunkpen)
True
画树枝
branches = []
branches.append(treetop)
for i in range(1, 8):
p = oedepict.OE2DPoint(treetop.GetX() + i * 20.0, treetop.GetY() + i * 40.0)
branches.append(p)
branches.append(oedepict.OE2DPoint(p.GetX() - i * 7.0, p.GetY()))
p = oedepict.OE2DPoint(treetop.GetX() - i * 20.0, treetop.GetY() + i * 40.0)
branches.insert(0, p)
branches.insert(0, oedepict.OE2DPoint(p.GetX() + i * 7.0, p.GetY()))
branchcolor = oechem.OEColor(oechem.OEDarkGreen)
branchcolor.SetA(200)
branchpen = oedepict.OEPen(branchcolor, branchcolor, oedepict.OEFill_On, 1.0,
oedepict.OEStipple_NoLine)
image.DrawPolygon(branches, branchpen)
True
画颗我闺女最爱的星星,放在树顶
def Rotate2DPoint(p, degree):
rad = degree * oechem.Deg2Rad
cosrad = math.cos(rad)
sinrad = math.sin(rad)
r = oedepict.OE2DPoint(cosrad * p.GetX() - sinrad * p.GetY(),
sinrad * p.GetX() + cosrad * p.GetY())
return r
octo = []
for i in range(0, 5):
x = treetop.GetX() + 20.0 * math.cos(i * 2.0 * math.pi / 5.0)
y = treetop.GetY() + 20.0 * math.sin(i * 2.0 * math.pi / 5.0)
octo.append(oedepict.OE2DPoint(x, y))
star = [octo[0], octo[2], octo[4], octo[1], octo[3]]
starpen = oedepict.OEPen(oechem.OEYellow, oechem.OEYellow, oedepict.OEFill_On, 1.0,
oedepict.OEStipple_NoLine)
image.DrawPolygon(star, starpen)
font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default, 30,
oedepict.OEAlignment_Center, oechem.OEDarkRed)
center = treetop + oedepict.OE2DPoint(0.0, 100.0)
radius = 150.0
text = "Happy Christmas"
axis = oedepict.OE2DPoint(0.0, -radius)
angleinc = 120.0 / len(text)
angle = 300.0 + angleinc / 2.0
for letter in text:
pos = center + Rotate2DPoint(axis, angle)
font.SetRotationAngle((360 - angle) % 360)
image.DrawText(pos, letter, font)
angle += angleinc
def DrawPresent(image, boxpos, boxcolor):
boxpen = oedepict.OEPen(boxcolor, boxcolor, oedepict.OEFill_On, 1.0, oedepict.OEStipple_NoLine)
boxsize = random.randint(15, 28)
tl = oedepict.OE2DPoint(boxpos.GetX() - boxsize / 2.0, boxpos.GetY() - boxsize)
br = oedepict.OE2DPoint(boxpos.GetX() + boxsize / 2.0, boxpos.GetY())
image.DrawRectangle(tl, br, boxpen)
ribbonpen = oedepict.OEPen(oechem.OELightGrey, oechem.OELightGrey, oedepict.OEFill_On, 3.0)
ribbonpen.SetLineCap(oedepict.OELineCap_Butt)
bgn = boxpos
end = boxpos + oedepict.OE2DPoint(0.0, -boxsize)
image.DrawLine(bgn, end, ribbonpen)
bgn = oedepict.OE2DPoint(boxpos.GetX() - boxsize / 2.0, boxpos.GetY() - boxsize / 2.0)
end = oedepict.OE2DPoint(boxpos.GetX() + boxsize / 2.0, boxpos.GetY() - boxsize / 2.0)
image.DrawLine(bgn, end, ribbonpen)
boxcolorg = oechem.OELinearColorGradient()
boxcolorg.AddStop(oechem.OEColorStop(0.0, oechem.OELightBlue))
boxcolorg.AddStop(oechem.OEColorStop(1.0, oechem.OEDarkPurple))
for i in range(1, 5):
boxcolor = boxcolorg.GetColorAt(random.random())
pos = oedepict.OE2DPoint(treetop.GetX() - i * 30.0, treetop.GetY() + 330.0)
DrawPresent(image, pos, boxcolor)
boxcolor = boxcolorg.GetColorAt(random.random())
pos = oedepict.OE2DPoint(treetop.GetX() + i * 30.0, treetop.GetY() + 330.0)
DrawPresent(image, pos, boxcolor)
画装饰品
def DrawBall(image, ballpos, ballcolor):
# randomize position
ballpos.SetX(ballpos.GetX() + random.uniform(0, ballpos.GetX() / 25.0))
ballpos.SetY(ballpos.GetY() + random.uniform(0, ballpos.GetY() / 25.0))
ballpen = oedepict.OEPen(ballcolor, ballcolor, oedepict.OEFill_On, 1.0)
ballradius = random.randint(3, 6)
image.DrawCircle(ballpos, ballradius, ballpen)
ballcolorg = oechem.OELinearColorGradient()
ballcolorg.AddStop(oechem.OEColorStop(0.0, oechem.OEYellow))
ballcolorg.AddStop(oechem.OEColorStop(1.0, oechem.OEDarkRed))
True
for i in range(2, 9):
ballcolor = ballcolorg.GetColorAt(random.random())
ballpos = oedepict.OE2DPoint(treetop.GetX(), treetop.GetY() + i * 30)
DrawBall(image, ballpos, ballcolor)
for i in range(2, 8):
for xsign in [10.0, -10.0]:
ballcolor = ballcolorg.GetColorAt(random.random())
ballpos = oedepict.OE2DPoint(treetop.GetX() + i * xsign, treetop.GetY() + i * 35)
DrawBall(image, ballpos, ballcolor)
for i in range(2, 9):
for xsign in [5.0, -5.0]:
ballcolor = ballcolorg.GetColorAt(random.random())
ballpos = oedepict.OE2DPoint(treetop.GetX() + i * xsign, treetop.GetY() + 50 + i * 25)
DrawBall(image, ballpos, ballcolor)
整点词
font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default, 10,
oedepict.OEAlignment_Right, oechem.OELightGrey)
text = "Generated by OEDepict TK"
image.DrawText(oedepict.OE2DPoint(imagewidth - 20.0, imageheight - 20.0), text, font)
保存为图片
import os
os.chdir('/public/gkxiao/work/demo')
oedepict.OEWriteImage("oexmas_2021.png", image)
True