An image part. An image part generally has a partname matching the regex `ppt/media/image[1-9][0-9]*.*`.
| 20 | |
| 21 | |
| 22 | class ImagePart(Part): |
| 23 | """An image part. |
| 24 | |
| 25 | An image part generally has a partname matching the regex `ppt/media/image[1-9][0-9]*.*`. |
| 26 | """ |
| 27 | |
| 28 | def __init__( |
| 29 | self, |
| 30 | partname: PackURI, |
| 31 | content_type: str, |
| 32 | package: Package, |
| 33 | blob: bytes, |
| 34 | filename: str | None = None, |
| 35 | ): |
| 36 | super(ImagePart, self).__init__(partname, content_type, package, blob) |
| 37 | self._blob = blob |
| 38 | self._filename = filename |
| 39 | |
| 40 | @classmethod |
| 41 | def new(cls, package: Package, image: Image) -> ImagePart: |
| 42 | """Return new |ImagePart| instance containing `image`. |
| 43 | |
| 44 | `image` is an |Image| object. |
| 45 | """ |
| 46 | return cls( |
| 47 | package.next_image_partname(image.ext), |
| 48 | image.content_type, |
| 49 | package, |
| 50 | image.blob, |
| 51 | image.filename, |
| 52 | ) |
| 53 | |
| 54 | @property |
| 55 | def desc(self) -> str: |
| 56 | """The filename associated with this image. |
| 57 | |
| 58 | Either the filename of the original image or a generic name of the form `image.ext` where |
| 59 | `ext` is appropriate to the image file format, e.g. `'jpg'`. An image created using a path |
| 60 | will have that filename; one created with a file-like object will have a generic name. |
| 61 | """ |
| 62 | # -- return generic filename if original filename is unknown -- |
| 63 | if self._filename is None: |
| 64 | return f"image.{self.ext}" |
| 65 | return self._filename |
| 66 | |
| 67 | @property |
| 68 | def ext(self) -> str: |
| 69 | """File-name extension for this image e.g. `'png'`.""" |
| 70 | return self.partname.ext |
| 71 | |
| 72 | @property |
| 73 | def image(self) -> Image: |
| 74 | """An |Image| object containing the image in this image part. |
| 75 | |
| 76 | Note this is a `pptx.image.Image` object, not a PIL Image. |
| 77 | """ |
| 78 | return Image(self._blob, self.desc) |
| 79 |
no outgoing calls
searching dependent graphs…