aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility
diff options
context:
space:
mode:
authorzsloan2020-08-17 17:14:29 -0500
committerGitHub2020-08-17 17:14:29 -0500
commit75ebbf446606d7498bb16a01a4912c66eba17cb5 (patch)
tree0bca7be443ff8b897e3b84075d5c756a4d4fd66b /wqflask/utility
parent1e421a063d750df485a768aa6da14b3db592d409 (diff)
parentd157019159ae3eb2e3efb02d874a1b4edfc559cb (diff)
downloadgenenetwork2-75ebbf446606d7498bb16a01a4912c66eba17cb5.tar.gz
Merge pull request #417 from BonfaceKilz/feature/pil-pillow-conversion
Resolve merge conflicts from Pil Pillow conversion branch
Diffstat (limited to 'wqflask/utility')
-rw-r--r--wqflask/utility/Plot.py89
-rw-r--r--wqflask/utility/pillow_utils.py25
2 files changed, 90 insertions, 24 deletions
diff --git a/wqflask/utility/Plot.py b/wqflask/utility/Plot.py
index 9bc84d22..d4373412 100644
--- a/wqflask/utility/Plot.py
+++ b/wqflask/utility/Plot.py
@@ -26,7 +26,7 @@
from __future__ import print_function
-import piddle as pid
+from PIL import (Image, ImageColor, ImageDraw, ImageFont)
from pprint import pformat as pf
from math import *
@@ -38,10 +38,21 @@ from numarray import ones, array, dot, swapaxes
import webqtlUtil
import corestats
from base import webqtlConfig
-
+from utility.pillow_utils import draw_rotated_text
import utility.logger
logger = utility.logger.getLogger(__name__ )
+# ---- Define common colours ---- #
+BLUE = ImageColor.getrgb("blue")
+BLACK = ImageColor.getrgb("black")
+# ---- END: Define common colours ---- #
+
+# ---- FONT FILES ---- #
+VERDANA_FILE = "./wqflask/static/fonts/verdana.ttf"
+COUR_FILE = "./wqflask/static/fonts/courbd.ttf"
+TAHOMA_FILE = "./wqflask/static/fonts/tahoma.ttf"
+# ---- END: FONT FILES ---- #
+
def cformat(d, rank=0):
'custom string format'
strD = "%2.6f" % d
@@ -114,7 +125,8 @@ def find_outliers(vals):
# parameter: data is either object returned by reaper permutation function (called by MarkerRegressionPage.py)
# or the first object returned by direct (pair-scan) permu function (called by DirectPlotPage.py)
-def plotBar(canvas, data, barColor=pid.blue, axesColor=pid.black, labelColor=pid.black, XLabel=None, YLabel=None, title=None, offset= (60, 20, 40, 40), zoom = 1):
+def plotBar(canvas, data, barColor=BLUE, axesColor=BLACK, labelColor=BLACK, XLabel=None, YLabel=None, title=None, offset= (60, 20, 40, 40), zoom = 1):
+ im_drawer = ImageDraw.Draw(canvas)
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
@@ -161,43 +173,67 @@ def plotBar(canvas, data, barColor=pid.blue, axesColor=pid.black, labelColor=pid
if count:
xc = (dataXY[i]-xLow)*xScale+xLeftOffset
yc =-(count-yLow)*yScale+yTopOffset+plotHeight
- canvas.drawRect(xc+2,yc,xc+barWidth-2,yTopOffset+plotHeight,edgeColor=barColor,fillColor=barColor)
+ im_drawer.rectangle(
+ xy=((xc+2,yc),(xc+barWidth-2,yTopOffset+plotHeight)),
+ outline=barColor, fill=barColor)
#draw drawing region
- canvas.drawRect(xLeftOffset, yTopOffset, xLeftOffset+plotWidth, yTopOffset+plotHeight)
+ im_drawer.rectangle(
+ xy=((xLeftOffset, yTopOffset), (xLeftOffset+plotWidth, yTopOffset+plotHeight))
+ )
#draw scale
- scaleFont=pid.Font(ttf="cour",size=11,bold=1)
+ scaleFont=ImageFont.truetype(font=COUR_FILE,size=11)
x=xLow
for i in range(int(stepX)+1):
xc=xLeftOffset+(x-xLow)*xScale
- canvas.drawLine(xc,yTopOffset+plotHeight,xc,yTopOffset+plotHeight+5, color=axesColor)
+ im_drawer.line(
+ xy=((xc,yTopOffset+plotHeight),(xc,yTopOffset+plotHeight+5)),
+ fill=axesColor)
strX = cformat(d=x, rank=0)
- canvas.drawString(strX,xc-canvas.stringWidth(strX,font=scaleFont)/2,yTopOffset+plotHeight+14,font=scaleFont)
+ im_drawer.text(
+ text=strX,
+ xy=(xc-im_drawer.textsize(strX,font=scaleFont)[0]/2,
+ yTopOffset+plotHeight+14),font=scaleFont)
x+= (xTop - xLow)/stepX
y=yLow
for i in range(int(stepY)+1):
yc=yTopOffset+plotHeight-(y-yLow)*yScale
- canvas.drawLine(xLeftOffset,yc,xLeftOffset-5,yc, color=axesColor)
+ im_drawer.line(xy=((xLeftOffset,yc),(xLeftOffset-5,yc)), fill=axesColor)
strY = "%d" %y
- canvas.drawString(strY,xLeftOffset-canvas.stringWidth(strY,font=scaleFont)-6,yc+5,font=scaleFont)
+ im_drawer.text(
+ text=strY,
+ xy=(xLeftOffset-im_drawer.textsize(strY,font=scaleFont)[0]-6,yc+5),
+ font=scaleFont)
y+= (yTop - yLow)/stepY
#draw label
- labelFont=pid.Font(ttf="tahoma",size=17,bold=0)
+ labelFont=ImageFont.truetype(font=TAHOMA_FILE,size=17)
if XLabel:
- canvas.drawString(XLabel,xLeftOffset+(plotWidth-canvas.stringWidth(XLabel,font=labelFont))/2.0,
- yTopOffset+plotHeight+yBottomOffset-10,font=labelFont,color=labelColor)
+ im_drawer.text(
+ text=XLabel,
+ xy=(xLeftOffset+(
+ plotWidth-im_drawer.textsize(XLabel,font=labelFont)[0])/2.0,
+ yTopOffset+plotHeight+yBottomOffset-10),
+ font=labelFont,fill=labelColor)
if YLabel:
- canvas.drawString(YLabel, 19, yTopOffset+plotHeight-(plotHeight-canvas.stringWidth(YLabel,font=labelFont))/2.0,
- font=labelFont,color=labelColor,angle=90)
-
- labelFont=pid.Font(ttf="verdana",size=16,bold=0)
+ draw_rotated_text(canvas, text=YLabel,
+ xy=(19,
+ yTopOffset+plotHeight-(
+ plotHeight-im_drawer.textsize(
+ YLabel,font=labelFont)[0])/2.0),
+ font=labelFont, fill=labelColor, angle=90)
+
+ labelFont=ImageFont.truetype(font=VERDANA_FILE,size=16)
if title:
- canvas.drawString(title,xLeftOffset+(plotWidth-canvas.stringWidth(title,font=labelFont))/2.0,
- 20,font=labelFont,color=labelColor)
+ im_drawer.text(
+ text=title,
+ xy=(xLeftOffset+(plotWidth-im_drawer.textsize(
+ title,font=labelFont)[0])/2.0,
+ 20),
+ font=labelFont,fill=labelColor)
# This function determines the scale of the plot
def detScaleOld(min,max):
@@ -265,16 +301,21 @@ def greenfunc(x):
def colorSpectrum(n=100):
multiple = 10
if n == 1:
- return [pid.Color(1,0,0)]
+ return [ImageColor.getrgb("rgb(100%,0%,0%)")]
elif n == 2:
- return [pid.Color(1,0,0),pid.Color(0,0,1)]
+ return [ImageColor.getrgb("100%,0%,0%)"),
+ ImageColor.getrgb("rgb(0%,0%,100%)")]
elif n == 3:
- return [pid.Color(1,0,0),pid.Color(0,1,0),pid.Color(0,0,1)]
+ return [ImageColor.getrgb("rgb(100%,0%,0%)"),
+ ImageColor.getrgb("rgb(0%,100%,0%)"),
+ ImageColor.getrgb("rgb(0%,0%,100%)")]
N = n*multiple
out = [None]*N;
for i in range(N):
x = float(i)/N
- out[i] = pid.Color(redfunc(x), greenfunc(x), bluefunc(x));
+ out[i] = ImageColor.getrgb("rgb({}%,{}%,{}%".format(
+ *[int(i*100) for i in (
+ redfunc(x), greenfunc(x), bluefunc(x))]))
out2 = [out[0]]
step = N/float(n-1)
j = 0
@@ -290,4 +331,4 @@ def _test():
if __name__=="__main__":
- _test() \ No newline at end of file
+ _test()
diff --git a/wqflask/utility/pillow_utils.py b/wqflask/utility/pillow_utils.py
new file mode 100644
index 00000000..dfbf3e19
--- /dev/null
+++ b/wqflask/utility/pillow_utils.py
@@ -0,0 +1,25 @@
+from PIL import Image, ImageColor, ImageDraw, ImageFont
+
+import utility.logger
+logger = utility.logger.getLogger(__name__ )
+
+BLACK = ImageColor.getrgb("black")
+WHITE = ImageColor.getrgb("white")
+
+# def draw_rotated_text(canvas: Image, text: str, font: ImageFont, xy: tuple, fill: ImageColor=BLACK, angle: int=-90):
+def draw_rotated_text(canvas, text, font, xy, fill=BLACK, angle=-90):
+ # type: (Image, str, ImageFont, tuple, ImageColor, int)
+ """Utility function draw rotated text"""
+ tmp_img = Image.new("RGBA", font.getsize(text), color=(0,0,0,0))
+ draw_text = ImageDraw.Draw(tmp_img)
+ draw_text.text(text=text, xy=(0,0), font=font, fill=fill)
+ tmp_img2 = tmp_img.rotate(angle, expand=1)
+ tmp_img2.save("/tmp/{}.png".format(text), format="png")
+ canvas.paste(im=tmp_img2, box=tuple([int(i) for i in xy]))
+
+# def draw_open_polygon(canvas: Image, xy: tuple, fill: ImageColor=WHITE, outline: ImageColor=BLACK):
+def draw_open_polygon(canvas, xy, fill=None, outline=BLACK, width=0):
+ # type: (Image, tuple, ImageColor, ImageColor)
+ draw_ctx = ImageDraw.Draw(canvas)
+ draw_ctx.polygon(xy, fill=fill)
+ draw_ctx.line(xy, fill=outline, width=width)