1

So I'm playing around with some code and I'm trying to design a program that displays an image if a true statement results from the input. An example would be as follows:

name = input('name: ')
if name == 'Sammi':
   # Here is where I would put the command to open an image. In place of print or whatever.
   # I need some help with exactly what function to put here though.

The idea is that if the input string matches 'Sammi', then an image of me will be displayed preferably in a separate window, but I'm not exactly sure if that's possible or practical.

I've seen some guides that use PIL but the process of downloading and installing the required software is really tedious and I just have to wonder if it is actually required. I primarily use PyCharm for my code and occasionally ill go over to Notepad++ but it's mostly PyCharm. I'm not sure if that information is helpful but I thought I would provide it.

The image I want to use is located on my desktop and ideally would use the path C:\users\sammi\OneDrive\Desktop\B&W_2.jpg

My question as stated before is: exactly what function will allow me to do this? When you answer I would also really appreciate it if you explain the purpose of certain operators like fromand or for example, or whatever other operators or functions are used. I'm still fairly new to this stuff but I want to get really good at it.

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
  • What do you mean by "a separate window"? How are you running your code? Is this a command line app that you run in a terminal such as cmd or bash? If so, creating a window IS tedious and requires a lot of work. – Code-Apprentice Mar 29 '22 at 22:25
  • 2
    The only built-in way to display an image is via Tkinter, but that's a fair amount of code to get from nothing to a visible image. PIL/Pillow would be a far simpler choice - is typing `pip install Pillow` at a command prompt really that difficult? – jasonharper Mar 29 '22 at 22:27
  • @jasonharper "is typing pip install Pillow at a command prompt really that difficult?" Depending on the OS, yes, it can be. On Windows, installing pillow is a chore because you have to compile C code...which means installing the tools to do that. – Code-Apprentice Mar 29 '22 at 22:28

4 Answers4

2

Short reply, if you are running Python from the Standard Library, you can't, directly. The Standard Library is mostly a back-end library, so it works with logic, but doesn't "show" anything. This means that there's no way with the Standard Library to show images, you can however launch a process which opens the images.

E.g. Microsoft Paint

import subprocess
def open_image(path:str) -> None:
    command = f'mspaint {path}'
    subprocess.Popen(command)

open_image(input('Type your path: '))

If you don't want paint or another application, you should find a library that does it, be it Pillow or others.

0

To do this "right", you will need to learn about GUI programming with a library such as tkinter. Otherwise, the quick and dirty way is to use Image.show() from PIL. The documentation states that this is only intended for debugging purposes, so you should avoid it as a permanent solution in a serious project.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

First of all, it is a good thing to know how to install packages (the things you can import in your scripts to use functions others made). It will be usefull when doing more complicated projects.

In Pycharm, you can add packages to your project:

  1. File -> Settings -> Project: Project_Name -> Python Interpreter
  2. Clicking on the + icon and searching pillow

This is not really used because there are better ways of installing packages

These are the two main ways of installing packages:

Both of these allow you to install packages. I would recommend using conda because it is easier to separate projects from one another and allows the installation of packages that are not available using pip.

You will then be able to install PIL in the command line using

pip install Pillow

or (conda requires more setup, I encourage you to read https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)

conda install -c anaconda pillow

After installing the package you want here pillow, you can then use it in your code:

from PIL import Image
#from is used to specify the package to import
#import is used to specify which module you want to import in that library. 
#The module contains functions you can use. (It's more complicated than that, but for basics this is what is does)

name = input('name: ')
if name == 'Sammi':
    im = Image.open("sample-image.png") #Open the image
    im.show() #Show the image on screen

   
fxdup
  • 1
  • 1
  • This is a great guide for installing packages. One thing to address here, especially since you mention pillow, is that your mileage may vary when installing packages on different operating systems. From my experience, installing pillow on Windows is painful, even as an experienced programmer, because it requires compiling some code from C source. – Code-Apprentice Mar 29 '22 at 22:53
  • 1
    @Code-Apprentice Yes, this is why using conda can be much easier while installing packages. Conda contains not only python, but also c compilers and python interpreters. https://pythonspeed.com/articles/conda-vs-pip/#:~:text=The%20fundamental%20difference%20between%20pip,even%20the%20Python%20interpreter%20itself). – fxdup Mar 29 '22 at 22:58
  • Good to know. My experience with conda is limited. – Code-Apprentice Mar 29 '22 at 23:01
0

If you prefer to avoid using any PyPi you can use tkinter.

# As to not to pollute the namespace, from module 'tkinter' only import these classes, and make it 
# so you don't have to type tkinter.Whatever() every time you want to have a class instance

from tkinter import Tk, PhotoImage, Label


# Allow for case-insensitive inputs.
if input('Name: ').casefold() == 'sammi':
    # Initialize graphical window instance
    window = Tk()
    window.title("StackOverflow Example")
    
    # Load the image
    image = PhotoImage(file=r"C:\users\sammi\OneDrive\Desktop\B&W_2.jpg")
    Label(window, image=image).pack()

    # Bring the window to the front once it appears
    window.lift()
    window.attributes('-topmost', True)
    window.after_idle(window.attributes, '-topmost', False)

    # Make window appear with all parameters set as described above
    window.mainloop()
Somebody Out There
  • 347
  • 1
  • 5
  • 15