Scale array values after resizing an image in python - python

I have a 480x640 image and I am cropping to 195x195.
The original 480x640 has a corresponding (68,2) array which can be used to further process the image. Since I cropped the image, how am can I rescale the (68,2) array values to correspond to the new cropped image?
I tried to do something along the line:
shape = shape*((195*195)/(480*640))
but it was no use. N.B. shape is the (68,2) array

Related

How to edit pixels via PIL with a 1D array [0:255]

Using the following code, PIL easily returns an array of single pixel values from an image. Not sure what the term for it is; but instead of a 3d array (RGB), it simplifies each pixel into one of 256 values.
from PIL import Image
im = Image.open(image_path, 'r')
pixel_values = list(im.getdata())
The question is, how can I edit pixels on an image with this same method? I believe the default arg for the putpixel method expects a 3d array (RGB), and if I only give one value; it only ranges over shades of black.
im.putpixel((x, y), value)
im.show()
I would like to be able to substitute integers (0-255) in for value and have access to the wider spectrum of discrete colors.
Is this possible? Seems like it should already be a built in method.

Reading 3D numpy array

I have a dataset which comprises of the binary data of pixelated 50x50 images. The array shape is (50, 50, 90245). I want to reach 50x50 pixels of each of the 90245 images. How can I slice the array?
If data is the variable storing the image data, and i is the index of the image you want to access, then you can do:
data[:,:,i]
to get the desired image data.
If data is the variable storing the image data, and i is the index of the image you want to access, then you can do as #BrokenBenchmark suggested. In case you want a (50,50,1) 3D array as the output, you could do:
data[:,:,i:i+1]
to get the image as a 3D array.
Edit1: If you reshaped your data matrix to be of shape (90245,50,50), you can get the ith image by doing data[i,:,:] or just data[i] to get a (50,50) image. Similarly, to get a (1,50,50) image, you could do data[i:i+1,:,:] or just data[i:i+1].
Edit2: To reshape the array, you could use the swapaxes() function in numpy.

Converting an image into a vector of pixels

I am trying to convert an image into an array of pixels.
Here is my current code.
im = Image.open("beeleg.png")
pixels = im.load()
im.getdata() # doesn't work
print(pixels # doesn't work
Ideally, my end goal is to convert the image into a vector of just pixels, so for instance if I have an image of dimensions 100x100, then I want a vector of dimensions 1x10000, where each value is between [0, 255]. Then, divide each of the values in the array by 256 and add a bias of 1 in the front of the vector. However, I am not able to proceed with all this without being able to obtain an array. How to proceed?
Scipy's ndimage library is generally the go-to library for working with pixels as data (arrays). You can load an image from file (most common formats supported) using scipy.ndimage.imread into a numpy array which can be easily reshaped and mathematically operated on. The mode keyword can be used to specify a colorspace transformation upon load (convert an RGB image to black and white). In your case you asked for single color pixels from 0-255 (8bit grayscale) so you would use mode='L'. See The Documentation for usage / more useful functions.
If use OpenCV, gray=cv2.imread(image,0) will return a grayscale image with n rows x m cols single channel numpy array. rows, cols = gray.shape will return the height and width of the image.

OpenCV: What is the dimension of the array representing a BGR image?

According to documentation, a BGR image is represented this way in OpenCV:
(source: opencv.org)
My question: what is the dimension of the array displayed by this picture ?
From the docs
Image properties include number of rows, columns and channels, type of image data, number of pixels etc.
Shape of image is accessed by img.shape. It returns a tuple of number of rows, columns and channels (if image is color):
>>> print img.shape
(342, 548, 3)
Note
If image is grayscale, tuple returned contains only number of rows and columns. So it is a good method to check if loaded image is grayscale or color image.

Python - get white pixels of image

I'm would like to go from an image filename to a list of coordinates of the white pixels in the image.
I know it involves PIL. I have tried using Image.load() but this doesn't help because the output is not indexable (to use in a for loop).
You can dump an image as a numpy array and manipulate the pixel values that way.
from PIL import Image
import numpy as np
im=Image.open("someimage.png")
pixels=np.asarray(im.getdata())
npixels,bpp=pixels.shape
This will give you an array whose dimensions will depend on how many bands you have per pixel (bpp above) and the number of rows times the number of columns in the image -- shape will give you the size of the resulting array. Once you have the pixel values, it ought to be straightforward to filter out those whose values are 255
To convert a numpy array back to an image use:
im=Image.fromarray(pixels)

Categories

Resources