ImageSize class

ImageSize class

Contains information about image size and resolution. To learn more, visit the Working with Images documentation article.

Constructors

NameDescription
ImageSize(width_pixels, height_pixels)Initializes width and height to the given values in pixels. Initializes resolution to 96 dpi.
ImageSize(width_pixels, height_pixels, horizontal_resolution, vertical_resolution)Initializes width, height and resolution to the given values.

Properties

NameDescription
height_pixelsGets the height of the image in pixels.
height_pointsGets the height of the image in points. 1 point is 1/72 inch.
horizontal_resolutionGets the horizontal resolution in DPI.
vertical_resolutionGets the vertical resolution in DPI.
width_pixelsGets the width of the image in pixels.
width_pointsGets the width of the image in points. 1 point is 1/72 inch.

Examples

Shows how to resize a shape with an image.

image = drawing.Image.from_file(IMAGE_DIR + 'Logo.jpg')
self.assertEqual(400, image.size.width)
self.assertEqual(400, image.size.height)
# When we insert an image using the "insert_image" method, the builder scales the shape that displays the image so that,
# when we view the document using 100% zoom in Microsoft Word, the shape displays the image in its actual size.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image(IMAGE_DIR + 'Logo.jpg')
# A 400x400 image will create an ImageData object with an image size of 300x300pt.
image_size = shape.image_data.image_size
self.assertEqual(300.0, image_size.width_points)
self.assertEqual(300.0, image_size.height_points)
# If a shape's dimensions match the image data's dimensions,
# then the shape is displaying the image in its original size.
self.assertEqual(300.0, shape.width)
self.assertEqual(300.0, shape.height)
# Reduce the overall size of the shape by 50%.
shape.width *= 0.5
# Scaling factors apply to both the width and the height at the same time to preserve the shape's proportions.
self.assertEqual(150.0, shape.width)
self.assertEqual(150.0, shape.height)
# When we resize the shape, the size of the image data remains the same.
self.assertEqual(300.0, image_size.width_points)
self.assertEqual(300.0, image_size.height_points)
# We can reference the image data dimensions to apply a scaling based on the size of the image.
shape.width = image_size.width_points * 1.1
self.assertEqual(330.0, shape.width)
self.assertEqual(330.0, shape.height)
doc.save(ARTIFACTS_DIR + 'Image.scale_image.docx')

See Also