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

Method _scaleImage

Lib/pagebot/elements/image.py:308–351  ·  view source on GitHub ↗

If the self.saveScaled is True and the reduction scale is inside the range, then create a new cached image file, if it does not already exist. Scaling images in the DrawBot context is a fast operation, so always worthwhile to creating PNG from large export PDF files.

(self, view)

Source from the content-addressed store, hash-verified

306 return alpha
307
308 def _scaleImage(self, view):
309 """If the self.saveScaled is True and the reduction scale is inside the
310 range, then create a new cached image file, if it does not already
311 exist. Scaling images in the DrawBot context is a fast operation, so
312 always worthwhile to creating PNG from large export PDF files.
313
314 In case the source is a PDF, then use self.index to request for the
315 page.
316
317 TODO: Add clipPath and filter as parameters."""
318 if self.path is None or not self.scaleImage:
319 return
320 if not path2Extension(self.path) in BITMAP_TYPES:
321 return
322
323 # Make sure image exists and not zero size, to avoid division.
324 if not self.iw or not self.ih:
325 print('Image.scaleImage: %dx%d zero size for image "%s"' % (self.iw, self.ih, self.path))
326 return
327
328 extension = path2Extension(self.path)
329 resolutionFactor = self.resolutionFactor or self.resolutionFactors.get(extension, 1)
330 # Translate the extension to the related type of output.
331 exportExtension = CACHE_EXTENSIONS.get(extension, extension)
332
333 resW = self.w * resolutionFactor
334 resH = self.h * resolutionFactor
335
336 sx, sy = upt(resW / self.iw, resH / self.ih)
337
338 if self.proportional:
339 sx = sy = max(sx, sy)
340
341 if not self.scaleImage and self.cacheScaledImageFactor <= sx and self.cacheScaledImageFactor <= sy:
342 # If no real scale reduction, then skip. Never enlarge.
343 return
344
345 # Scales the image the cache does not exist already. A new self.path is
346 # saved for the scaled image file at self.srcPath. Resets (self.iw, self.ih).
347 self.path = self.context.scaleImage(
348 path=self.srcPath.lower(), w=resW, h=resH, index=self.index,
349 showImageLoresMarker=self.showImageLoresMarker or view.showImageLoresMarker,
350 exportExtension=exportExtension
351 )
352
353 def prepare_html(self, view):
354 """Responds to the top-down element broadcast to prepare for build_html.

Callers 2

prepare_htmlMethod · 0.95
prepareMethod · 0.95

Calls 4

path2ExtensionFunction · 0.90
uptFunction · 0.90
getMethod · 0.45
scaleImageMethod · 0.45

Tested by

no test coverage detected