Scaling issue when inserting jpg image, which was converted from png
See original GitHub issuepython3.4, OSX Yosemite, xlsxwriter 0.7.2
Issue: When inserting a .jpg image into a worksheet, the resulting image in Excel 2011 is scaled incorrectly. I have not tried another version of Excel. Instead of 100%, it is scaled to height: 6720 % and width: 7950 %. If I manually change the image size in Excel back to 100%, then the image is correctly shown.
Note: It would seem that this is not an Excel 2011 issue, because the same image can be inserted into Excel using a ruby gem “axlsx” (see below). Be that as it may, I am limited to python for this project.
Question: If this is not fixable, can you suggest a workaround? Thanks in advance for any insight.
This is the code:
wb = Workbook()
ws = wb.add_worksheet()
ws.insert_image(0,0,'test.jpg')
wb.filename = 'test.xlsx'
wb.close()
I’m attaching 2 versions of an image. Both images have pixel width x height of: 1199 x 790
- The original image (test.png). The issue does not occur with this image file.
- The .jpg image (test.jpg) which was converted to .jpg using ImageMagick. The issue occurs only with the .jpg version of the file.
Additional Note: The issue does not occur when using the ruby gem ‘axlsx’ to do the equivalent function with the same image files in the same environment. The ruby code looks like this:
p = Axlsx::Package.new
wb = p.workbook
ws = wb.add_worksheet
ws.add_image({image_src: 'test.jpg', noSelect: false, noMove: true}) do |image|
image.start_at(0,0)
image.width = 1199
image.height = 790
end
p.serialize('test-ruby.xlsx')
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Thanks so much for that info. I will do some research and try your suggestion of specifying dpi during the image conversion and see if that helps. Preview (a Mac application) thinks both images have a DPI of 72.
I can’t answer your question re the PNG not having any DPI information. That seems odd. Let me look into that too. How are you checking the files for dpi information?
Regardless, I will come back and update this ticket.
The issue seems to be around the DPI of the images. The PNG doesn’t (as far as I can tell) have any DPI information and is treated with a default resolution of 96 by the module. The JPG has a (probably invalid) DPI of 1 and hence is scaled incorrectly.
Any idea why the PNG doesn’t have a resolution? On converting to JPG can you try specifying a DPI such as 72 or 96 and check the results.