0

I am attempting to compile a folder of images into a single PDF by the order in which they were taken.

os.getcwd()
os.chdir(folder_path)

a= glob.glob(folder_path)
b = [os.path.getctime(i) for i in a]
c = {}
for i,j  in list(zip(a,b)):
    c[i] = j
sorted_c = dict(sorted(c.items(), key=operator.itemgetter(1),reverse=False))
with open("output.pdf", "wb") as f:
    f.write(img2pdf.convert([k for k in sorted_c]))

For some reason

 with open("output.pdf", "wb") as f:
    f.write(img2pdf.convert([k for k in sorted_c]))

keeps throwing TypeError: a bytes-like object is required, not 'str' Alternate methods to accomplish this are also welcome.

Dominic Naimool
  • 313
  • 2
  • 11
  • 1
    Help would come quicker if you actually added the error message to your post, which would at least indicate the line of the error. Which statement gives you the error? – RufusVS Jan 28 '22 at 03:30
  • A wild guess: the error is in `f.write(img2pdf.convert([k for k in sorted_c]))` because `img2pdf.convert()` accepts only files/pdf-like/bytes-like objects as parameters. Is `[k for k in sorted_c]` a `str`/string? Where do you get the error? Please [edit] the question to include necessary details. – The Amateur Coder Jan 28 '22 at 03:34
  • You are correct, the error is in f.write(img2pdf.convert([k for k in sorted_c])) , [k for k in sorted_c] is a list – Dominic Naimool Jan 28 '22 at 03:56
  • @The Amateur Coder: The [docs](https://gitlab.mister-muffin.de/josch/img2pdf) state that it accepts a list of filenames. – azelcer Jan 28 '22 at 04:01
  • @azelcer, oh, but then where could the error occur? Silly me, it's because the file is opened in `"wb"` mode; see [this answer](https://stackoverflow.com/a/33054552/16136190). – The Amateur Coder Jan 28 '22 at 04:17
  • Can you check your python an img2pdf versions? – azelcer Jan 28 '22 at 04:23
  • Why would that be necessary? Did they change anything/the `convert()` method, or are the versions different? – The Amateur Coder Jan 28 '22 at 04:35
  • The mode change does not work, it has been tried :/ – Dominic Naimool Jan 28 '22 at 04:51

0 Answers0