pythonarraysimagevectorrescale

Python - How can I convert a 2D grayscale image to a 1D vector


I'm learning python and am trying to learn about manipulating images. I want to rescale (downscale) a 2D graysacle image to a 1D vector (array of single row/column). In my test code, when I rescale the image, the output values in the array are in decimal (float) format. But I want to rescale and keep the values in the 1D array as integers. Can someone please help/guide me?

This is my code:

#Testing Image to vector

#Importing required functionality
import skimage.io as io
import numpy as np
from skimage.transform import rescale


#read image
image=io.imread("https://www.usna.edu/Users/cs/wcbrown/courses/F14IC210/lab/l09/cat.jpg")
#print image
print (image)

#rescale to 50%
small_im = rescale(image,0.5)
#print the rescaled image
print(small_im)

#manipulate the array
x=np.array(small_im)
#convert to 1D vector
y=np.concatenate(x)
print (y)


#print each value in the 1D vector in a new line. Just to see how far it would go
for i in y:
    print (i, end='\n')

A snippet of the output I get is this(it goes way further due to the loop):

[[ 8  8  9 ... 12 11 11]
 [ 8  8  9 ... 12 11 11]
 [ 7  7  8 ... 12 11 11]
 ...
 [ 5  5  5 ... 98 97 96]
 [ 5  5  5 ... 98 97 97]
 [ 5  5  5 ... 99 98 97]]
[[0.02745098 0.02941176 0.02941176 ... 0.04509804 0.04313725 0.04313725]
 [0.0254902  0.0254902  0.0254902  ... 0.04509804 0.04313725 0.04313725]
 [0.0254902  0.0254902  0.0254902  ... 0.04509804 0.04313725 0.04313725]
 ...
 [0.01960784 0.01960784 0.01960784 ... 0.38039216 0.37843137 0.37647059]
 [0.01960784 0.01960784 0.01960784 ... 0.38039216 0.37843137 0.37647059]
 [0.01960784 0.01960784 0.01960784 ... 0.38039216 0.38039216 0.37843137]]
[0.02745098 0.02941176 0.02941176 ... 0.38039216 0.38039216 0.37843137]
0.027450980392156862
0.029411764705882575
0.029411764705882575
0.027450980392156862
0.03137254901960784
0.03529411764705882
0.03529411764705882
0.032352941176470695
0.03039215686274498
0.02941176470588213
0.030392156862744994
0.03431372549019597
0.03529411764705882
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.043137254901960784

Solution

  • After trying and googling, I've found the answer. At least, in my context, it is what I was trying to achieve.

    Solution code:

    #solution to converting to 1D vector
    
    #Importing required functionality
    import numpy as np
    from PIL import Image
    
    #Opening Image and resizing to 10X10 for easy viewing
    image_test = np.array(Image.open('1.png').resize((10,10)))  #note: I used a local image
    #print image
    print (image_test)
    
    #manipulate the array
    x=np.array(image_test)
    #convert to 1D vector
    y=np.concatenate(x)
    print (y)
    
    
    #print each value in the 1D vector in a new line. Just to see how far it would go
    for i in y:
        print (i, end='\n')
    

    Desired sample output (due to the loop it goes further):

    [[ 48  52  72  96  96  99  81  71  68  47]
     [ 52  85 133 149 168 175 157 116  70  46]
     [ 54 129 170 174 185 179 177 169  92  42]
     [ 55 142 165 171 187 175 162 167  97  40]
     [112 150 144 134 172 157 128 143 129 113]
     [162 166 166 158 166 164 154 163 157 155]
     [105 166 185 174 170 165 175 179 140  81]
     [ 35 113 199 170 147 145 174 181  83  32]
     [ 46  65 179 183 160 153 166 155  71  37]
     [ 47  58 169 178 170 159 148 158  74  39]]
    [ 48  52  72  96  96  99  81  71  68  47  52  85 133 149 168 175 157 116
      70  46  54 129 170 174 185 179 177 169  92  42  55 142 165 171 187 175
     162 167  97  40 112 150 144 134 172 157 128 143 129 113 162 166 166 158
     166 164 154 163 157 155 105 166 185 174 170 165 175 179 140  81  35 113
     199 170 147 145 174 181  83  32  46  65 179 183 160 153 166 155  71  37
      47  58 169 178 170 159 148 158  74  39]
    48
    52
    72
    96
    96
    99
    81
    71
    68
    47
    52
    85
    133
    149
    168
    175
    157
    116
    70
    46