【发布时间】:2025-12-06 14:20:22
【问题描述】:
我最初从初始数组为灰度图像创建了一个子数组:Deleting consecutive numbers from a numpy array 和 Remove following duplicates in a numpy array
但现在我想对彩色图像做同样的事情,我真的很困惑。我已经为此工作了好几天,根本无法理解如何处理它。
问题是正方形的大小不同,我希望每个正方形都有一个像素,用相同的颜色表示。
彩色图像:
我的灰度图像代码:
from PIL import Image
import numpy as np
name1 = raw_input("What is the name of the .png file you want to open? ")
filename1 = "%s.png" % name1
img = Image.open(filename1).convert('L') # convert image to 8-bit grayscale
WIDTH, HEIGHT = img.size
a = list(img.getdata()) # convert image data to a list of integers
# convert that to 2D list (list of lists of integers)
a = np.array ([a[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)])
print " "
print "Intial array from image:" #print as array
print " "
print a
rows_mask = np.insert(np.diff(a[:, 0]).astype(np.bool), 0, True)
columns_mask = np.insert(np.diff(a[0]).astype(np.bool), 0, True)
b = a[np.ix_(rows_mask, columns_mask)]
print " "
print "Subarray from Image:" #print as array
print " "
print b
#img = Image.fromarray(b, mode='L')
print " "
print "Subarray from Image (clearer format):" #print as array
print " "
for row in b: #print as a table like format
print(' '.join('{:3}'.format(value) for value in row))
#img.save("chocolate.png")
#print np.mean(b) #finding mean
例如这张图片:
输入数组示例:
来自a = list(img.getdata()),这是我从图像中得到的输入。
[(115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95)]
numpy 输入使用a = np.array ([a[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)]):
[[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]
[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]
[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]
[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]
[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]
[[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]
[[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]
[[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]
[[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]
[[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]
[[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[ 55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]]
所需的输出:
[[[115 45 135] [245 245 35]]
[ 55 235 195] [245 245 95]]]
【问题讨论】:
-
我已经更新了上面的帖子。如果你能调查一下,我将非常感激!
-
与其从图像中删除像素并不得不将所有像素重新组合在一起以填补由此产生的空白,不如在开始时创建一个新的空输出图像,然后遍历您的原始图像,并且仅将像素复制到原始图像中值发生变化的新图像....
-
非常感谢。你能指导我找到一个资源或一个例子吗,我可以看看?
标签: python arrays numpy image-processing python-imaging-library