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 from
and 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.
CodePudding user response:
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.
CodePudding user response:
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.
CodePudding user response:
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:
- File -> Settings -> Project: Project_Name -> Python Interpreter
- 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:
Using pip (https://pip.pypa.io/en/stable/installation/)
Using conda (https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html#regular-installation)
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
CodePudding user response:
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
if input('Name: ').casefold() == 'sammi':
# Initialize graphical window instance
ws = Tk()
ws.title("StackOverflow Example")
# Load the image
img = PhotoImage(file=r"C:\users\sammi\OneDrive\Desktop\B&W_2.jpg")
Label(ws, image=img).pack()
# Bring the window to the front once it appears
ws.lift()
ws.attributes('-topmost', True)
ws.after_idle(ws.attributes, '-topmost', False)
# Make window appear with all parameters set as described above
ws.mainloop()