Skip to content
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

add_picture #187

Open
davelawrence3d opened this issue Jun 15, 2015 · 14 comments · May be fixed by #1451
Open

add_picture #187

davelawrence3d opened this issue Jun 15, 2015 · 14 comments · May be fixed by #1451
Labels

Comments

@davelawrence3d
Copy link

I am new to python and I am having issues witth add_picture
my code is simple

from docx import Document
from docx.shared import Inches
document = Document()
document.add_heading('Document Title', 0)
document.add_picture('IMG_2063.jpg')
document.add_page_break()
document.save('demo.docx')

for some reason it was working and now i get a long string of errors. i cant seem to see what i am doing wrong. if nothting do i need to reinstall?

here is the error
E:\calculations\temp>doc2.py
Traceback (most recent call last):
File "E:\calculations\temp\doc2.py", line 18, in
document.add_picture('IMG_2063.jpg')
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\document.py", line 79, in add_picture
return run.add_picture(image_path_or_stream, width, height)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\text\run.py", line 62, in add_picture
inline = self.part.new_pic_inline(image_path_or_stream, width, height)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\parts\document.py", line 91, in new_pic_inline
rId, image = self.get_or_add_image(image_descriptor)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\parts\document.py", line 54, in get_or_add_image
image_descriptor
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\package.py", line 76, in get_or_add_image_part
image = Image.from_file(image_descriptor)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\image.py", line 55, in from_file
return cls._from_stream(stream, blob, filename)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\image.py", line 176, in _from_stream
image_header = _ImageHeaderFactory(stream)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\image.py", line 198, in _ImageHeaderFactory
return cls.from_stream(stream)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 47, in from_stream
markers = _JfifMarkers.from_stream(stream)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 111, in from_stream
for marker in marker_parser.iter_markers():
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 177, in iter_markers
marker_code, self._stream, segment_offset
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 271, in _MarkerFactory
return marker_cls.from_stream(stream, marker_code, offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 413, in from_stream
tiff = cls._tiff_from_exif_segment(stream, offset, segment_length)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 455, in _tiff_from_exif_segment
return Tiff.from_stream(substream)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 36, in from_stream
parser = _TiffParser.parse(stream)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 63, in parse
ifd_entries = _IfdEntries.from_stream(stream_rdr, ifd0_offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 176, in from_stream
entries = dict((e.tag, e.value) for e in ifd_parser.iter_entries())
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 176, in
entries = dict((e.tag, e.value) for e in ifd_parser.iter_entries())
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 204, in iter_entries
ifd_entry = _IfdEntryFactory(self._stream_rdr, dir_entry_offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 231, in _IfdEntryFactory
return entry_cls.from_stream(stream_rdr, offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 256, in from_stream
stream_rdr, offset, value_count, value_offset
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 294, in _parse_value
return stream_rdr.read_str(value_count-1, value_offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\helpers.py", line 70, in read_str
chars = self._unpack_item(struct, base, offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\helpers.py", line 96, in unpack_item
bytes
= self._read_bytes(struct.size, base, offset)
File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\helpers.py", line 88, in _read_bytes
raise UnexpectedEndOfFileError
docx.image.exceptions.UnexpectedEndOfFileError

@scanny
Copy link
Contributor

scanny commented Jun 15, 2015

I think there are issues with some JPEG-EXIF images, one was reported recently.

Does it work with some JPEGs and not others?

There are some test images you can try here:
https://github.com/python-openxml/python-docx/tree/master/tests/test_files

@davelawrence3d
Copy link
Author

it appears to work with most of the sample files you sent the WMF errored but all the rest worked. what is in my file that is adding the error. it is a simple picture from an iPhone. is there something I need to strip out? I am trying to connect the document output to a Arc py script so I would prefer not to lose the location information.
img_2065

@davelawrence3d
Copy link
Author

it looks like the error is only present on images from IOS, i saw the thread on the differences in the exif data, but is there a way to modify the exif so that it reads correctly? or are there any plans to update the code to read the ios 8.3 images.

@scanny
Copy link
Contributor

scanny commented Jun 23, 2015

I'm open to a patch if someone wants to dig in there and see what it would take. Unfortunately I'm tied up with other projects now and don't plan to be in there for a while.

@scanny scanny added the image label May 26, 2016
@Cabchinoe
Copy link

I think there are problems when tiff.py reads the bytes of a jpeg type photo taken by iOS system, it may caused by an incorrect way of counting exif messages length, thus the offset can not be correct, I guess.
_330227199507226817_b

buchi added a commit to 4teamwork/docxcompose that referenced this issue Sep 13, 2017
The image parser of docx doesn't work with some exif data
python-openxml/python-docx#187
We have already all needed data in the image part, thus we don't need
to parse the image data
@darkcircle
Copy link

I have met the same error, is it considered to be fixed?

@wensylviasun
Copy link

I've having the same issue with a .JPG file. I'm on latest version 0.8.10.

Has there been a fix?

@sachinrevvsales
Copy link

Same error here. Is there a fix?

@dragomej
Copy link

For those who are still looking for solution - use Fotosizer Standard Edition. there is option under Miscellaneous Settings -> Image Metadata and Dates -> uncheck all boxes and save/convert images. You don't need to resize. There is option for orginal size on top. For me that fixed docx.image.exceptions.UnexpectedEndOfFileError

@imreverse
Copy link

Also, if you're on linux, you can try to convert jpg to png using imagemagik's convert command
Example : convert hello.jpg hello.png

Swind added a commit to Disfactory/Disfactory that referenced this issue Sep 12, 2020
python-docx can't parse some jpeg files correctly, so we use PIL
to format all jpeg files to workaround this issue when generate docx
file.
Swind added a commit to Disfactory/Disfactory that referenced this issue Sep 12, 2020
python-docx can't parse some jpeg files correctly, so we use PIL
to format all jpeg files to workaround this issue when generating docx
file.
stegben added a commit to Disfactory/Disfactory that referenced this issue Sep 12, 2020
@Zidingyi
Copy link

在百度贴吧找到了问题原因和解决方法:
问题是出现在图片上面,图片中包含的某些exif信息会导致add_picture()方法出现读取问题。解决方法有两个:
1.删除图片中的属性和个人信息。图片属性——>详细信息选项卡——>左下角删除属性和个人信息——>从此文件中删除以下属性——>全选
2.方法1不适合批量执行,批量处理建议使用PIL模块,PIL模块在存取文件时会丢失掉部分exif信息,这些信息恰好就是影响我们的add_picture()方法的信息。
所以第二个方法就是在代码中加入
from PIL import Image
f = Image.open('C:XXXXXX\pic.jpg')#你的图片文件
f.save('C:XXXXXX\pic.jpg')#替换掉你的图片文件
f.close()
这样处理完之后再用add_picture()读取就不会出现问题了。当然这个方法也有缺陷,因为这里是用新图片覆盖掉旧图片,所以不想丢失含有exif信息的旧文件的话,最好先备份一下。

原贴地址:https://tieba.baidu.com/p/6409886653?red_tag=1219468420

@morfair
Copy link

morfair commented May 21, 2021

Oh my god...

@abubelinha
Copy link

abubelinha commented May 22, 2021

Almost 6 years later, I am getting the same error message
(I use Python 3.8, Windows 7, 64bit, and also Python 3.4 WinXP 32bit, with identical error)

BTW, I am using images produced by a scanner attached to another Win7 32bit PC.
So this issue is not limited only to iOS-produced images as @davelawrence3d and @Cabchinoe suggested above.

@TheZ3ro TheZ3ro linked a pull request Nov 29, 2024 that will close this issue
@TheZ3ro
Copy link

TheZ3ro commented Nov 29, 2024

This 9 years old issue is now fixed in #1451 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.