MCPcopy Create free account
hub / github.com/PageBot/PageBot / imageSize

Method imageSize

Lib/pagebot/contexts/basecontext/basecontext.py:1048–1086  ·  view source on GitHub ↗

Answer the images size of path. >>> from pagebot.contexts import getContext >>> from pagebot.document import Document >>> from pagebot.filepaths import getResourcesPath >>> #from pagebot.toolbox.transformer import path2Dir >>> context = getContext() >

(self, path, index=0)

Source from the content-addressed store, hash-verified

1046 '''
1047
1048 def imageSize(self, path, index=0):
1049 """Answer the images size of path.
1050
1051 >>> from pagebot.contexts import getContext
1052 >>> from pagebot.document import Document
1053 >>> from pagebot.filepaths import getResourcesPath
1054 >>> #from pagebot.toolbox.transformer import path2Dir
1055 >>> context = getContext()
1056 >>> #doc = Document(w=123, h=345, context=context, autoPages=1)
1057 >>> resourcesPath = getResourcesPath()
1058 >>> path = resourcesPath + '/images/cookbot1.jpg'
1059 >>> context.imageSize(path)
1060 (1376pt, 1350pt)
1061 >>> #path = resourcesPath + '/templates/test.pdf'
1062 >>> #context.imageSize(path)
1063 #(400pt, 200pt)
1064 """
1065 extension = path2Extension(path)
1066
1067 '''
1068 if extension == FILETYPE_PDF:
1069 im = PdfFileReader(open(path, 'rb'))
1070 r = im.getPage(0).mediaBox # RectangleObject([0, 0, w, h])
1071 w = float(r.getWidth())
1072 h = float(r.getHeight())
1073 return pt(w, h)
1074 '''
1075 if extension == FILETYPE_SVG:
1076 svgTree = ET.parse(path)
1077 # FIXME: Answer the real size from the XML tree
1078 return pt(1000, 1000)
1079
1080 elif self.hasPIL:
1081 # Using Pillow for other image formats.
1082 # TODO: check compatible formats first.
1083 image = Image.open(path)
1084 return pt(image.size)
1085 else:
1086 return None
1087
1088 def imagePixelColor(self, path, p):
1089 return self.b.imagePixelColor(path, p)

Callers 2

initImageSizeMethod · 0.45
initializeImageSizeMethod · 0.45

Calls 3

path2ExtensionFunction · 0.90
ptFunction · 0.90
openMethod · 0.45

Tested by

no test coverage detected