Home > Software design >  img2pdf not converting png to bytes
img2pdf not converting png to bytes

Time:05-07

I have a script that turns my images into .pdf formats and assigns these their respective article name and author. I have two separate files for two different authors, the script works fine for one of the two, when it's not working I get the following error:

TypeError: a bytes-like object is required, not 'str'

The code that I used:

img2pdf.convert(car_test['images_path'][0])

This has the following files:

['/Users/dollar/Car_Files-copy/3A31735061848903-0001.png',
 '/Users/dollar/Car_Files-copy/3A31735061848903-0002.png',
 '/Users/dollar/Car_Files-copy/3A31735061848903-0003.png']

However, when I use the same code on my other set of files:

['/Users/dollar/Jeff_Files-copy-2/3A31735062223130-0001.png',
 '/Users/dollar/Jeff_Files-copy-2/3A31735062223130-0002.png']

It works fine. Any idea why it's not working?

Additional notes:

  1. They're located in separate files and the path to each of them is correct.

Full traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/dr/9wh_z8y10fl79chj86pq7knc0000gn/T/ipykernel_828/3317035330.py in <module>
      1 for k, j in zip(car_test['path_to_pdf'],car_test['images_path']):
      2     with open(k, "wb") as f:
----> 3         f.write(img2pdf.convert(j))

~/opt/anaconda3/lib/python3.8/site-packages/img2pdf.py in convert(*images, **kwargs)
   2261             rotation,
   2262             iccp,
-> 2263         ) in read_images(
   2264             rawdata,
   2265             kwargs["colorspace"],

~/opt/anaconda3/lib/python3.8/site-packages/img2pdf.py in read_images(rawdata, colorspace, first_frame_only, rot)
   1442 
   1443 def read_images(rawdata, colorspace, first_frame_only=False, rot=None):
-> 1444     im = BytesIO(rawdata)
   1445     im.seek(0)
   1446     imgdata = None

TypeError: a bytes-like object is required, not 'str'

CodePudding user response:

It would appear the file simply doesn't exist.

If trying to open a file with that name fails for whatever reason, img2pdf instead assumes the given variable is not a filename but raw image data (as bytes).

This means that when the file doesn't exist (or was locked, access was denied or it failed to open for whatever other reason), it will then attempt to treat the filename string as image data, causing the error you see.

If you are sure the file exists and you can't figure out why it failed to open so you want to see the exception that img2pdf swallows here, you can also attempt to open the file yourself and pass the file descriptor to img2pdf instead, which is also supported. This way, you will be able to see the exact exception that happened when attempting to open the file.

  • Related