I am trying to take the points in this image:
and transfer them to this US map outline:
but I am struggling with it.
I am trying to use a method in which I read the color and coordinate location of the non-green pixels from 'data.png', store them in a list, and them place those pixels onto 'us_outline.png' based of off their location.
Here is the code I have so far:
#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)
But doing so results in an error: IndexError: image index out of range
I have also tried this:
#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 )
but doing so results in errors in lines 21 and 22
I have also tried simply adding the two images together, but that yielded a weird result.
I have tried many methods and I can't figure out how to get it to work. Please help!
Thanks in advance
In your first piece of code, you just need to swap row
and col
to read in the pixels correctly. Line 18 becomes
r,g,b = data.getpixel((col, row))
Otherwise, the following piece of code accomplishes your goal and is a bit more succinct:
import numpy as np
import matplotlib.pyplot as plt
# find indices of non-green pixels
data = plt.imread('data.png')
green = np.zeros_like(data)
green[:,:,1] = 1. # plt.imread for some bizarre reason returns rgb values between 0.-1. for the given pngs, not 0-255!
x, y = np.where(np.any(data != green, axis=-1))
# plot non-green pixels on us outline
us = plt.imread('us_outline.png')
us[x,y] = data[x,y]
plt.imshow(us)
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.