Resize Bitmap Images

This example will show that a bitmap image is just an array of numbers.

Using the array programming capabilities of the J programming language, we may manipulate a bitmap image like any other array.


In [1]:
NB. Load required scripts
load 'graphics/bmp viewmat'




In [2]:
NB. Load bitmap image
img =: readbmp jpath '~user/Jupyter_Notebook_J_Example_Data/galaxy.bmp'

In [3]:
NB. Show bitmap image
viewrgb img



In [4]:
NB. Define a utility verb to resize an array (bitmap image)
NB. Resize image (maintain aspect ratio)
NB. Usage: (w,h) resize <image-data>
resize =: 4 : 0
   ss =. $y                         NB. Source size
   ds =. <.ss*<./(|.x)%ss           NB. Destination size
   fa =. ss%ds                      NB. Factor height/width
   oh =. i. 0{ds                    NB. Height array
   ow =. i. 1{ds                    NB. Width array
   xh =. <. (0{fa) * oh             NB. Multiply with factor and floor
   xw =. <. (1{fa) * ow             NB. Multiply with factor and floor
   oi =. (<(xh;xw)){y               NB. Generator output image
)

In [5]:
NB. Show the original dimensions of the bitmap image (array)
$img


320 480

In [6]:
NB. Resize the original bitmap image to half its size
viewrgb (240 160 resize img)



In [7]:
NB. Resize the original bitmap image to double its size
viewrgb (960 640 resize img)



In [8]:
NB. Define another utility verb to resize an array (bitmap image)
NB. Resize image (NOT maintain aspect ratio)
NB. Usage: (w,h) resize <image-data>
resize2 =: 4 : 0
   ss =. $y                         NB. Source size
   ds =. <.ss*(|.x)%ss              NB. Destination size
   fa =. ss%ds                      NB. Factor height/width
   oh =. i. 0{ds                    NB. Height array
   ow =. i. 1{ds                    NB. Width array
   xh =. <. (0{fa) * oh             NB. Multiply with factor and floor
   xw =. <. (1{fa) * ow             NB. Multiply with factor and floor
   oi =. (<(xh;xw)){y               NB. Generator output image
)

In [9]:
NB. Resize the original bitmap image without maintain aspect ration
viewrgb (300 300 resize2 img)



In [ ]: