def test_imageFallbackBackgroundColor(self): self.makeTestDrawing() with TempFile(suffix=".jpg") as tmp: drawBot.saveImage(tmp.path, imageJPEGCompressionFactor=1.0) self.assertEqual(drawBot.imagePixelColor(tmp.path, (5, 5)), (1.0, 1.0, 1.0, 1.0)) with TempFile(suffix=".jpg") as tmp: drawBot.saveImage(tmp.path, imageJPEGCompressionFactor=1.0, imageFallbackBackgroundColor=(0, 1, 0)) r, g, b, a = drawBot.imagePixelColor(tmp.path, (5, 5)) self.assertEqual((round(r, 2), round(g, 2), round(b, 2)), (0, 1.0, 0)) with TempFile(suffix=".jpg") as tmp: drawBot.saveImage(tmp.path, imageJPEGCompressionFactor=1.0, imageFallbackBackgroundColor=AppKit.NSColor.redColor()) r, g, b, a = drawBot.imagePixelColor(tmp.path, (5, 5)) self.assertEqual((round(r, 2), round(g, 2), round(b, 2)), (1, 0.0, 0))
def test_buildPackage(self): package = DrawBotPackage() package.info.name = "demo" package.info.developer = "drawbot" package.info.developerURL = "http://www.drawbot.com" package.info.requiresVersion = "3.0" package.info.mainScript = "main.py" succes, _ = package.buildPackage("does/not/exists", "does/not/exists") self.assertFalse(succes) with TempFolder(suffix=".drawbot") as tempPath: succes, _ = package.buildPackage(tempPath.path, "does/not/exists") self.assertFalse(succes) with TempFolder() as tempScriptRoot: succes, _ = package.buildPackage(tempPath.path, tempScriptRoot.path) self.assertFalse(succes) with TempFile(suffix=".py", dir=tempScriptRoot.path) as tmp: with open(tmp.path, "w") as f: f.write("print('hello world'") package.info.mainScript = os.path.basename(tmp.path) succes, _ = package.buildPackage(tempPath.path, tempScriptRoot.path) self.assertTrue(succes)
def test_oddPageWidth_mp4(self): # https://github.com/typemytype/drawbot/issues/250 self.makeTestAnimation(1, pageWidth=201, pageHeight=200) with TempFile(suffix=".mp4") as tmp: with self.assertRaises(DrawBotError) as cm: drawBot.saveImage(tmp.path) self.assertEqual(cm.exception.args[0], "Exporting to mp4 doesn't support odd pixel dimensions for width and height.")
def test_saveImage_multipage_positionalArgument(self): self.makeTestDrawing() with TempFile(suffix=".png") as tmp: with StdOutCollector(captureStdErr=True) as output: drawBot.saveImage(tmp.path, False) self.assertEqual(output.lines(), [ "*** DrawBot warning: 'multipage' should be a keyword argument: use 'saveImage(path, multipage=True)' ***" ])
def test_linkURL_svg(self): expectedPath = os.path.join(testDataDir, "expected_svgLinkURL.svg") drawBot.newDrawing() drawBot.newPage(200, 200) drawBot.rect(10, 10, 20, 20) drawBot.linkURL("http://drawbot.com", (10, 10, 20, 20)) with TempFile(suffix=".svg") as tmp: drawBot.saveImage(tmp.path) self.assertEqual(readData(tmp.path), readData(expectedPath), "Files %r and %s are not the same" % (tmp.path, expectedPath))
def test_formattedStringURL_svg(self): expectedPath = os.path.join(testDataDir, "expected_formattedStringURL.svg") drawBot.newDrawing() drawBot.newPage(200, 200) drawBot.underline("single") drawBot.url("http://drawbot.com") drawBot.text("foo", (10, 10)) with TempFile(suffix=".svg") as tmp: drawBot.saveImage(tmp.path) self.assertEqual(readData(tmp.path), readData(expectedPath), "Files %r and %s are not the same" % (tmp.path, expectedPath))
def test_export_svg_fallbackFont(self): expectedPath = os.path.join(testDataDir, "expected_svgSaveFallback.svg") drawBot.newDrawing() drawBot.newPage(100, 100) drawBot.fallbackFont("Courier") drawBot.font("Times") drawBot.text("a", (10, 10)) with TempFile(suffix=".svg") as tmp: drawBot.saveImage(tmp.path) self.assertEqual(readData(tmp.path), readData(expectedPath), "Files %r and %s are not the same" % (tmp.path, expectedPath))
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 _testMultipage(self, extension, numFrames, expectedMultipageCount): self.makeTestAnimation(numFrames) with TempFolder() as tmpFolder: with TempFile(suffix=extension, dir=tmpFolder.path) as tmp: base, ext = os.path.splitext(tmp.path) pattern = base + "_*" + ext self.assertEqual(len(glob.glob(pattern)), 0) drawBot.saveImage(tmp.path) self.assertEqual(len(glob.glob(pattern)), 0) drawBot.saveImage(tmp.path, multipage=False) self.assertEqual(len(glob.glob(pattern)), 0) drawBot.saveImage(tmp.path, multipage=True) self.assertEqual(len(glob.glob(pattern)), expectedMultipageCount) assert not os.path.exists(tmpFolder.path) # verify TempFolder cleanup
def test_imageAntiAliasing(self): from testScripts import DrawBotTest expectedPath = os.path.join(testDataDir, "expected_imageAntiAliasing.png") drawBot.newDrawing() drawBot.size(100, 100) drawBot.fill(1, 0, 0) drawBot.oval(10, 10, 40, 80) drawBot.fill(0) drawBot.stroke(0) drawBot.line((-0.5, -0.5), (100.5, 100.5)) drawBot.line((0, 20.5), (100, 20.5)) drawBot.fontSize(20) drawBot.text("a", (62, 30)) with TempFile(suffix=".png") as tmp: drawBot.saveImage(tmp.path, antiAliasing=False) self.assertImageFilesEqual(tmp.path, expectedPath)
def test_export_SVG_mixin(self): expectedPath = os.path.join(testDataDir, "expected_svgMixin.svg") drawBot.newDrawing() drawBot.newPage(100, 100) path = drawBot.BezierPath() path.svgID = "hello" path.svgClass = "foo bar" path.svgLink = "drawbot.com" path.rect(0, 0, 20, 20) drawBot.drawPath(path) txt = drawBot.FormattedString() txt += "world" txt.svgID = "hello" txt.svgClass = "foo bar" txt.svgLink = "drawbot.com" drawBot.text(txt, (20, 20)) with TempFile(suffix=".svg") as tmp: drawBot.saveImage(tmp.path) self.assertEqual( readData(tmp.path), readData(expectedPath), "Files %r and %s are not the same" % (tmp.path, expectedPath))
def test_saveImage_multipage_keywordArgument(self): self.makeTestDrawing() with TempFile(suffix=".png") as tmp: with StdOutCollector(captureStdErr=True) as output: drawBot.saveImage(tmp.path, multipage=False) self.assertEqual(output.lines(), [])
def _saveImageAndReturnSize(self, extension, **options): with TempFile(suffix=extension) as tmp: drawBot.saveImage(tmp.path, **options) fileSize = os.stat(tmp.path).st_size return fileSize
def test_animatedGIF(self): self.makeTestAnimation(5) with TempFile(suffix=".gif") as tmp: drawBot.saveImage(tmp.path) self.assertEqual(gifFrameCount(tmp.path), 5)
def test_export_pathlib(self): import pathlib self.makeTestDrawing() with TempFile(suffix=".png") as tmp: drawBot.saveImage(pathlib.Path(tmp.path))