title: Using Pillow and img2pdf to convert images to pdf
body: <p>I have a task that requires me to get data from an image upload (jpg or png), resize it based on the requirement, and then transform it into pdf and then store in s3.</p>
  
  <ol>
  <li>The file comes in as ByteIO</li>
  <li>I have <code>Pillow</code> available so I can resize the image with it</li>
  <li>Now the file type is <code>class 'PIL.Image.Image'</code> and I don't know how to proceed.</li>
  <li>I found <code>img2pdf</code> library on <a href="https://gitlab.mister-muffin.de/josch/img2pdf" rel="nofollow noreferrer">https://gitlab.mister-muffin.de/josch/img2pdf</a>, but I don't know how to use it when I have PIL format (use <code>tobytes()</code>?)</li>
  <li>The s3 upload also looks like a file-like object, so I don't want to save it into a temp file before loading it again. Do I even need img2pdf in this case then?</li>
  </ol>
  
  <p>How do I achieve this goal?</p>
  
  <p>EDIT: I tried using <code>tobytes()</code> and upload to s3 directly. Upload was successful. However, when downloading to see the content, it shows an empty page. It seems like the file data is not written into the pdf file</p>
  
  <p>EDIT 2: Actually went on the s3 and check the file stored. When I download it and open it, it shows <code>cannot be opened</code></p>
  
  <p>EDIT 3: I don't really have working code as I'm still experimenting what could work, but here's a gist</p>
  
  <pre><code>data = request.FILES['file'].file  # where the data is
  im = Image.open(data)
  (width, height) = (im.width // 2, im.height // 2)  # example action I wanna take with Pillow
  data = im_resized.tobytes()
  # potential step for using img2pdf here but I don't know how
  # img2pdf.convert(data)  # this fails because "ImageOpenError: cannot read input image (not jpeg2000). PIL: error reading image: cannot identify image file &lt;_io.BytesIO..."
  # img2pdf.convert(im_resized)  # this also fails because "TypeError: Neither implements read() nor is str or bytes"
  upload_to_s3(data)  # some function that utilizes boto3 to upload to s3
  </code></pre>