2

I am writing a command line diary application in Python, and I want to be able to store images in an encrypted SQLite database as a byte array (using solution here).

Then to show images I was going to load the byte array using PIL.Image.fromarray(...) and display it with PIL.Image.show(...), but I am concerned after reading the documation as it says it temporarily stores the image:

PIL.Image.Show documentation

Obviously this is not desirable as the point of this application is to store elements in an encrypted form and I am worried it will leave these images stored on the disk after the application has exited. But to resolve this worry I can not seem to find where it temporarily stores the image and if this is deleted after. I have checked the /tmp/ directory.

So my question is where does PIL.Image.show(...) temporarily store images and will they be left around on my computer in an unencrypted format after?

Alfie
  • 1,903
  • 4
  • 21
  • 32

1 Answers1

2

After looking at the source of the PIL.Image.show(...) method, the method seems to write the image temporarily to disk (as the documentation stated) and then is immediately erased after it has been opened with an image viewer.

For example, the method for macOS is:

def show_file(self, file, **options):
    """Display given file"""
    fd, path = tempfile.mkstemp()
    with os.fdopen(fd, "w") as f:
        f.write(file)
    with open(path, "r") as f:
        subprocess.Popen(
            ["im=$(cat); open -a Preview.app $im; sleep 20; rm -f $im"],
                shell=True,
                stdin=f,
            )
    os.remove(path)
    return 1

This uses the tempfile.mkstemp([suffix=''[, prefix='tmp'[, dir=None[, text=False]]]]) method to create a temporary file. The documentation states:

If dir is specified, the file will be created in that directory; otherwise, a default directory is used. The default directory is chosen from a platform-dependent list, but the user of the application can control the directory location by setting the TMPDIR, TEMP or TMP environment variables.

As no dir is specified, the default directory used, which is selected like so: (stated here):

When set to a value other than None, this variable defines the default value for the dir argument to all the functions defined in this module.

If tempdir is unset or None at any call to any of the above functions, Python searches a standard list of directories and sets tempdir to the first one which the calling user can create files in. The list is:

  1. The directory named by the TMPDIR environment variable.
  2. The directory named by the TEMP environment variable.
  3. The directory named by the TMP environment variable.
  4. A platform-specific location:
    • On RiscOS, the directory named by the Wimp$ScrapDir environment variable.
    • On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.
    • On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.
  5. As a last resort, the current working directory.
Alfie
  • 1,903
  • 4
  • 21
  • 32