2

I'm trying to convert a jpg to pdf with img2pdf.

It works with most jpg's but not all.

Here is my script:

import img2pdf
import PIL.Image
import os
image = PIL.Image.open("Lidl.jpg")
pdf_bytes = img2pdf.convert(image.filename)
file = open(pdf_path, "wb")
file.write("file.pdf")
image.close()
file.close()

Here is the error I get:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    pdf_bytes = img2pdf.convert(image.filename)
  File "/home/ksb/Dropbox/Python/imap/venv/lib/python3.6/site-packages/img2pdf.py", line 1829, in convert
    ) in read_images(rawdata, kwargs["colorspace"], kwargs["first_frame_only"]):
  File "/home/ksb/Dropbox/Python/imap/venv/lib/python3.6/site-packages/img2pdf.py", line 1191, in read_images
    imgdata, imgformat, default_dpi, colorspace, rawdata
  File "/home/ksb/Dropbox/Python/imap/venv/lib/python3.6/site-packages/img2pdf.py", line 1030, in get_imgmetadata
    'Image "%s": invalid rotation (%d)' % (im.name, value)
NameError: name 'im' is not defined

If I look into the image meta data it says:

Unknown rotation value 0
ColorSpace=sRGB

Is is possible to set the rotation value?

Any hints are very much appreciated.

BR Kresten

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kresten
  • 810
  • 13
  • 36

3 Answers3

0

I guess the problem is with below code line.

file.write("file.pdf")

Here try passing the bytes you got from convert function.

file.write(pdf_bytes)

For the error you are getting you may try as said below. Try using library imdirect

from PIL import Image
import imdirect
img = Image.open('Lidl.jpg')
img_rotated = imdirect.autorotate(img)

The above code snippet is copied from project description page of the library. Also read the quick description, to know why such problems occur.

https://pypi.org/project/imdirect/

If still not working, as a part of last try, rotate the image after opening and before converting into bytes, with the desired angle that satisfies your requirement.

image = PIL.Image.open("Lidl.jpg")
image.rotate(90) #90 is rough number, you calculate, what you need.
pdf_bytes = img2pdf.convert(image.filename)
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • Try the updated answer. Let me know if still not working. I will remove the above answer, as I did not try. – Pavan Chandaka Feb 24 '20 at 21:55
  • If I do this img = Image.open('image.jpg') print(img._getexif().get(274)) img_rotated = imdirect.autorotate(img) print(img._getexif().get(274)) I get the same print and still the same error. Opening the file in an image program, rotating and saving solves it, but how is that done with imdirect? autorotate doen't – Kresten Feb 24 '20 at 22:24
  • Actually that should work....But here is the worst approach... call image.rotate(..) with the desired angle, before calling convert. If this works, I will add it as a worst approach at the end of the answer – Pavan Chandaka Feb 24 '20 at 22:44
  • I can't make it work. I'll try again this afternoon. – Kresten Feb 25 '20 at 06:46
0
from PIL import Image

image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.png')
image2 = Image.open(r'C:\Users\Ron\Desktop\Test\image2.png')
image3 = Image.open(r'C:\Users\Ron\Desktop\Test\image3.png')
image4 = Image.open(r'C:\Users\Ron\Desktop\Test\image4.png')
 
im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')
im4 = image4.convert('RGB')

imagelist = [im2,im3,im4]

im1.save(r'C:\Users\Ron\Desktop\Test\myImages.pdf',save_all=True, append_images=imagelist)

It is work for me

  • 1
    It would be nice if you provided a description of ypur code. – Ruli Jan 21 '21 at 23:28
  • It is full code for example. You may create an own function using my code – Юрий Такаев Jan 23 '21 at 07:36
  • Description on stack overflow stands for "tell us how the code works". It is necessary to provide description about how the code works and why it solves the problem for sake of understanding by other users. A description to code always increases the quality of posts. – Ruli Jan 23 '21 at 07:41
0

In documentation check bugs list https://github.com/josch/img2pdf#bugs and you have example from here https://github.com/josch/img2pdf#library how to fix that:

# ignore invalid rotation values in the input images
with open("name.pdf","wb") as f:
    f.write(img2pdf.convert('test.jpg'), rotation=img2pdf.Rotation.ifvalid)

therefore

import img2pdf
import PIL.Image
import os
image = PIL.Image.open("Lidl.jpg")
with open("file.pdf","wb") as f:
    f.write(img2pdf.convert(image.filename), rotation=img2pdf.Rotation.ifvalid)
fearis
  • 424
  • 3
  • 15