def test_imageResolution(self): self.makeTestDrawing() with TempFile(suffix=".png") as tmp: drawBot.saveImage(tmp.path) self.assertEqual(drawBot.imageSize(tmp.path), (500, 500)) drawBot.saveImage(tmp.path, imageResolution=144) self.assertEqual(drawBot.imageSize(tmp.path), (1000, 1000)) drawBot.saveImage(tmp.path, imageResolution=36) self.assertEqual(drawBot.imageSize(tmp.path), (250, 250)) drawBot.saveImage(tmp.path, imageResolution=18) self.assertEqual(drawBot.imageSize(tmp.path), (125, 125))
def imageSize(cls, path): """Answer the images size in points. >>> imagePath = '../../resources/images/cookbot10.jpg' >>> Image.imageSize(imagePath) (2058, 946) """ return drawBot.imageSize(path)
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 imageSize(self, path): """Answers the (w, h) image size of the image file at path. If the path is an SVG image, then determine by parsing the SVG-XML. >>> builder = InDesignBuilder() >>> builder.imageSize('../../resources/images/cookbot10.jpg') (2058, 946) >>> builder.imageSize('../../resources/images/Berthold-Grid.pdf') (590, 842) >>> builder.imageSize('../../NOTEXIST.pdf') is None True """ if not os.path.exists(path): return None w, h = drawBot.imageSize(path) return int(round(w)), int(round(h))
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 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 get_image_rect(src): w, h = db.imageSize(str(src)) return Rect(0, 0, w, h)
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 initImageSize(self): if self.path is not None and os.path.exists(self.path): self.iw, self.ih = imageSize(self.path) else: self.iw = self.ih = 0 # Undefined, there is no image file.
def mockImageSize(path): return drawBot.imageSize(mockedImagePath)
# --- Constants --- # # --- Objects & Methods --- # def createPlaceholder(wdt, hgt, locationOnDisk): dB.newDrawing() dB.newPage(wdt, hgt) dB.fill(.5) dB.rect(0, 0, dB.width(), dB.height()) dB.stroke(1) dB.strokeWidth(10) dB.line((0, 0), (dB.width(), dB.height())) dB.line((0, dB.height()), (dB.width(), 0)) dB.saveImage(f'{locationOnDisk}') dB.endDrawing() # --- Variables --- # # --- Instructions --- # if __name__ == '__main__': root_directory = Path(".") for eachJPG in root_directory.glob('**/*.jpg'): print(f"hi, I'm a file: {eachJPG}") wdt, hgt = dB.imageSize(f'{eachJPG}') createPlaceholder(wdt, hgt, eachJPG)
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 os db.newDrawing() background_images = os.listdir('img/tiles-copy/') background_image_paths = [] for background_image in background_images: background_image_paths.append('img/tiles-copy/' + background_image) # print(background_image_paths) db.newPage(1240, 496) tileSize = 124 images = [['i' for i in range(4)] for j in range(10)] srcWidth, srcHeight = db.imageSize(background_image_paths[0]) dstWidth, dstHeight = tileSize, tileSize factorWidth = dstWidth / srcWidth factorHeight = dstHeight / srcHeight 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):
tests = [ os.path.join(root, filename) for filename in os.listdir(root) if os.path.splitext(filename)[-1].lower() == ".png" ] drawBot.newDrawing() for path in tests: fileName = os.path.basename(path) if not fileName.startswith("example_"): fileName = "expected_" + fileName localPath = os.path.join(testDataDir, fileName) if not os.path.exists(localPath): continue w, h = drawBot.imageSize(path) a = compareImages(localPath, path) padding = 0 if a > 0.0012: 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)
def imageSize(self, path): return drawBot.imageSize(path)
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))