0

I have created an app that runs with PyGame, which loads a page of a PDF, creates a Pixmap with getPixmap and saves into a jpg file. Then, the image is displayed with pygame.image.load().

The program works perfectly fine, but it requires to save a .jpg file in the folder, which substitutes and therefore deletes the previous .jpg file. When converting it into an exe and distributing the program, the users get a security message error, saying that the program does not have the permission to delete the .jpg file.

How could I skip the part of saving the photo in the folder, and directly showing from the PDF into pygame?

I think it can be done by creating a file-like object with Bitmap.IO(), but I don't know how to use it. I'm relatively new in python, so I would appreciate if you could write the lines of code you would use.

zoom = 2
doc = fitz.open(pdf_path)
mat = fitz.Matrix(zoom, zoom)
page = doc.loadPage(photo_num)
pix = page.getPixmap(matrix = mat)
photo_output = "photo.jpg" 
pix.writePNG(photo_output) 
load_photo = pygame.image.load(photo_output) 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • If `pygame.image` can load from an open file object, that's a good plan, but you can also use the `tempfile` module to generate a unique temporary file name that's always going to be writable. – Tim Roberts Feb 13 '22 at 19:40
  • In the future it would be good to include the names of modules you're using. It took me a couple searches, but you're using the PyMuPDF module, I assume. According to their docs `writePNG` is deprecated, it's preferred to use `save` now. But what you probably want is `tobytes(output="png")` and then feed those bytes into pygame.image.load like `pygame.image.load(the_bytes, "png")`. I can't test that myself because import fitz crashes itself on my computer (some DLopen thing). – Starbuck5 Feb 14 '22 at 02:14

0 Answers0