def image(self, path, x, y=None, alpha=None, pageNumber=None): """ Add an image from a `path` with an `offset` and an `alpha` value. This should accept most common file types like pdf, jpg, png, tiff and gif. Optionally an `alpha` can be provided, which is a value between 0 and 1. Optionally a `pageNumber` can be provided when the path referes to a multi page pdf file. .. showcode:: /../examples/image.py """ if isinstance(x, (tuple)): if alpha is None and y is not None: alpha = y x, y = x else: _deprecatedWarningWrapInTuple("image(\"%s\", (%s, %s), alpha=%s)" % (path, x, y, alpha)) if alpha is None: alpha = 1 if isinstance(path, self._imageClass): path = path._nsImage() if isinstance(path, (str, unicode)): path = optimizePath(path) self._requiresNewFirstPage = True self._addInstruction("image", path, (x, y), alpha, pageNumber)
def saveImage(self, paths, multipage=None): """ Save or export the canvas to a specified format. The argument `paths` can either be a single path or a list of paths. The file extension is important because it will determine the format in which the image will be exported. All supported file extensions: `pdf`, `svg`, `png`, `jpg`, `jpeg`, `tiff`, `tif`, `gif`, `bmp` and `mov`. * A `pdf` can be multipage. If `multipage` is `False` only the current page is saved. * A `mov` will use each page as a frame. * A `gif` can be animated when there are multiple pages and it will use each page as a frame. * All images and `svg` formats will only save the current page. If `multipage` is `True` all pages are saved to disk (a page index will be added to the file name). .. showcode:: /../examples/saveImage.py """ if isinstance(paths, (str, unicode)): paths = [paths] for rawPath in paths: path = optimizePath(rawPath) dirName = os.path.dirname(path) if not os.path.exists(dirName): raise DrawBotError("Folder '%s' doesn't exists" % dirName) base, ext = os.path.splitext(path) ext = ext.lower()[1:] if not ext: ext = rawPath context = getContextForFileExt(ext) if context is None: raise DrawBotError( "Did not found a supported context for: '%s'" % ext) self._drawInContext(context) context.saveImage(path, multipage)
def saveImage(self, paths, multipage=None): """ Save or export the canvas to a specified format. The argument `paths` can either be a single path or a list of paths. The file extension is important because it will determine the format in which the image will be exported. All supported file extensions: `pdf`, `svg`, `png`, `jpg`, `jpeg`, `tiff`, `tif`, `gif`, `bmp` and `mov`. * A `pdf` can be multipage. If `multipage` is `False` only the current page is saved. * A `mov` will use each page as a frame. * A `gif` can be animated when there are multiple pages and it will use each page as a frame. * All images and `svg` formats will only save the current page. If `multipage` is `True` all pages are saved to disk (a page index will be added to the file name). .. showcode:: /../examples/saveImage.py """ if isinstance(paths, (str, unicode)): paths = [paths] for path in paths: path = optimizePath(path) dirName = os.path.dirname(path) if not os.path.exists(dirName): raise DrawBotError, "Folder '%s' doesn't exists" % dirName base, ext = os.path.splitext(path) ext = ext.lower()[1:] context = getContextForFileExt(ext) self._drawInContext(context) context.saveImage(path, multipage)
def imageSize(self, path): """ Return the `width` and `height` of an image. .. showcode:: /../examples/imageSize.py """ if isinstance(path, self._imageClass): path = path._nsImage() if isinstance(path, AppKit.NSImage): rep = path.TIFFRepresentation() isPDF = False else: if isinstance(path, (str, unicode)): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: if not os.path.exists(path): raise DrawBotError("Image does not exist") url = AppKit.NSURL.fileURLWithPath_(path) isPDF = AppKit.PDFDocument.alloc().initWithURL_(url) is not None rep = AppKit.NSImageRep.imageRepWithContentsOfURL_(url) if isPDF: w, h = rep.size() else: w, h = rep.pixelsWide(), rep.pixelsHigh() return w, h
def numberOfPages(self, path): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: url = AppKit.NSURL.fileURLWithPath_(path) pdf = Quartz.CGPDFDocumentCreateWithURL(url) if pdf: return Quartz.CGPDFDocumentGetNumberOfPages(pdf) _isGIF, _ = isGIF(url) if _isGIF: frameCount = gifTools.gifFrameCount(url) if frameCount: return frameCount return None
def imageSize(self, path): """ Return the `width` and `height` of an image. .. showcode:: /../examples/imageSize.py """ if isinstance(path, AppKit.NSImage): source = path else: if isinstance(path, (str, unicode)): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: url = AppKit.NSURL.fileURLWithPath_(path) source = AppKit.NSImage.alloc().initByReferencingURL_(url) w, h = source.size() return w, h
def imageSize(self, path): """ Return the `width` and `height` of an image. .. showcode:: /../examples/imageSize.py """ if isinstance(path, AppKit.NSImage): source = path else: if isinstance(path, (str, unicode)): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: url = AppKit.NSURL.fileURLWithPath_(path) source = AppKit.NSImage.alloc().initByReferencingURL_(url) w, h = source.size() return int(w), int(h)
def imageSize(self, path, pageNumber=None): """ Return the `width` and `height` of an image. .. showcode:: /../examples/imageSize.py """ if isinstance(path, self._imageClass): path = path._nsImage() if isinstance(path, AppKit.NSImage): rep = path.TIFFRepresentation() _isPDF = False else: if isinstance(path, (str, unicode)): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: if not os.path.exists(path): raise DrawBotError("Image does not exist") url = AppKit.NSURL.fileURLWithPath_(path) _isPDF, pdfDocument = isPDF(url) # check if the file is an .eps _isEPS, epsRep = isEPS(url) # check if the file is an .gif _isGIF, gifRep = isGIF(url) if _isEPS: _isPDF = True rep = epsRep elif _isGIF and pageNumber is not None: rep = gifTools.gifFrameAtIndex(url, pageNumber - 1) elif _isPDF and pageNumber is not None: page = pdfDocument.pageAtIndex_(pageNumber - 1) # this is probably not the fastest method... rep = AppKit.NSImage.alloc().initWithData_( page.dataRepresentation()) else: rep = AppKit.NSImageRep.imageRepWithContentsOfURL_(url) if _isPDF: w, h = rep.size() elif _isGIF: w, h = rep.size() else: w, h = rep.pixelsWide(), rep.pixelsHigh() return w, h
def imageSize(self, path, pageNumber=None): """ Return the `width` and `height` of an image. .. showcode:: /../examples/imageSize.py """ if isinstance(path, self._imageClass): path = path._nsImage() if isinstance(path, AppKit.NSImage): rep = path.TIFFRepresentation() _isPDF = False else: if isinstance(path, (str, unicode)): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: if not os.path.exists(path): raise DrawBotError("Image does not exist") url = AppKit.NSURL.fileURLWithPath_(path) _isPDF, pdfDocument = isPDF(url) # check if the file is an .eps _isEPS, epsRep = isEPS(url) # check if the file is an .gif _isGIF, gifRep = isGIF(url) if _isEPS: _isPDF = True rep = epsRep elif _isGIF and pageNumber is not None: rep = gifTools.gifFrameAtIndex(url, pageNumber-1) elif _isPDF and pageNumber is not None: page = pdfDocument.pageAtIndex_(pageNumber-1) # this is probably not the fastest method... rep = AppKit.NSImage.alloc().initWithData_(page.dataRepresentation()) else: rep = AppKit.NSImageRep.imageRepWithContentsOfURL_(url) if _isPDF: w, h = rep.size() elif _isGIF: w, h = rep.size() else: w, h = rep.pixelsWide(), rep.pixelsHigh() return w, h
def image(self, path, x, y=None, alpha=None): """ Add an image from a `path` with an `offset` and an `alpha` value. This should accept most common file types like pdf, jpg, png, tiff and gif. Optionally an `alpha` can be provided, which is a value between 0 and 1. .. showcode:: /../examples/image.py """ if isinstance(x, (tuple)): if alpha is None and y is not None: alpha = y x, y = x else: _deprecatedWarning("image(\"%s\", (%s, %s), alpha=%s)" % (path, x, y, alpha)) if alpha is None: alpha = 1 if isinstance(path, (str, unicode)): path = optimizePath(path) self._addInstruction("image", path, (x, y), alpha)
if _isPDF: w, h = rep.size() elif _isGIF: w, h = rep.size() else: w, h = rep.pixelsWide(), rep.pixelsHigh() return w, h def imagePixelColor(self, path, (x, y)): """ Return the color `r, g, b, a` of an image at a specified `x`, `y` possition. .. showcode:: /../examples/pixelColor.py """ if isinstance(path, (str, unicode)): path = optimizePath(path) bitmap = _chachedPixelColorBitmaps.get(path) if bitmap is None: if isinstance(path, self._imageClass): source = path._nsImage() elif isinstance(path, AppKit.NSImage): source = path else: if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: url = AppKit.NSURL.fileURLWithPath_(path) source = AppKit.NSImage.alloc().initByReferencingURL_(url) bitmap = AppKit.NSBitmapImageRep.imageRepWithData_( source.TIFFRepresentation())
w, h = source.size() return int(w), int(h) def imagePixelColor(self, path, (x, y)): """ Return the color `r, g, b, a` of an image at a specified `x`, `y` possition. .. showcode:: /../examples/pixelColor.py """ bitmap = _chachedPixelColorBitmaps.get(path) if bitmap is None: if isinstance(path, AppKit.NSImage): source = path else: if isinstance(path, (str, unicode)): path = optimizePath(path) if path.startswith("http"): url = AppKit.NSURL.URLWithString_(path) else: url = AppKit.NSURL.fileURLWithPath_(path) source = AppKit.NSImage.alloc().initByReferencingURL_(url) bitmap = AppKit.NSBitmapImageRep.imageRepWithData_(source.TIFFRepresentation()) _chachedPixelColorBitmaps[path] = bitmap color = bitmap.colorAtX_y_(x, bitmap.pixelsHigh() - y - 1) if color is None: return None color = color.colorUsingColorSpaceName_("NSCalibratedRGBColorSpace") return color.redComponent(), color.greenComponent(), color.blueComponent(), color.alphaComponent()