Ставим в Пайтон venv
$ pip install keras
$ pip install tensorflow
$ pip install pillow
****************
Code source
****************
(.env) boris@boris-All-Series:~/JPGNUMPY$ cat ConvertJPGEG.py
from tensorflow.keras.preprocessing.image import load_img
import warnings
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import array_to_img
# load the image via load_img function
img = load_img('/home/boris/Pictures/Lake-Wanaka-768x490.jpg')
# details about the image printed below
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
# convert the given image into numpy array
img_numpy_array = img_to_array(img)
print("Image is converted and NumPy array information :")
# <class 'numpy.ndarray'>
print(type(img_numpy_array))
# type: float32
print("type:", img_numpy_array.dtype)
# shape
print("shape:", img_numpy_array.shape)
# convert back to image
img_pil_from_numpy_array = array_to_img(img_numpy_array)
# <class 'PIL.PngImagePlugin.PngImageFile'>
print("converting NumPy array into image:",
type(img_pil_from_numpy_array))
********************
Выполняем тест :
********************
(.env) boris@boris-All-Series:~/JPGNUMPY$ python3 ConvertJPGEG.py
<class 'PIL.JpegImagePlugin.JpegImageFile'>
JPEG
RGB
(768, 490)
Image is converted and NumPy array information :
<class 'numpy.ndarray'>
type: float32
shape: (490, 768, 3)
converting NumPy array into image: <class 'PIL.Image.Image'>
(.env) boris@boris-All-Series:~/JPGNUMPY$ cat ShowJPGEG.py
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import save_img
from tensorflow.keras.preprocessing.image import img_to_array
import warnings
# load image as as grayscale
img = load_img('/home/boris/Pictures/Lake-Wanaka-768x490.jpg', color_mode = "grayscale")
# convert image to a numpy array
img_array = img_to_array(img)
# save the image with a new filename
save_img('wanaka.jpg', img_array)
# load the image to confirm it was saved correctly
img = load_img('wanaka.jpg')
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
img.show()
(.env) boris@boris-All-Series:~/JPGNUMPY$ python3 ShowJPGEG.py
<class 'PIL.Image.Image'>
None
RGB
(768, 490)
Другой способ PIL && asarray
(.env) boris@boris-All-Series:~/JPGNUMPY$ cat ConvertJPGEG1.py
# Import the necessary libraries
from PIL import Image
from numpy import asarray
# load the image and convert into
# numpy array
img = Image.open('/home/boris/Pictures/Lake-Wanaka-768x490.jpg')
numpydata = asarray(img)
# data
print(numpydata)
(.env) boris@boris-All-Series:~/JPGNUMPY$ python3 ConvertJPGEG1.py
[[[ 37 70 113]
[ 37 70 113]
[ 38 69 113]
...
[ 47 66 99]
[ 47 66 99]
[ 47 66 99]]
[[ 37 70 113]
[ 37 70 113]
[ 39 70 114]
...
[ 48 67 100]
[ 48 67 100]
[ 48 67 100]]
[[ 37 72 114]
[ 37 72 114]
[ 38 71 114]
...
[ 52 68 102]
[ 52 68 102]
[ 52 68 102]]
...
[[ 27 93 145]
[ 27 93 145]
[ 27 93 145]
...
[ 82 108 135]
[ 82 108 135]
[ 82 108 135]]
[[ 26 92 144]
[ 25 91 143]
[ 25 91 143]
...
[ 82 108 135]
[ 82 108 135]
[ 81 107 134]]
[[ 26 92 144]
[ 25 91 143]
[ 25 91 143]
...
[ 82 108 135]
[ 82 108 135]
[ 81 107 134]]]
No comments:
Post a Comment