1
我想取分这个图像间的像素颜色和位置:如何传输图像
,并将它们转移到美国的这一地图的轮廓:
但我正在努力。
我想用一种方法,从'data.png'中读取非绿色像素的颜色和坐标位置,将它们存储在列表中,然后将这些像素放置到'us_outline.png'基于他们的位置。
这里是我的代码至今:
#IMPORTS
from __future__ import division
import math
import numpy as np
from PIL import Image
import matplotlib.pyplot as mplot
#List of pixels from data.png
pixels = []
height = 140
width = 200
#Read in data from data.png
data = Image.open("data.png")
data = data.convert('RGB')
for row in range(0,height): #loops over the number of rows in the image
for col in range(0,width): # loops over the number of columns in the current row
r,g,b = data.getpixel((row,col))
rgb = []
rgb.append(r)
rgb.append(g)
rgb.append(b)
if rgb != [0,255,0]:
pixels.append(rgb)
但这样做在一个错误的结果:IndexError:图像索引超出范围
我也试过这样:
#Convert to float32 format
data_image = np.float32(data)
#Reads in data points from data.png and appends them to a list
for row in range(len(data_image)): #loops over the number of rows in the image
for col in range(len(data_image[row])): # loops over the number of columns in the current row
pixel = data_image[row][col] #Assigns pixel at row and column to a variable
if pixel != [0,255,0,255]: #If pixel is not green (a.k.a it is a data point)
pixels.append(pixel) #Set key to the location of pixel and set value to pixel color
#Read in data from us_outline.png
img2 = Image.open("us_outline.png")
usmap = img2.load()
#Convert to float32 format
usmap_image = np.float32(usmap)
#Writes data from pixels list to US map
for row in range(len(usmap_image)): #loops over the number of rows in the image
for col in range(len(usmap_image[row])): # loops over the number of columns in the current row
for pixel in range(len(pixels)):
if pixels[row][col] == usmap_image[row][col]:
usmap_image[row][col] = pixels[row][col]
usmap_image = np.uint8(usmap_image)
但这样做会导致第21行和第22行出现错误
我也尝试过简单地添加将这两幅图像放在一起,但是这产生了一个奇怪的结果。
我已经尝试了很多方法,我无法弄清楚如何让它工作。请帮忙!
在此先感谢