Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问PIL图像子元素,并从中获取字符串_Python_String_Variables_Python Imaging Library - Fatal编程技术网

Python 访问PIL图像子元素,并从中获取字符串

Python 访问PIL图像子元素,并从中获取字符串,python,string,variables,python-imaging-library,Python,String,Variables,Python Imaging Library,目前,我有一个图像,我只想从示例中抽取一个字符串,概述以下细节(假设该图像已加载到内存中供PIL访问) 但是当我试图访问它时,它抛出了一个错误,只要求一个整数(据我所知,它不可能使用??),这意味着我只能访问一个像素,我是否可以从图像行(x=0到x=250)的开始在列(y=200)上采样一行 感谢您在不使用numpy阵列的情况下抽出时间: from PIL import Image i = Image.open("basic_training.png") # get a line def ge

目前,我有一个图像,我只想从示例中抽取一个字符串,概述以下细节(假设该图像已加载到内存中供PIL访问)

但是当我试图访问它时,它抛出了一个错误,只要求一个整数(据我所知,它不可能使用??),这意味着我只能访问一个像素,我是否可以从图像行(x=0到x=250)的开始在列(y=200)上采样一行


感谢您在不使用numpy阵列的情况下抽出时间

from PIL import Image
i = Image.open("basic_training.png")

# get a line
def get_line(i, y):
    pixels = i.load() # this is not a list, nor is it list()'able
    width, height = i.size
    all_pixels = []
    for xPos in range(width):
        cpixel = pixels[xPos, y]
        all_pixels.append(cpixel)
    return all_pixels

# get a columns
def get_column(i, x):
    pixels = i.load() # this is not a list, nor is it list()'able
    width, height = i.size
    all_pixels = []
    for yPos in range(height):
        cpixel = pixels[x, yPos]
        all_pixels.append(cpixel)
    return all_pixels

line = get_line(i, y)
print len(line)
col = get_column(i, x)
print len(col)
get_line给出一条线,给定它的y位置
get_col给出一个给定x位置的列


你对字符串、图像等的描述有点混乱。不知道您是否只需要一个像素、整行等。使用上面的代码,因为它返回一个普通的python列表,您可以将它们连接到(col)或您需要的方式。

使用教程作为参考:

这允许您从检索到的数据行中检索RGB字节:

>>> L[0, 0]
(98, 92, 44)
>>> L[250, 0]
(104, 97, 43)
为了显示尺寸限制:

>>> L[0, 1] # out of bounds
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: image index out of range
>>> L[251, 0] # out of bounds
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: image index out of range
>L[0,1]#超出范围
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器:图像索引超出范围
>>>L[251,0]#出界
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器:图像索引超出范围

对不起,我的意思是我只是注意到了图像的像素大小,真的应该换一种说法
>>> L[0, 0]
(98, 92, 44)
>>> L[250, 0]
(104, 97, 43)
>>> L[0, 1] # out of bounds
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: image index out of range
>>> L[251, 0] # out of bounds
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: image index out of range