NameError: global name 'im' is not defined #61

Closed
opened 2021-04-25 19:58:25 +00:00 by josch · 0 comments
Owner

By allan on 2019-03-28T08:44:18.885Z

  File "dist-packages/img2pdf.py", line 1829, in convert
    ) in read_images(rawdata, kwargs["colorspace"], kwargs["first_frame_only"]):
  File "dist-packages/img2pdf.py", line 1191, in read_images
    imgdata, imgformat, default_dpi, colorspace, rawdata
  File "dist-packages/img2pdf.py", line 1030, in get_imgmetadata
    'Image "%s": invalid rotation (%d)' % (im.name, value)

I'm using the latest version from pip , I will try to see if I can get the original image


By josch on 2019-03-28T08:57:09.240Z


Status changed to closed by commit 07903e9ef1


By josch on 2019-03-28T08:57:23.882Z


No need, this issue was simple enough. Thanks! :)


By allan on 2019-03-28T08:58:40.009Z


if I replace by imgdata then i got

    'Image "%s": invalid rotation (%d)' % (imgdata.name, value)                                                                                                                                                                              
AttributeError: 'JpegImageFile' object has no attribute 'name'   

By allan on 2019-03-28T08:59:08.271Z


damn that was fast :)


By allan on 2019-03-28T09:00:01.434Z


ok if i apply the patch locally, I now got

Exception: Image : invalid rotation (0)                                                                                                                                                                                                      

By allan on 2019-03-28T09:00:58.143Z


(if you could tag a bufix version that would be awesome )


By josch on 2019-03-28T09:02:13.748Z


Yes, that is the problem. "0" is not a valid exif orientation. Which program produced your image?


By allan on 2019-03-28T09:04:21.307Z


I will try to get this information, but basically we have a webservice that allow people to upload some documents as jpg/png, and we convert them to pdf , so we're not really under control of the software.


By josch on 2019-03-28T09:07:13.768Z


How about you add a preprocessing step then which removes any invalid exif orientation tags?

If you get arbitrary input you need such a sanitation step anyways. Not the least because img2pdf will refuse to convert images with an alpha channel.


By allan on 2019-03-28T09:11:39.284Z


yes I already have some code for the alpha channel :)

so here for exif, you would advice me to default on one exif orientation if no good value are found?

Can we make that by you throwing a "InvalidExiftOrientation" , so that user code know when to run this kind of "cleaning code" ?
(so that not to have to duplicate your checking code)


By josch on 2019-03-28T09:14:29.982Z


Are you using img2pdf as an application or as a library?

Which exif orientation you pick is totally up to you and your use case. :) I guess if the exif orientation value is invalid, I would just delete the whole tag altogether.

Yes, I could add an InvalidExiftOrientation exception. There is no such exception for the alpha channel error yet. Would you appreciate one there as well?


By allan on 2019-03-28T09:17:02.010Z


as a library

for the exif orientation, yes I didn't know if removing it all together would have please img2pdf ^^

yes for alphachannelerror, any kind of specific exception for which as a user of your library I can try to "do some stuff to prevent the exception from re-happening" is good to have as a specific exception


By josch on 2019-03-28T09:29:02.188Z


Okay, fixed in git master. But before doing a new release I was planning to fix issue #59.


By Shaib8qu on 2020-01-22T11:49:13.131Z


I don't know whether it is the same thing. I use img2pdf as an app, not as a library. I got

 ERROR:root:error: name 'im' is not defined

when converting a jpg file. After some tries, I observe that it appears when I convert photos captured by OpenCamera with HDR.

I am not sure whether it is the same issue as this one, but after googling the keyword img2pdf im not defined, I find this issue.
(Now I temporarily use tesseract to do the lossless conversion with success)


By josch on 2020-01-22T19:55:55.649Z


@uM8Ma8fa To say anything to your issue I need more information. Which version of img2pdf are you running? And can you supply me with an input image?


By Shaib8qu on 2020-01-22T21:46:02.544Z


% img2pdf --version
img2pdf 0.3.3
% exiftool -TAG -Orientation foo.jpg
Orientation                     : Unknown (0)

By josch on 2020-01-22T21:57:35.150Z


You were correct. The issue you see is the same that @allan saw. Essentially your jpeg contains an invalid exif rotation value, namly "0". Only values 1 to 8 are valid. You should file a bug with whichever software created that image.

*By allan on 2019-03-28T08:44:18.885Z* ```python File "dist-packages/img2pdf.py", line 1829, in convert ) in read_images(rawdata, kwargs["colorspace"], kwargs["first_frame_only"]): File "dist-packages/img2pdf.py", line 1191, in read_images imgdata, imgformat, default_dpi, colorspace, rawdata File "dist-packages/img2pdf.py", line 1030, in get_imgmetadata 'Image "%s": invalid rotation (%d)' % (im.name, value) ``` I'm using the latest version from pip , I will try to see if I can get the original image --- *By josch on 2019-03-28T08:57:09.240Z* --- Status changed to closed by commit 07903e9ef196e66b747aea12e0fb9d3012ea8cad --- *By josch on 2019-03-28T08:57:23.882Z* --- No need, this issue was simple enough. Thanks! :) --- *By allan on 2019-03-28T08:58:40.009Z* --- if I replace by imgdata then i got ``` 'Image "%s": invalid rotation (%d)' % (imgdata.name, value) AttributeError: 'JpegImageFile' object has no attribute 'name' ``` --- *By allan on 2019-03-28T08:59:08.271Z* --- damn that was fast :) --- *By allan on 2019-03-28T09:00:01.434Z* --- ok if i apply the patch locally, I now got ``` Exception: Image : invalid rotation (0) ``` --- *By allan on 2019-03-28T09:00:58.143Z* --- (if you could tag a bufix version that would be awesome ) --- *By josch on 2019-03-28T09:02:13.748Z* --- Yes, that is the problem. "0" is not a valid exif orientation. Which program produced your image? --- *By allan on 2019-03-28T09:04:21.307Z* --- I will try to get this information, but basically we have a webservice that allow people to upload some documents as jpg/png, and we convert them to pdf , so we're not really under control of the software. --- *By josch on 2019-03-28T09:07:13.768Z* --- How about you add a preprocessing step then which removes any invalid exif orientation tags? If you get arbitrary input you need such a sanitation step anyways. Not the least because img2pdf will refuse to convert images with an alpha channel. --- *By allan on 2019-03-28T09:11:39.284Z* --- yes I already have some code for the alpha channel :) so here for exif, you would advice me to default on one exif orientation if no good value are found? Can we make that by you throwing a "InvalidExiftOrientation" , so that user code know when to run this kind of "cleaning code" ? (so that not to have to duplicate your checking code) --- *By josch on 2019-03-28T09:14:29.982Z* --- Are you using img2pdf as an application or as a library? Which exif orientation you pick is totally up to you and your use case. :) I guess if the exif orientation value is invalid, I would just delete the whole tag altogether. Yes, I could add an InvalidExiftOrientation exception. There is no such exception for the alpha channel error yet. Would you appreciate one there as well? --- *By allan on 2019-03-28T09:17:02.010Z* --- as a library for the exif orientation, yes I didn't know if removing it all together would have please img2pdf ^^ yes for alphachannelerror, any kind of specific exception for which as a user of your library I can try to "do some stuff to prevent the exception from re-happening" is good to have as a specific exception --- *By josch on 2019-03-28T09:29:02.188Z* --- Okay, fixed in git master. But before doing a new release I was planning to fix issue #59. --- *By Shaib8qu on 2020-01-22T11:49:13.131Z* --- I don't know whether it is the same thing. I use `img2pdf` as an app, not as a library. I got ``` ERROR:root:error: name 'im' is not defined ``` when converting a jpg file. After some tries, I observe that it appears when I convert photos captured by `OpenCamera` with HDR. I am not sure whether it is the same issue as this one, but after googling the keyword `img2pdf im not defined`, I find this issue. (Now I temporarily use `tesseract` to do the lossless conversion with success) --- *By josch on 2020-01-22T19:55:55.649Z* --- @uM8Ma8fa To say anything to your issue I need more information. Which version of img2pdf are you running? And can you supply me with an input image? --- *By Shaib8qu on 2020-01-22T21:46:02.544Z* --- ``` % img2pdf --version img2pdf 0.3.3 ``` ``` % exiftool -TAG -Orientation foo.jpg Orientation : Unknown (0) ``` --- *By josch on 2020-01-22T21:57:35.150Z* --- You were correct. The issue you see is the same that @allan saw. Essentially your jpeg contains an invalid exif rotation value, namly "0". Only values 1 to 8 are valid. You should file a bug with whichever software created that image.
josch closed this issue 2021-04-25 19:58:27 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#61
No description provided.