question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scaling issue when inserting jpg image, which was converted from png

See original GitHub issue

python3.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:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
willjs12commented, May 14, 2015

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.

1reaction
jmcnamaracommented, May 14, 2015

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoCAD: Insert and Scale an Image to Trace - YouTube
AutoCAD Video #2 for Digital Technology Class at UMass. Ex 3: Inserting and Scaling a Raster ( JPG ) Image to be traced...
Read more >
To properly scale an image or PDF after inserting into AutoCAD
Try one or more of the following to attempt to properly scale the image to the appropriate dimension: Using the ALIGN (Command):. Draw...
Read more >
Useful Tips To Fix 5 Common AutoCAD JPEG Problems
PROBLEM 1: JPG image disappears on reopening AutoCAD file. ... You can convert AutoCAD DWG file to PNG using PNGOUT command.
Read more >
PNG and JPG quality loss inserting webpage screenshot into ...
Text in a png or jpg file always becomes distorted if you scale the image. · Ahh. · I got rid of the...
Read more >
Blurry Images & JPG vs. PNG | PicMonkey Help and Support
Your image may appear blurry due to a compression issue. Whenever you resize an image, text or graphic, you are also shrinking and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found