0

The task was to read from an image and extract the data to get a grid of 25 x 25 pixels. I used this code to extract it and I got the data. But how am I supposed to save it in a text file to see the whole 25 x 25 array?

I just converted an image to an array like this:

>>> img=cv2.imread("1670.png",0)
>>> img=cv2.imread("1670.png",0)

>>> img
array([[14, 15, 15, ..., 92, 91, 92],
[14, 15, 15, ..., 93, 93, 91],
[15, 15, 16, ..., 94, 93, 91],
...,
[69, 71, 76, ..., 73, 70, 68],
[69, 75, 81, ..., 76, 72, 69],
[69, 76, 81, ..., 80, 75, 71]], type=uint8)

I want to save it to a text file as a 25 x 25 array. How can I do this?

Matthew Champion
  • 402
  • 9
  • 18
Tom
  • 1
  • 1

1 Answers1

0

Wrote a little script with a few different file conversions:

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 09 15:53:54 2018
@author: soyab
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread, imsave, imresize

#%%
## Read photo ; view photo
img = imread('new_photo.jpg')
plt.imshow(img)

#%%
## Treat as a Numpy array
np.shape(img)
newimg = img[:25,:25,:][:,:,0] ## Take a 25x25 slice from a 
np.shape(newimg)               ##  three 'layer' photo
plt.imshow(newimg)

#%%
## Save as a jpeg ; visual check
imsave('modify_new_photo.jpg',newimg)
nextimg = imread('modify_new_photo.jpg')
plt.imshow(nextimg)

#%%
## Using a Pandas Dataframe ; like a Kung Fu Panda
### Save as .txt or .csv ; reload as a pd.DataFrame
#### Review again as an image
nextimg = pd.DataFrame(nextimg)
nextimg.to_csv('modify_nextimg.txt',header=False,index=False)
fimg = pd.read_csv('modify_nextimg.txt')
plt.imshow(fimg)
  • Hi there, Y did u take a slice from a three layer photo? Without slicing can't i do it? – Tom Mar 11 '18 at 02:13
  • You'll have to check the dimensions of your photo. When I wrote this example I used a photo with the 3d array format. Whatever Python image object you assign to "nextimg" can be saved with the provided code. – Soya Bjorlie Mar 15 '18 at 18:19