text: from PIL import Image
  from pathlib import Path
  import os
  import sys
  import random
  
  
  class Heif2Pdf:
  
      def __init__(self, path):
          """
          Accepts a valid path as a parameter.
          """
          self.path = path
          self.images = []
          self.temp_path = ""
          self.convert_to_jpg()
          self.add_images_to_list()
          self.convert_to_pdf()
          self.remove_temp_directory()
  
      def convert_to_jpg(self):
          """
          Convert .HEIC images to .jpg
          """
          img_list = [img for img in sorted(os.listdir(self.path)) if img.endswith(".HEIC") or img.endswith(".heic")]
          
          if len(img_list) == 0:
              sys.exit("No HEIF images found in the path provided. Make sure the path contains HEIF images.")
              
          self.create_temp_directory()
          img_count = 1
          img_total = len(img_list)
  
          print("Starting conversion process...")
          
          for img_file in img_list:
              print(f"Processing image ({img_count}/{img_total})")
  
              img = self.path/img_file
              os.system(f"magick {img} {self.temp_path}/photo{img_count}.jpg")
              img_count += 1
  
      def create_temp_directory(self):
          """
          Creates temporary directory to store .jpg images
          """
          rand_number = random.randint(1,1000000)
          temp_photo_dir = f"heif2pdf_photos_{rand_number}"
          self.temp_path = os.path.join(self.path, temp_photo_dir)
          os.mkdir(self.temp_path)
  
      def add_images_to_list(self):
          """
          Adds .HEIC images to self.images
          """
          img_list = [img for img in sorted(os.listdir(self.temp_path))]
  
          for img_file in img_list:
              img = os.path.join(self.temp_path, img_file)
              pillow_img = Image.open(img)
              self.images.append(pillow_img)
  
      def convert_to_pdf(self):
          """
          Saves all images in self.images into images.pdf
          """
          print("Creating PDF...")
  
          if len(self.images) == 1:
              self.images[0].save("image.pdf")
          else:
              self.images[0].save("images.pdf", save_all=True, append_images=self.images[1:])
  
      def remove_temp_directory(self):
          os.system(f"rm -rf {self.temp_path}")
          print("PDF successfully created (see image(s).pdf -- located in the directory where you called the program)")
  
  
  def main():
      path = sys.argv[1]
      Heif2Pdf(Path(path))
  
  
  if __name__ == "__main__":
      main()
  