def test_instructionStack(self): expected = [ "reset None", "newPage 200 200", "save", "clipPath moveTo 5.0 5.0 lineTo 15.0 5.0 lineTo 15.0 15.0 lineTo 5.0 15.0 closePath", "restore", "image Image Object 10 10 0.5 None", "blendMode saturation", "transform 1 0 0 1 10 10", "drawPath moveTo 10.0 10.0 lineTo 110.0 10.0 lineTo 110.0 110.0 lineTo 10.0 110.0 closePath", "textBox foo bar 72.48291015625 84.0 55.0341796875 26.0 center", "frameDuration 10", "saveImage * {'myExtraAgrument': True}" ] with StdOutCollector() as output: import drawBot drawBot.newDrawing() drawBot.size(200, 200) drawBot.save() path = drawBot.BezierPath() path.rect(5, 5, 10, 10) drawBot.clipPath(path) drawBot.restore() im = drawBot.ImageObject() with im: drawBot.size(20, 20) drawBot.rect(5, 5, 10, 10) drawBot.image(im, (10, 10), alpha=.5) drawBot.blendMode("saturation") drawBot.translate(10, 10) drawBot.rect(10, 10, 100, 100) drawBot.text("foo bar", (100, 100), align="center") drawBot.frameDuration(10) drawBot.saveImage("*", myExtraAgrument=True) drawBot.endDrawing() self.assertEqual(output.lines(), expected)
def backgroundImage(canvasWidth, canvasHeight): background_images = os.listdir('background_images/') background_image_path = 'background_images/' + background_images[(int)( len(background_images) * random.random())] # https://forum.drawbot.com/topic/180/how-do-i-size-an-image-with-the-imageobject-or-without/4 srcWidth, srcHeight = db.imageSize(background_image_path) dstWidth, dstHeight = canvasWidth, canvasHeight factorWidth = dstWidth / srcWidth factorHeight = dstHeight / srcHeight with db.savedState(): db.scale(factorWidth, factorHeight) db.image(background_image_path, (0, 0))
def drawContent(self, ox, oy, doc, page, parent): """We just need to define drawing of the image. The rest of behavior for the Image element (including drawing on the background and the frame) is handled by the base Element class. """ # Get the scale of the image, comparing the file size with the size # of the image element. sx, sy = self.getScale() # In drawBot it is not possible to scale the image, so we need to scale # the canvas instead. Then also we need to scale the (ox, oy) positions. # After drawing, reverse scale the canvas back to 100% drawBot.scale(sx, sy) drawBot.image(self.path, (ox / sx, oy / sy)) drawBot.scale(1 / sx, 1 / sy)
def feelingSlide(canvasWidth, canvasHeight, polarity): db.newPage(canvasWidth, canvasHeight) background_fill = polarityBackground(polarity) db.fill(*background_fill) db.frameDuration(4) db.rect(0, 0, canvasWidth, canvasHeight) background_images = os.listdir('background_images/') background_image_path = 'background_images/' + background_images[(int)( len(background_images) * random.random())] # https://forum.drawbot.com/topic/180/how-do-i-size-an-image-with-the-imageobject-or-without/4 srcWidth, srcHeight = db.imageSize(background_image_path) dstWidth, dstHeight = canvasWidth - 50, canvasHeight - 50 factorWidth = dstWidth / srcWidth factorHeight = dstHeight / srcHeight with db.savedState(): db.translate(25, 25) with db.savedState(): db.scale(factorWidth, factorHeight) db.image(background_image_path, (0, 0)) dril_feels_text = db.FormattedString() dril_feels_text.append("@dril feels", font="Calibri-Bold", fontSize=150, fill=1, align='center', stroke=background_fill, strokeWidth=0.5) db.shadow((0, 0), 50, background_fill) db.text(dril_feels_text, (canvasWidth / 2, canvasHeight - 300)) if polarity < -0.1: drils_feeling = "angry" db.font("LucidaBlackletter", 250) elif polarity < 0.25: drils_feeling = "neutral" db.font("Helvetica", 180) else: drils_feeling = "happy" db.font("Cortado", 250) db.fill(1) db.shadow((0, 0), 50, background_fill) db.text(drils_feeling, (canvasWidth / 2, 250), align='center')
def draw(pen, state, data): if state == 0: DrawBotPen(pen, rect).draw(scale=scale) elif state == -1: imgf = pen.data.get("imgf") if imgf: im = db.ImageObject() im.lockFocus() db.size(rect.w+300, rect.h+300) db.translate(150, 150) db.scale(scale) pen.data["im"] = im elif state == 1: imgf = pen.data.get("imgf") im = pen.data.get("im") if imgf and im: im.unlockFocus() imgf(im) x, y = im.offset() db.translate(-150, -150) db.image(im, (x, y))
def image(self, src=None, opacity=1, rect=None, rotate=0, repeating=False, scale=True): bounds = self.dat.bounds() src = str(src) if not rect: rect = bounds try: img_w, img_h = db.imageSize(src) except ValueError: print("DrawBotPen: No image") return x = bounds.x y = bounds.y if repeating: x_count = bounds.w / rect.w y_count = bounds.h / rect.h else: x_count = 1 y_count = 1 _x = 0 _y = 0 while x <= (bounds.w+bounds.x) and _x < x_count: _x += 1 while y <= (bounds.h+bounds.y) and _y < y_count: _y += 1 with db.savedState(): r = Rect(x, y, rect.w, rect.h) #db.fill(1, 0, 0.5, 0.05) #db.oval(*r) if scale == True: db.scale(rect.w/img_w, center=r.point("SW")) elif scale: try: db.scale(scale[0], scale[1], center=r.point("SW")) except TypeError: db.scale(scale, center=r.point("SW")) db.rotate(rotate) db.image(src, (r.x, r.y), alpha=opacity) y += rect.h y = 0 x += rect.w
def draw(self, origin, view): u"""Draw the image in the calculated scale. Since we need to use the image by scale transform, all other measure (position, lineWidth) are scaled back to their original proportions. If stroke is defined, then use that to draw a frame around the image. Note that the (sx, sy) is already scaled to fit the padding position and size.""" p = pointOffset(self.oPoint, origin) p = self._applyScale(p) px, py, _ = self._applyAlignment(p) # Ignore z-axis for now. if self.path is None or not os.path.exists(self.path) or not self.iw or not self.ih: # TODO: Also show error, in case the image does not exist, to differ from empty box. print 'Cannot display pixelMap', self #self._drawMissingElementRect(page, px, py, self.w, self.h) else: save() sx = self.w / self.iw sy = self.h / self.ih scale(sx, sy) # If there is a clipRect defined, create the bezier path if self.clipRect is not None: clipRect = BezierPath() clX, clY, clW, clH = self.clipRect sclX = clX/sx sclY = clY/sx sclW = clW/sx sclH = clH/sy # move to a point clipRect.moveTo((sclX, sclY)) # line to a point clipRect.lineTo((sclX, sclY+sclH)) clipRect.lineTo((sclX+sclW, sclY+sclH)) clipRect.lineTo((sclX+sclW, sclY)) # close the path clipRect.closePath() # set the path as a clipping path clipPath(clipRect) # the image will be clipped inside the path #fill(1, 0, 0, 0.5) #drawPath(clipRect) elif self.clipPath is not None: #Otherwise if there is a clipPath, then use it. clipPath(self.clipPath) if self.imo is not None: with self.imo: image(self.path, (0, 0), pageNumber=0, alpha=self._getAlpha()) image(self.imo, (px/sx, py/sy), pageNumber=0, alpha=self._getAlpha()) else: # Store page element Id in this image, in case we want to make an image index later. image(self.path, (px/sx, py/sy), pageNumber=0, alpha=self._getAlpha()) # TODO: Draw optional (transparant) forground color? restore() # If there are child elements, draw them over the pixel image. self._drawElements(origin, view) self._restoreScale() view.drawElementMetaInfo(self, origin)
def test_instructionStack(self): expected = [ "reset None", "newPage 200 200", "save", "clipPath moveTo 5.0 5.0 lineTo 15.0 5.0 lineTo 15.0 15.0 lineTo 5.0 15.0 closePath", "restore", "image Image Object 10 10 0.5 None", "blendMode saturation", "transform 1 0 0 1 10 10", "drawPath moveTo 10.0 10.0 lineTo 110.0 10.0 lineTo 110.0 110.0 lineTo 10.0 110.0 closePath", "textBox foo bar 82.48291015625 84.0 35.0341796875 26.0 center", "frameDuration 10", "saveImage * {'myExtraAgrument': True}" ] with StdOutCollector() as output: import drawBot drawBot.newDrawing() drawBot.size(200, 200) drawBot.save() path = drawBot.BezierPath() path.rect(5, 5, 10, 10) drawBot.clipPath(path) drawBot.restore() im = drawBot.ImageObject() with im: drawBot.size(20, 20) drawBot.rect(5, 5, 10, 10) drawBot.image(im, (10, 10), alpha=.5) drawBot.blendMode("saturation") drawBot.translate(10, 10) drawBot.rect(10, 10, 100, 100) drawBot.text("foo bar", (100, 100), align="center") drawBot.frameDuration(10) drawBot.saveImage("*", myExtraAgrument=True) drawBot.endDrawing() self.assertEqual(output.lines(), expected)
import sys sys.path.append('./') sys.path.append('./../') import drawBot as db width = 300 height = 250 db.newDrawing() db.size(width, height) # db.fill(0, .9) # db.rect(0, 0, width, height) # db.stroke(1) frame = db.ImageObject('assets/[email protected]') sf = width / frame.size()[0] db.scale(sf) db.image(frame, (0,0)) db.scale(1/sf) badge = db.ImageObject('assets/badge.png') sf = 500 / db.imageSize(badge)[0] badge.lanczosScaleTransform(sf) print(badge.size()) db.blendMode('difference') db.image(badge, (30, 30)) db.saveImage('outputs/svg_test.png') db.endDrawing()
import drawBot drawBot.size(500, 500) imagePath = "../data/drawBot.pdf" w, h = drawBot.imageSize(imagePath) drawBot.scale(250 / w) drawBot.image(imagePath, (0, 0)) drawBot.image(imagePath, (w, 0), alpha=0.5) drawBot.image(imagePath, (0, h), alpha=0.25) drawBot.image(imagePath, (w, h), alpha=0.75)
def mockImage(path, position, alpha=1): if isinstance(path, DrawBotDrawingTool._imageClass): drawBot.image(path, position, alpha) else: drawBot.image(mockedImagePath, position, alpha)
import pathlib import drawBot drawBot.size(500, 500) imagePath = "../data/drawBot.pdf" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (0, 0)) drawBot.restore() imagePath = "../data/drawBot.png" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (w, 0)) drawBot.restore() imagePath = "../data/drawBot.jpg" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (0, h)) drawBot.restore() imagePath = "../data/drawBot.bmp" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(pathlib.Path(imagePath), (w, h)) # verify that pathlib.Path objects work drawBot.restore()
from os import listdir from os.path import isfile, join width = 970 height = 600 files = [f for f in listdir('outputs/gif/') if isfile(join('outputs/gif', f))] shuffle(files) db.newDrawing() db.size(width, height) for i in range(len(files)): fp = 'outputs/gif/' + files[i] print(fp) db.newPage(width, height) db.frameDuration(.25) # db.fill(0) # db.rect(0,0,width,height) # db.fill(None) im = db.ImageObject(fp) if (im.size()[1] == height): pos = ((width - im.size()[0]) / 2, 0) elif (im.size()[0] == width): pos = (0, (height - im.size()[1]) / 2) else: pos = ((width - im.size()[0]) / 2, (height - im.size()[1]) / 2) db.image(im, pos) db.saveImage('outputs/gif_slow.gif') db.endDrawing()
import sys sys.path.append('./') import drawBot drawBot.newDrawing() drawBot.size(1080,1080) im = drawBot.ImageObject('assets/1080_1080.png') texture = drawBot.ImageObject('assets/chalkboard_invert.png') texture.lanczosScaleTransform(2.5) im.blendWithMask(backgroundImage=None, maskImage=texture) mask = drawBot.ImageObject('assets/mask_square.png') im.blendWithMask(backgroundImage=None, maskImage=mask) drawBot.image(im, (0,0)) drawBot.saveImage('outputs/experiments/texture.png')
import drawBot drawBot.size(500, 500) imagePath = "../data/drawBot.pdf" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (0, 0)) drawBot.restore() imagePath = "../data/drawBot.png" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (w, 0)) drawBot.restore() imagePath = "../data/drawBot.jpg" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (0, h)) drawBot.restore() imagePath = "../data/drawBot.bmp" w, h = drawBot.imageSize(imagePath) drawBot.save() drawBot.scale(250 / w) drawBot.image(imagePath, (w, h)) drawBot.restore()
for x in range(0, 1240, tileSize): for y in range(0, 496, tileSize): with db.savedState(): stepX = int(x/tileSize) stepY = int(y/tileSize) validPath = False while not validPath: path = random.choice(background_image_paths) if(stepX > 0): if(images[stepX-1][stepY] == path): continue if(stepY > 0): if(images[stepX][stepY-1] == path): continue validPath = True db.translate(x, y) db.scale(factorWidth, factorHeight) db.image(path, (0, 0) ) images[int(x/tileSize)][int(y/tileSize)] = path db.saveImage('img/header.png') # srcWidth, srcHeight = db.imageSize(background_image_path) # dstWidth, dstHeight = canvasWidth, canvasHeight # factorWidth = dstWidth / srcWidth # factorHeight = dstHeight / srcHeight # with db.savedState(): # db.scale(factorWidth, factorHeight) # db.image(background_image_path, (0, 0)) db.endDrawing()
import drawBot drawBot.size(250, 250) imagePath = "http://f.cl.ly/items/1T3x1y372J371p0v1F2Z/drawBot.jpg" drawBot.scale(250 / 700) drawBot.image(imagePath, (20, 20))
import drawBot drawBot.size(250, 250) imagePath = "https://d1sz9tkli0lfjq.cloudfront.net/items/1T3x1y372J371p0v1F2Z/drawBot.jpg" drawBot.scale(250 / 700) drawBot.image(imagePath, (20, 20))
padding = 50 pathPadding = 30 drawBot.newPage(w * 4 + padding * 2, h + padding * 2 + pathPadding) drawBot.translate(0, pathPadding) if padding: with drawBot.savedState(): drawBot.fill(None) drawBot.stroke(1, 0, 0) drawBot.strokeWidth(padding * 2) drawBot.rect(0, 0, drawBot.width(), drawBot.height()) drawBot.translate(padding, padding) drawBot.text(f"{os.path.basename(path)} - {os.path.basename(localPath)}", (10, 10 - padding - pathPadding)) drawBot.image(path, (0, 0)) drawBot.image(localPath, (w, 0)) im1 = Image.open(path) im2 = Image.open(localPath) diff = ImageChops.difference(im1, im2) with tempfile.NamedTemporaryFile("wb", suffix=".png") as f: diff.save(f, "png") imDiff = drawBot.ImageObject(f.name) drawBot.image(imDiff, (w * 2, 0)) hist = diff.histogram() redPath = drawBot.BezierPath() greenPath = drawBot.BezierPath() bluePath = drawBot.BezierPath()
def image(self, path, p): drawBot.image(path, p)
def answerSlide(canvasWidth, canvasHeight, answer, polarity): background_fill = polarityBackground(polarity) db.newPage(canvasWidth, canvasHeight) db.fill(*background_fill) db.rect(0, 0, canvasWidth, canvasHeight) db.frameDuration(4) background_images = os.listdir('background_images/') background_image_path = 'background_images/' + background_images[(int)( len(background_images) * random.random())] # https://forum.drawbot.com/topic/180/how-do-i-size-an-image-with-the-imageobject-or-without/4 srcWidth, srcHeight = db.imageSize(background_image_path) dstWidth, dstHeight = canvasWidth - 50, canvasHeight - 50 factorWidth = dstWidth / srcWidth factorHeight = dstHeight / srcHeight with db.savedState(): db.translate(25, 25) with db.savedState(): db.scale(factorWidth, factorHeight) db.image(background_image_path, (0, 0)) db.fill(*rgba(*background_fill, 0.1)) box_width = 0.7 * canvasWidth box_height = canvasHeight * 0.7 x_0 = (canvasWidth - box_width) / 2 y_0 = (canvasHeight - box_height) / 2 - 100 text_box_margin = 40 text_box_width = box_width - text_box_margin * 2 text_box_height = box_height - text_box_margin * 2 current_font_size = 10 db.font('Calibri-Bold', current_font_size) # this is not efficient. Don't show anyone I made this while True: db.fontSize(current_font_size) current_font_size += 1 _, current_text_height = db.textSize(answer, 'left', width=text_box_width) if (current_font_size > 150): break elif (current_text_height > text_box_height): current_font_size -= 2 break db.fontSize(current_font_size) db.stroke(*background_fill) db.strokeWidth(0.5) db.fill(*rgb(255, 252, 61)) db.textBox(answer, (x_0, y_0, box_width, box_height), 'left') # dril says d_says = db.FormattedString() d_says.append("@dril says:", font="Calibri-Bold", fontSize=100, fill=rgb(255, 252, 61), stroke=background_fill, strokeWidth=2) # db.shadow((0,0), 50, background_fill) db.text(d_says, (x_0, y_0 + box_height + 30))