Skip to content

Commit

Permalink
#28 adding numpy support example scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
myselfhimself committed Aug 24, 2020
1 parent 19d86a6 commit f5227e1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/numpy/LGM_numpy_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import gmic
import PIL
import PIL.Image
import numpy
from numpy import asarray, array_equal
from matplotlib import pyplot

l = []
gmic.run("sp duck", l)
gmic.run("display", l)

ll = l[0].to_numpy_array(interleave=True, astype=numpy.uint8, squeeze_shape=True)
print(ll.shape)
print(ll.dtype)

pyplot.imshow(ll)
pyplot.show()
46 changes: 46 additions & 0 deletions examples/numpy/numpy_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# TEST 1: numpy(interleaved)->pyplot_display(interleaved)->gmic_from_nparray(deinterleaved)->gmic_display(deinterleaved)
import gmic
import PIL
import PIL.Image
import numpy
from numpy import asarray, array_equal
from matplotlib import pyplot

gmic.run("sp duck output image.png")
i = PIL.Image.open('image.png') # Lena sample
ii = asarray(i)
print(ii.shape) #(512, 512, 3)
print(ii.dtype) #uint8

l = []
gmic.run("sp duck", l)
ll = l[0].to_numpy_array(interleave=True, astype=numpy.uint8, squeeze_shape=True)
array_equal(ll,ii) # True
print(ll.shape) #(512, 512, 3)
print(ll.dtype) #uint8

pyplot.imshow(ll)
pyplot.show() # Lena in good colors, dimensions and orientation

pyplot.imshow(ii)
pyplot.show() # Lena in good colors, dimensions and orientation

j = gmic.GmicImage.from_numpy_array(ll, deinterleave=True)
gmic.run("display", j) # Lena in good colors, dimensions and orientation
"""
[gmic]-1./ Display image [0] = '[unnamed]', from point (256,256,0).
[0] = '[unnamed]':
size = (512,512,1,3) [3072 Kio of floats].
data = (225,225,223,223,225,225,225,223,225,223,223,223,(...),78,78,78,77,91,80,79,89,77,79,79,82).
min = 8, max = 251, mean = 128.241, std = 58.9512, coords_min = (457,60,0,1), coords_max = (425,20,0,0).
"""

jj = gmic.GmicImage.from_numpy_array(ii, deinterleave=True)
gmic.run("display", jj) # Lena in good colors, dimensions and orientation
"""
[gmic]-1./ Display image [0] = '[unnamed]', from point (256,256,0).
[0] = '[unnamed]':
size = (512,512,1,3) [3072 Kio of floats].
data = (225,225,223,223,225,225,225,223,225,223,223,223,(...),78,78,78,77,91,80,79,89,77,79,79,82).
min = 8, max = 251, mean = 128.241, std = 58.9512, coords_min = (457,60,0,1), coords_max = (425,20,0,0).
"""
38 changes: 38 additions & 0 deletions examples/numpy/numpy_testing2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# TEST 2: numpy(deinterleaved)->gmic_from_nparray(deinterleaved)->gmic_display(deinterleaved)
import gmic
import PIL
import PIL.Image
from numpy import asarray, array_equal

gmic.run("sp lena output image.png")
i = PIL.Image.open('image.png')
ii = asarray(i)
print(ii.shape) # (512, 512, 3)
print(ii.dtype) # uint8

l = []
gmic.run("sp lena", l)
ll = l[0].to_numpy_array(interleave=False, astype=int, squeeze_shape=True) # default astype=float32, default squeeze_shape=True
print(ll.shape) # (512, 512, 3)
print(ll.dtype) # int64
array_equal(ll,ii) # False; uint8 vs. int64 match but deinterleave vs interleave unmatch

j = gmic.GmicImage.from_numpy_array(ll, deinterleave=False)
gmic.run("display", j) # Lena in good orientation, color, size
"""
[gmic]-1./ Display image [0] = '[unnamed]', from point (256,256,0).
[0] = '[unnamed]':
size = (512,512,1,3) [3072 Kio of floats].
data = (225,225,223,223,225,225,225,223,225,223,223,223,(...),78,78,78,77,91,80,79,89,77,79,79,82).
min = 8, max = 251, mean = 128.241, std = 58.9512, coords_min = (457,60,0,1), coords_max = (425,20,0,0).
"""

jj = gmic.GmicImage.from_numpy_array(ii, deinterleave=True)
gmic.run("display", jj) # Lena in good orientation, color, size
"""
[gmic]-1./ Display image [0] = '[unnamed]', from point (256,256,0).
[0] = '[unnamed]':
size = (512,512,1,3) [3072 Kio of floats].
data = (225,225,223,223,225,225,225,223,225,223,223,223,(...),78,78,78,77,91,80,79,89,77,79,79,82).
min = 8, max = 251, mean = 128.241, std = 58.9512, coords_min = (457,60,0,1), coords_max = (425,20,0,0).
"""

0 comments on commit f5227e1

Please sign in to comment.