Reputation: 23
I have made myself a numpy array from a picture using
from PIL import Image
import numpy as np
image = Image.open(file)
np.array(image)
its shape is (6000, 6000, 4)
and in that array I would like to replace pixel values by one number lets say this green pixel [99,214,104,255]
will be 1
.
I have only 4 such pixels I want to replace with a number and all other pixels will be 0
. Is there a fast and efficient way to do so and what is the best way to minimize the size of the data. Is it better to save it as dict()
, where keys will be x,y
and values, will be integers
? Or is it better to save the whole array as it is with the shape it has? I only need the color values the rest is not important for me.
I need to process such a picture as fast as possible because there is one picture every 5 minutes and lets say i would like to store 1 year of data. That is why I'd like to make it as efficient as possible time and space-wise.
Upvotes: 2
Views: 1386
Reputation: 14486
If I understand the question correctly, you can use np.where for this:
>>> arr = np.array(image)
>>> COLOR = [99,214,104,255]
>>> np.where(np.all(arr == COLOR, axis=-1), 1, 0)
This will produce a 6000*6000 array with 1
if the pixel is the selected colour, or 0
if not.
Upvotes: 1
Reputation: 4510
How about just storing in a database
: the position
and value
of the pixels you want to modify, the shape
of the image
, the dtype
of the array and the extension
(jpg, etc...). You can use that information to build a new image from an array filled with 0
.
Upvotes: 0