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

Method scaleImage

Lib/pagebot/contexts/htmlcontext/htmlcontext.py:169–204  ·  view source on GitHub ↗

>>> from pagebot.filepaths import getResourcesPath >>> srcPath = getResourcesPath() + '/images/peppertom.png' >>> path2ScaledImagePath(srcPath, 100, 200, 0, 'png') 'scaled/peppertom-w100-h200-i0.png' >>> context = HtmlContext() >>> imagePath = context

(self, path, w, h, index=None, showImageLoresMarker=False,
            exportExtension=None, force=False)

Source from the content-addressed store, hash-verified

167 #return cls.b.imagePixelColor(path, p)
168
169 def scaleImage(self, path, w, h, index=None, showImageLoresMarker=False,
170 exportExtension=None, force=False):
171 """
172 >>> from pagebot.filepaths import getResourcesPath
173 >>> srcPath = getResourcesPath() + '/images/peppertom.png'
174 >>> path2ScaledImagePath(srcPath, 100, 200, 0, 'png')
175 'scaled/peppertom-w100-h200-i0.png'
176 >>> context = HtmlContext()
177 >>> imagePath = context.scaleImage(srcPath, 100, 200)
178 >>> imagePath
179 'scaled/peppertom-w100-h200.png'
180 >>> context.imageSize(imagePath)
181 (100pt, 133pt)
182 """
183 if exportExtension is None:
184 exportExtension = path2Extension(path)
185 exportExtension = exportExtension.lower()
186 # FIXME, for now hard-converted to png, as JPG does not for for PIL now.
187 exportExtension = 'png'
188 # If default ./scaled directory does not exist, then create it.
189 cachedFilePath = path2ScaledImagePath(path, w, h, index, exportExtension)
190 cacheDirPath = path2Dir(cachedFilePath) #
191
192 if not os.path.exists(cacheDirPath):
193 os.makedirs(cacheDirPath)
194
195 if exportExtension in BITMAP_TYPES and path != cachedFilePath:
196 try:
197 if force or not os.path.exists(cachedFilePath):
198 if HAS_PIL:
199 im = Image.open(path)
200 im.thumbnail(upt(w, h), Image.ANTIALIAS)
201 im.save(cachedFilePath, exportExtension)
202 except IOError:
203 print("Cannot create resize image '%s'" % path)
204 return cachedFilePath
205
206 def image(self, path, p=None, alpha=1, pageNumber=None, w=None, h=None):
207 """Make an HTML image tag by calling the builder"""

Callers 2

imageMethod · 0.45
_scaleImageMethod · 0.45

Calls 6

path2ExtensionFunction · 0.90
path2ScaledImagePathFunction · 0.90
path2DirFunction · 0.90
uptFunction · 0.90
openMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected