New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XlsxWriter can't find length and width information in some jpeg files #506

Closed
Shaun-Chen opened this Issue Apr 13, 2018 · 9 comments

Comments

2 participants
@Shaun-Chen

Shaun-Chen commented Apr 13, 2018

No description provided.

@Shaun-Chen

This comment has been minimized.

Shaun-Chen commented Apr 13, 2018

HI, I have a picture, the type is JEPG;

import xlsxwriter
image = '1492045796581265623.jpg'
wordbook = xlsxwriter.Workbook('../../test.xlsx')
worksheet = wordbook.add_worksheet()
worksheet.insert_image('A1', image)
wordbook.close()

The error:
Traceback (most recent call last):
File "", line 1, in
File "F:\py3env\lib\site-packages\xlsxwriter\workbook.py", line 310, in close
self._store_workbook()
File "F:\py3env\lib\site-packages\xlsxwriter\workbook.py", line 621, in _store_workbook
self._prepare_drawings()
File "F:\py3env\lib\site-packages\xlsxwriter\workbook.py", line 1064, in _prepare_drawings
self._get_image_properties(filename, image_data)
File "F:\py3env\lib\site-packages\xlsxwriter\workbook.py", line 1172, in _get_image_properties
raise Exception("%s: no size data found in image file." % filename)
Exception: F:\file\main_image_file\romwe\1492045796581265623.jpg: no size data found in image file.

I see the source,the function: workbook._get_image_properties can't get the width and height from the picture.
Why does this happen?
Thank You!

@Shaun-Chen

This comment has been minimized.

Shaun-Chen commented Apr 13, 2018

I can get the size,when I use the Image:

from PIL import Image
img = Image.open(image)
img.size
The result: (423, 563)

@jmcnamara

This comment has been minimized.

Owner

jmcnamara commented Apr 13, 2018

Can you please add the image on github and I will have a look at the issue.

@Shaun-Chen

This comment has been minimized.

Shaun-Chen commented Apr 13, 2018

@Shaun-Chen

This comment has been minimized.

Shaun-Chen commented Apr 13, 2018

by the way:
It is worked, when I modify the source: workbook._get_image_properties

if not height or not width:
    from PIL import Image
    img = Image.open(filename)
    height = img.size[0]
    width = img.size[1]
    # raise Exception("%s: no size data found in image file." % filename)
@jmcnamara

This comment has been minimized.

Owner

jmcnamara commented Apr 13, 2018

Here is a fix for the issue. I'll push up the change after I do a bit more testing:

diff --git a/xlsxwriter/workbook.py b/xlsxwriter/workbook.py
index 57bfa3c..343f6b0 100644
--- a/xlsxwriter/workbook.py
+++ b/xlsxwriter/workbook.py
@@ -1240,14 +1240,14 @@ class Workbook(xmlwriter.XMLwriter):
         y_dpi = 96

         # Search through the image data to read the height and width in the
-        # 0xFFC0/C2 element. Also read the DPI in the 0xFFE0 element.
+        # 0xFFC0/C1/C2 element. Also read the DPI in the 0xFFE0 element.
         while not end_marker and offset < data_length:

             marker = (unpack('>H', data[offset + 0:offset + 2]))[0]
             length = (unpack('>H', data[offset + 2:offset + 4]))[0]

             # Read the image dimensions.
-            if marker == 0xFFC0 or marker == 0xFFC2:
+            if marker == 0xFFC0 or marker == 0xFFC1 or marker == 0xFFC2:
                 height = (unpack('>H', data[offset + 5:offset + 7]))[0]
                 width = (unpack('>H', data[offset + 7:offset + 9]))[0]

@jmcnamara jmcnamara changed the title from Why some pictures cannot obtain length and width information(The picture's type is JPEG) to XlsxWriter can't find length and width information in some jpeg files Apr 13, 2018

@jmcnamara jmcnamara self-assigned this Apr 13, 2018

@Shaun-Chen

This comment has been minimized.

Shaun-Chen commented Apr 13, 2018

OK, Thank You!

@jmcnamara jmcnamara closed this in 1c5a243 Apr 13, 2018

@jmcnamara

This comment has been minimized.

Owner

jmcnamara commented Apr 13, 2018

I've pushed a fix for this to master.

@jmcnamara

This comment has been minimized.

Owner

jmcnamara commented Apr 14, 2018

A fix is now available in XlsxWriter 1.0.4. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment