社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Carlos Horn  »  全部回复
回复总数  3

我想到的另一种可能性是使用最小过滤器。然而,我预计它会比第一个提议的解决方案慢,但在它的基础上构建更多的解决方案可能会有用。

import numpy as np
from scipy.ndimage import minimum_filter

# create a footprint that only takes the neighbours into account
neighbours = (np.arange(9) % 2 == 1).reshape(3,3)

# create a mask of relevant pixels, img should be your image as array
mask = np.logical_and(
    img == 255,
    minimum_filter(img, footprint=neighbours) == 0
)

# get indexes
indexes = np.where(mask)

# as list
list(zip(*indexes))

你可以用 numba 即时编译器来加速你的循环。

from numba import njit

@njit
def find_highlow_pixels(img):
    pixels = []
    for j in range(1, img.shape[0]-1):
        for i in range(1, img.shape[1]-1):
            if (
                img[j, i] == 255 and (
                    img[j-1, i]==0 or img[j+1,i]==0 or 
                    img[j, i-1]==0 or img[j, i+1]==0
                )
            ):
                pixels.append((j, i))
    return pixels 
4 年前
回复了 Carlos Horn 创建的主题 » 如何用python从这个字典中提取值?

你应该把这个字符串变成字典。 json.loads .然后从普通嵌套字典中提取like。

例子:

import json
message = mydict["message"]
message_dict = json.loads(message)
message_dict['id']

哪里 mydict 是给定的输入字典。

如果代码段显示文件的内容,可以执行以下操作:

with open(filepath) as file:
    mydict = json.load(file)
message_dict = mydict["message"]
message_dict['id']