This is with respect to the FER2013 dataset. The data consists of 48x48 pixel grayscale images of faces. The CSV file contains three columns as (emotion, pixels, Usage), where Usage has any of three value - training, PrivateTest and PublicTest. I want to read the array of pixels, convert them into an image and save them in the respective folder named as per their Usage type.
I need python code that can do the above.
Following is my code
import pandas as pd
import numpy as np
from PIL import Image
df=pd.read_csv("fer2013.csv")
for rows in df:
arr=np.array(df['pixels'])
print(arr)
print(arr.shape)
img = Image.fromarray(arr.reshape(48,48), 'L')
img.save("dataset/df['Usage']/img.jpg", "JPEG")
The above code shows error:
cannot reshape array of size 35887 into shape (48,48).
In case there are any doubts (because I have been working with the FER dataset):
import pandas as pd
import numpy as np
from PIL import Image
df = pd.read_csv('fer2013.csv')
for image_pixels in df.iloc[1:,1]: #column 2 has the pixels. Row 1 is column name.
image_string = image_pixels.split(' ') #pixels are separated by spaces.
image_data = np.asarray(image_string, dtype=np.uint8).reshape(48,48)
img = Image.fromarray(image_data) #final image
Related
I am trying to segment a colour image using the Mean-Shift algorithm using sklearn.
I have the following code:
import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
from itertools import cycle
from PIL import Image
image = Image.open('sample_images/fruit_half.png')
image = np.array(image)
#need to convert image into feature array based on rgb intensities
flat_image = np.reshape(image, [-1,3])
I am trying to convert the image into a feature array based on rgb intensities so that I can do the clustering.
However, I get the following error:
ValueError: cannot reshape array of size 3979976 into shape (3)
I am not sure why I am getting this error and how I can resolve this. Any insights are appreciated.
It's because the image you're loading does not have RGB values (if you look at the dimensions, the last one is 4.
You need to first convert it to RGB like this:
image = Image.open('sample_images/fruit_half.png').convert('RGB')
image = np.array(image)
# Now it works
flat_image = np.reshape(image, [-1,3])
I have a folder with 230400 images, each representing one pixel in a 480 x 480 image.
How can I use Python to make a single image out of each image?
I tried to creat a npy-array but I believe it resulted in a 3d array instead of a 2d array:
import cv2
import glob
import numpy as np
data = []
files = glob.glob("./data/*.PNG")
for myFile in files:
print(myFile)
image = cv2.imread(myFile)
data.append(image)
print('shape:', np.array(data).shape)
np.save('data',data)
Output: shape: (230400, 100, 100, 3)
How do I create a 2d array of images? And how do I convert it to an image?
Start by creating an empty numpy image with the size of your output image. For each pixel load the image in.
import numpy as np
import cv2
import glob
image_x = 480
image_y =480
files = glob.glob("./data/*.PNG")
output = np.zeros((image_x, image_y, 3))
for i in range(image_x):
for j in range(image_y):
pixel = cv2.imread(files[image_x*i+j])
output[i,j] = pixel[0,0]
Note: This is neither fast nor nice, but explicit.
For saving, use cv2.imwrite on the resulting array as in:
cv2.imwrite('output.png', output)
I'm using PIL and numpy to combine two images while one is a .jpg and the other image is represented by a numpy array, which defines a mask that I want to put on top of the original image (basically just a matrix with one and zero entries and the same size as the .jpg). PIL’s composite function works just fine for that but for some reason, after saving the composite image, the file size shrinks to approximately 1/3 of the original image size. Can someone explain this behavior to me?
Here's a code snippet:
import numpy as np
import PIL
from PIL import Image
from PIL import ImageColor
rgb = ImageColor.getrgb('black')
# Read image and write into numpy array
image = Image.open('test_image.jpg')
(im_width, im_height) = image.size
# Create empty mask
mask = np.zeros((im_width, im_height))
# Composite image and mask
solid_color = np.expand_dims(np.ones_like(mask), axis=2) *
np.reshape(list(rgb), [1, 1, 3])
pil_solid_color =
Image.fromarray(np.uint8(solid_color)).convert('RGBA')
pil_mask = Image.fromarray(np.uint8(255.*mask)).convert('L')
image = Image.composite(pil_solid_color, image, pil_mask)
# save image
image.save('test_image_with_mask.jpg')
Code was inspired by tnesorflow's object detection api. Thanks in advance.
I am using cifar-10 dataset for my training my classifier. I have downloaded the dataset and tried to display am image from the dataset. I have used the following code:
from six.moves import cPickle as pickle
from PIL import Image
import numpy as np
f = open('/home/jayanth/udacity/cifar-10-batches-py/data_batch_1', 'rb')
tupled_data= pickle.load(f, encoding='bytes')
f.close()
img = tupled_data[b'data']
single_img = np.array(img[5])
single_img_reshaped = single_img.reshape(32,32,3)
plt.imshow(single_img_reshaped)
the description of data is as follows:
Each array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.
Is my implementation correct?
the above code gave me the following image:
I used
single_img_reshaped = np.transpose(np.reshape(single_img,(3, 32,32)), (1,2,0))
to get the correct format in my program.
Since Python uses the default C-like indexing order (row-major order), it can be forced to work in column-major order:
import numpy as np
import matplotlib.pyplot as plt
# I assume you have loaded your data into x_train (see some tutorial)
data = x_train[0, :] # get a row data
data = np.reshape(data, (32,32,3), order='F' ) # Fortran-like indexing order
plt.imshow(data)
single_img_reshaped = single_img.reshape(3,32,32).transpose([1, 2, 0])
i have MNIST dataset and i am trying to visualise it using pyplot. The dataset is in cvs format where each row is one image of 784 pixels. i want to visualise it in pyplot or opencv in the 28*28 image format. I am trying directly using :
plt.imshow(X[2:],cmap =plt.cm.gray_r, interpolation = "nearest")
but i its not working? any ideas on how should i approach this.
Assuming you have a CSV file with this format, which is a format the MNIST dataset is available in
label, pixel_1_1, pixel_1_2, ...
Here's how you can visulize it in Python with Matplotlib and then OpenCV
Matplotlib / Pyplot
import numpy as np
import csv
import matplotlib.pyplot as plt
with open('mnist_test_10.csv', 'r') as csv_file:
for data in csv.reader(csv_file):
# The first column is the label
label = data[0]
# The rest of columns are pixels
pixels = data[1:]
# Make those columns into a array of 8-bits pixels
# This array will be of 1D with length 784
# The pixel intensity values are integers from 0 to 255
pixels = np.array(pixels, dtype='uint8')
# Reshape the array into 28 x 28 array (2-dimensional array)
pixels = pixels.reshape((28, 28))
# Plot
plt.title('Label is {label}'.format(label=label))
plt.imshow(pixels, cmap='gray')
plt.show()
break # This stops the loop, I just want to see one
OpenCV
You can take the pixels numpy array from above which is of dtype='uint8' (unsigned 8-bits integer) and shape 28 x 28 , and plot with cv2.imshow()
title = 'Label is {label}'.format(label=label)
cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()
Importing necessary packages
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
Reading mnist train dataset ( which is csv formatted ) as a pandas dataframe
s = pd.read_csv("mnist_train.csv")
Converting the pandas dataframe to a numpy matrix
data = np.matrix(s)
The first column contains the label, so store it in a separate array
output = data[:, 0]
And delete the first column from the data matrix
data = np.delete(data, 0, 1)
The first row represents the first image, it is 28X28 image (stored as 784 pixels)
img = data[0].reshape(28,28)
# And displaying the image
plt.imshow(img, cmap="gray")
For all like me who want a quick and dirty solution, simply to get a rough idea what a given input is about, in-console and without fancy libraries:
def print_greyscale(pixels, width=28, height=28):
def get_single_greyscale(pixel):
val = 232 + round(pixel * 23)
return '\x1b[48;5;{}m \x1b[0m'.format(int(val))
for l in range(height):
line_pixels = pixels[l * width:(l+1) * width]
print(''.join(get_single_greyscale(p) for p in line_pixels))
(expects the input to be shaped like [784] and with float values from 0 to 1. If either is not the case, you can easily convert (e.g. pixels = pixels.reshape((784,)) or pixels \= 255)
The output is a bit distorted but you get the idea.