如何在python中接受图像作为用户输入

2024-09-28 03:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个图像处理程序。目前,我已经在程序中包含了图像名。 如果可能的话,我正在尝试让用户输入图像,这样程序就可以为任何图像服务。

在python中有一种接受用户输入的方法,但是我怎样才能接受一个图像作为输入,它通向图像的路径。??

程序的前几行,寻找方法,接受来自用户的图像“house1.jpg”。我的电脑中此图像的路径是“C:\ Python27\house1”

import Image
#open the images
im1 = Image.open("house1.jpg").convert("L")
im2 = Image.open("house2.jpg").convert("L")

im1.save(house1.jpg")
im2.save(house2.jpg")

sizex, sizey = im1.size

。。。。


Tags: 方法用户图像image路径程序convertsave
3条回答

在python中,我们有一个名为枕头的包,可以用来读取和编辑图像。 所以,如果我们有兴趣用python打开一个文件,我们可以这样做--

from PIL import Image img = Image.open(path) #path is the path of image file

对于其他选项,您可以遵循枕头文档 https://pillow.readthedocs.io/en/5.3.x/

import sys

im1 = Image.open(sys.argv[1])

并在命令行上传递图像:python script.py house1.jpg。这显然扩展到了多个命令行参数/图像;请阅读库文档中的^{}

你要找的是^{}

代码如下所示:

 imageFileName = raw_input("enter the name of the image file: ")

相关问题 更多 >

    热门问题