add_image does not respect DPI-setting of images

Issue #1121 new
G. Wiora
created an issue

When adding an image to an excel sheet it is always displayed with the default resolution. Width and height properties are set to the number of pixels respectively. That means it is not displayed with its print size but with its pixel size.

It would be better if the DPI setting in the image file would be used to adapt the display size of the image accordingly. The following formulas should solve the problem:

  wb.active._images[0].width = xpixel * defaultXDPI / imageXDPI
  wb.active._images[0].height = ypixel * defaultYDPI / imageYDPI

Comments (5)

  1. G. Wiora reporter

    Maybe it is not documented yet, but I did not find a public element allowing scaling, I had to use the private ._images[] array of the worksheet. Is there a better way to access this?

  2. CharlieC

    The documentation is really in the OOXML but basically you pass in the anchor when you add the image. As DPI is an output device specification and not in part of the image information, the actual size will depend on the application viewing the image.

  3. G. Wiora reporter

    Sorry, I do not see how the anchor can scale the print/display size of the image object in excel. It is just a cell address like "A1". And the cell object does not specify the display size of the image. How should I specify this?

    It is true that DPI is an output device specifications. But all image file formats like PNG contain a "DPI" information meaning the resolution of the image. If you match image file DPI and display DPI you get equal display sizes on all devices. That is the basic idea of DPI information.

  4. CharlieC

    Whether the file formats allow for DPI hints or not does not mean they're present in every file and even less if the library reading the file supports it: Pillow doesn't seem to. At least not with any of the bitmaps I have. It would, therefore, be naive to want to rely on it.

    You can pass in either a coordinate or a fully-fledged anchor. Information on using anchors is part of the OOXML specification. It would be great if someone could work their way through this and improve the documentation.

  5. Log in to comment