Open luowqn opened 6 years ago
I've shortened your code to get rid of lines that I don't believe are related. Please ensure the error still occurs with this minimal code to reproduce.
I believe you're going to find that your JPEG file is missing some important information, in particular, the horizontal DPI setting. Try using another JPEG file and seeing if the error reoccurs.
In case anyone gets this error... It is "solved" by monkey patching the height and width of the Image class:
from docx.image.image import Image
from docx.shared import Inches
@property
def image_width(self):
if (self.horz_dpi == 0):
return Inches(self.px_width / 72)
return Inches(self.px_width / self.horz_dpi)
@property
def image_height(self):
if (self.vert_dpi == 0):
return Inches(self.px_height / 72)
return Inches(self.px_height / self.vert_dpi)
Image.width = image_width
Image.height = image_height
If don't have a clue to why it is zero in the first place, as the images (in my case) do not appear to have an header, so it should default to 72 dpi.
I think it's a True issue because in readthedoc it says that it should default to 72 DPI.
Thanks! I had the same issue. Jpegs with bad header. Hope this gets fixed in the main branch.
So I couldn't get this monkey patch to work, so I set out to try and fix the actual bug. A bit beyond my pay grade, but I did track it down to this function in tiff.py:
` def _dpi(self, resolution_tag): """ Return the dpi value calculated for resolution_tag, which can be either TIFF_TAG.X_RESOLUTION or TIFF_TAG.Y_RESOLUTION. The calculation is based on the values of both that tag and the TIFF_TAG.RESOLUTION_UNIT tag in this parser's |_IfdEntries| instance. """
ifd_entries = self._ifd_entries
logging.debug(resolution_tag)
if resolution_tag not in ifd_entries:
logging.debug("no tag")
return 72`
That error trap simply isn't going off.. because the photos do have resolution tags. There's something else that's preventing these values from getting passed correctly, but that's why it's not defaulting.
Anyways, I did manage to get it to work simply put putting line 343 of tiff.py in a try/except block and setting a default value of 72 there. I don't even know if this is referring to resolution... but I figured it had something to do with sizing so I started there, and it worked.
Hope that gives a starting point to anyone
In case anyone gets this error... It is "solved" by monkey patching the height and width of the Image class:
from docx.image.image import Image from docx.shared import Inches @property def image_width(self): if (self.horz_dpi == 0): return Inches(self.px_width / 72) return Inches(self.px_width / self.horz_dpi) @property def image_height(self): if (self.vert_dpi == 0): return Inches(self.px_height / 72) return Inches(self.px_height / self.vert_dpi) Image.width = image_width Image.height = image_height
If don't have a clue to why it is zero in the first place, as the images (in my case) do not appear to have an header, so it should default to 72 dpi.
Had the same issue in 2024, the bug still exists.
Just in case, I solved this using Pillow. It's probably a bit overkill but hey, it works.
# before:
picture_stream = BytesIO(picture_content)
run = paragraph.add_run()
run.add_picture(picture_stream) # raises the ZeroDivisionError
# after:
picture_stream = BytesIO(picture_content)
image = Image.open(picture_stream)
clean_stream = BytesIO()
image.save(clean_stream, format=image.format)
clean_stream.seek(0)
run = paragraph.add_run()
run.add_picture(clean_stream)
Hello,I have a question,i want to add a picture to doc Mycode: