Images not added with original size

Issue #543 resolved
Johan Abbors
created an issue

Hi,

Images added to worksheet are "zoomed-in" and image size is reduced to about 70% of the original size. However, if I open the document and "reset" the image size it will get the correct size and zoom level.

Comments (13)

  1. Johan Abbors reporter
    import openpyxl
    
    wb = openpyxl.Workbook()
    ws = wb.active
    
    img = openpyxl.drawing.image.Image('logoBitBucketPNG.png')
    ws.add_image(img, 'A1')
    
    wb.save("TestInsertImage.xlsx")
    
  2. Charlie Clark

    I don't see any zooming (it's a large image suitable for different dpi devices) happening but the image has been cropped.

    The code for adding images was initially reverse engineered so it's not clear which behaviour has been added. 2.3 uses a much more deterministic approach but as the code for placing the image within the drawing is based on that for charts, it's possible that we've missed an important element and Excel is using defaults. Or we're simply off on the pixel to EMU calculations.

  3. Johan Abbors reporter

    Cropped images is what I see also. Zooming was the only thing that came to mind when I create this issue.

    If I find some spare time I can start digging into the source code and see if can fix it myself and create a PR. We would like to add images to the reports we automatically generate but it's not that important and all images will be left out until it's fixed.

  4. Charlie Clark

    That would be much appreciated. With high-resolution images it's common to enforce some kind of scaling for displays so things print nicely.

    The image size is 868 x 250 which equates to 8267700 x 2381250 in EMU, assuming the calculations in openpyxl.utils.units are correct (no idea). Excel itself comes up with 11023600 x 3175000, but it's using a slightly different positioning system, anchoring to two cells (A1 to J16 with offsets) and applying a transformation.

    The DrawingML specification is incredibly cryptic. 2.3 now contains most of the relevant classes so it's possible to try out different combinations.

  5. Charlie Clark

    hm, just tried the image using the sizing from Excel and it is no longer cropped so it looks like the pixels_to_EMU logic is off. The utils code probably needs revisiting based on the specification.

  6. Charlie Clark

    Okay. I think I've got this one solved. You're also using MacOS, right?

    Unfortunately, Excel makes fairly liberal use of system settings which means lots of things look different on different OS: check the settings for column widths for example. Converting from pixels to EMUs is another one of these instances. MacOS uses 72dpi but Windows, which we use as the default, uses 96 dpi. This explains the difference in calculation and also highlights the nonsense of the coordinate system is.

    If you open the file on Windows then the image should no longer be cropped (at least it isn't in Excel 2016 in my Windows 7 VM). There seems, however, to be a reasonable solution to this: stick with 96dpi and add a constraint to force the image to "fill" the container.

    Can you work with a checkout?

  7. Johan Abbors reporter

    Thanks for fixing.

    Yes, I use MacOS and I can work with checkouts.

    Btw, the image I used in this sample is not the real image we will be using the the generated docs, but we noticed the same crop effect with all images we tested.

  8. Charlie Clark

    Have you got it working locally? It will affect all images because they're being placed into a box 3/4 the image size with cropping the default behaviour. Though how you're supposed to work this out from the specification is a mystery to me.

  9. Johan Abbors reporter

    Yes, I got it working and noticed the image size was 3/4 but not cropped. We can deal with that.

    Yeah, reading the specification can sometimes be a mystery.

  10. Charlie Clark

    Well, you can have it either "Windows" sized or cropped. Relying on the DPI of the originating system, which is currently the case, and cropping not scaling by default, is insane. As, in my opinion, is defaulting to a two-cell anchor for images.

  11. Log in to comment