# Jupyter Notebookでpgm画像を表示させる方法

Jupyter Notebookでpgm imageを表示させる方法ということで、先ずはこのサイトから拝借した下記のコードを実行してpgm画像を作成する。

#! /usr/bin/env python
#
import numpy as np
import pycuda.autoinit
import pycuda.driver as drv
import pycuda.gpuarray as gpuarray
import pycuda.tools

#from pgma_io import pgma_write
from pycuda.compiler import SourceModule
from pycuda.elementwise import ElementwiseKernel
#
#  Define the kernel function: ( "arguments", """text""", name=name ).
#
complex_gpu = ElementwiseKernel(
"float xmin, float xmax, float ymin, float ymax, int xnum, int ynum, int count_max, int *count_gpu",
"""
float ci;
int col;
float cr;
int k;
int row;
float zi;
float zi_new;
float znormsq;
float zr;
float zr_new;
//
//  I, the value of the 1-dimensional thread index, is automatically supplied.
//
col = i % xnum;
row = i / xnum;
cr = ( ( xnum - col - 1 ) * xmin + col * xmax ) / ( xnum - 1 );
ci = ( ( ynum - row - 1 ) * ymax + row * ymin ) / ( ynum - 1 );
count_gpu[i] = count_max;
zr = 0.0;
zi = 0.0;
for ( k = 0; k < count_max; k++ )
{
zr_new = zr * zr - zi * zi + cr;
zi_new = 2.0 * zr * zi + ci;
zi = zi_new;
zr = zr_new;

znormsq = zr * zr + zi * zi;
if ( 4.0 <= znormsq )
{
count_gpu[i] = k;
break;
}
}
""",
name = "mandlebrot_gpu"
)
def mandelbrot_pycuda ( ):

print ( '' )
print ( 'MANDELBROT_PYCUDA:' )
print ( '  PyCUDA version' )
print ( '  Compute the Mandelbrot set and save it in a graphics file.' )

xmin = -2.0
xmax = 1.0
ymin = -1.25
ymax = 1.25
xnum = 1000
ynum = 1000
count_max = 250

print ( '' )
print ( '  X range: [ %g, %g ]' % ( xmin, xmax ) )
print ( '  Y range: [ %g, %g ]' % ( ymin, ymax ) )
print ( '  Xnum = %d x Ynum = %d = %d Pixels' % ( xnum, ynum, xnum * ynum ) )
print ( '  Maximum number of iterations = %d' % ( count_max ) )
#
#  Allocate a CPU vector.
#
count_cpu = np.zeros ( xnum * ynum ).astype ( np.int32 )
#
#  Link it to a vector on the GPU.
#
count_gpu = gpuarray.to_gpu ( count_cpu )
#
#  Perform the calculation on the GPU.
#
complex_gpu ( \
np.float32 ( xmin ), \
np.float32 ( xmax ), \
np.float32 ( ymin ), \
np.float32 ( ymax ), \
np.int16 ( xnum ), \
np.int16 ( ynum ), \
np.int16 ( count_max ), \
count_gpu )
#
#  Copy the GPU vector to the CPU vector.
#
count_gpu.get ( count_cpu )
#
#  Reshape the CPU vector to an array.
#
count_cpu = count_cpu.reshape ( xnum, ynum )
#
#  Save the array as PGM graphics file.
#
file_name = 'mandelbrot_pycuda.pgm'
comment = 'mandelbrot_pycuda.pgm'
%time pgma_write ( file_name, comment, xnum, ynum, count_max, count_cpu )

print ( '' )
print ( '  Plot saved in file "%s"' % ( file_name ) )
#
#  Terminate.
#
print ( '' )
print ( 'MANDELBROT_PYCUDA:' )
print ( '  Normal end of execution.' )
return

def pgma_write ( file_name, comment, width, height, maxval, gray ):

#*****************************************************************************80
#
## PGMA_WRITE writes an ASCII PGM graphics file.
#
#  Licensing:
#
#
#  Modified:
#
#    13 May 2017
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, string FILE_NAME, the name of the file.
#
#    Input, string COMMENT, a comment, which may be empty ('');
#
#    Input, integer WIDTH, HEIGHT, the width and height of the graphics image.
#
#    Input, integer MAXVAL, the maximum allowed gray value.
#
#    Input, integer GRAY[WIDTH,HEIGHT], gray values between 0 and MAXVAL.
#
file_type = 'P2'

file_handle = open ( file_name, 'wt' )
file_handle.write ( "%s\n" % ( file_type ) )
file_handle.write ( "#%s\n" % ( comment ) )
file_handle.write ( "%d %d\n" % ( width, height ) )
file_handle.write ( "%d\n" % ( maxval ) )

for i in range ( height ):
for j in range ( width ):
file_handle.write ( " %d" % ( gray[i,j] ) )
file_handle.write ( "\n" )

file_handle.close ( )

return

if ( __name__ == '__main__' ):
mandelbrot_pycuda ( )

スポンサーリンク

## IPython.display編¶

IPython.displayが一番手っ取り早い方法だが、残念ながらpgmは扱えない。

from IPython.display import Image, display
display(Image("mandelbrot_pycuda.pgm"))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-b0301ddaeb77> in <module>()
1 from IPython.display import Image, display
----> 2 display(Image("mandelbrot_pycuda.pgm"))

~/.pyenv/versions/py365/lib/python3.6/site-packages/IPython/core/display.py in __init__(self, data, url, filename, format, embed, width, height, retina, unconfined, metadata)
1140
1141         if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
-> 1142             raise ValueError("Cannot embed the '%s' image format" % (self.format))
1143         if self.embed:
1144             self._mimetype = self._MIMETYPES.get(self.format)

ValueError: Cannot embed the 'pgm' image format
スポンサーリンク

## matplotlib.image編¶

matplotlib.imageも簡単な方法だが、これもpgmは扱えない。

import matplotlib.pyplot as plt
import matplotlib.image as mpi

plt.imshow(img)
plt.show()

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-7-bdea4ce29068> in <module>()
2 import matplotlib.image as mpi
3
5 plt.imshow(img)
6 plt.show()

1355
1356     if ext not in handlers:
1358         if im is None:
1359             raise ValueError('Only know how to handle extensions: %s; '

1333         except ImportError:
1334             return None
-> 1335         with Image.open(fname) as image:
1336             return pil_to_array(image)
1337

~/.pyenv/versions/py365/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
2588         fp.close()
2589     raise IOError("cannot identify image file %r"
-> 2590                   % (filename if filename else fp))
2591
2592 #

OSError: cannot identify image file 'mandelbrot_pycuda.pgm'
スポンサーリンク

## PIL.Image編¶

from PIL import Image

img = Image.open('mandelbrot_pycuda.pgm')
plt.imshow(img)
plt.show()

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-43-62af1b48e076> in <module>()
1 from PIL import Image
2
----> 3 img = Image.open('mandelbrot_pycuda.pgm')
4 plt.imshow(img)
5 plt.show()

~/.pyenv/versions/py365/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
2588         fp.close()
2589     raise IOError("cannot identify image file %r"
-> 2590                   % (filename if filename else fp))
2591
2592 #

OSError: cannot identify image file 'mandelbrot_pycuda.pgm'
スポンサーリンク

## opencv編¶

やはり最後はopencvということで、これだとpgm画像を表示できる。

import numpy as np
import cv2
import matplotlib.pyplot as plt

# Load an color image in grayscale
plt.imshow(img, cmap='gnuplot2')
plt.show()

import numpy as np
import cv2
import matplotlib.pyplot as plt