{"id":812,"date":"2021-11-17T21:10:40","date_gmt":"2021-11-18T03:10:40","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=812"},"modified":"2021-11-18T10:01:23","modified_gmt":"2021-11-18T16:01:23","slug":"bilinear-interpolation-to-resize-an-image","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=812","title":{"rendered":"Bilinear Interpolation to Resize an Image"},"content":{"rendered":"<p>Bilinear interpolation can be used to resize an image, in particular to make it larger.\u00a0 See my video at\u00a0<a href=\"https:\/\/youtu.be\/mxTUZW4CR_w\">https:\/\/youtu.be\/mxTUZW4CR_w<\/a> for the the details.<\/p>\n<p>Here is the test image that I process in that video: <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-820\" src=\"https:\/\/www.brezeale.com\/wp-content\/uploads\/2021\/11\/book_fausett_small.jpg\" alt=\"\" width=\"73\" height=\"130\" \/><\/p>\n<p>Here is the code:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n'''\r\n    use bilinear interpolation to resize an image\r\n'''\r\n\r\nimport numpy as np\r\nfrom PIL import Image\r\n\r\n\r\ndef resizeImage(name) :\r\n    img1 = Image.open(name)\r\n\r\n    old = np.asarray(img1)    # convert to Numpy array\r\n    rows, cols, layers = old.shape\r\n    new = np.zeros( (2*rows - 1, 2*cols - 1, layers) )\r\n    print(&quot;original dimensions:&quot;, old.shape)\r\n\r\n    for layer in range(3) :\r\n        new&#x5B;:, :, layer] = resizeLayer(old&#x5B;:, :, layer])\r\n\r\n    # convert the values to unsigned, 8-bit integers\r\n    new = new.astype(np.uint8)\r\n    print(&quot;     new dimensions:&quot;, new.shape)\r\n\r\n    img2 = Image.fromarray(new)  # convert back to Image\r\n    newName = &quot;big-&quot; + name \r\n    img2.save(newName)\r\n\r\n\r\ndef resizeLayer(old) :\r\n    rows, cols = old.shape\r\n\r\n    rNew = 2*rows - 1\r\n    cNew = 2*cols - 1\r\n    new = np.zeros((rNew, cNew))\r\n\r\n    # move old points\r\n    new&#x5B;0:rNew:2, 0:cNew:2] = old&#x5B;0:rows, 0:cols]\r\n\r\n    ''' alternative approach\r\n    # something like this would be necessary in languages \r\n    #   that don't support slicing\r\n    new = np.zeros( (2*rows - 1, 2*cols - 1) )\r\n    for r in range(rows) :\r\n        for c in range(cols) :\r\n            new&#x5B;2*r, 2*c] = old&#x5B;r,c]\r\n\r\n    rows, cols = new.shape\r\n    '''\r\n\r\n    # produce vertical values\r\n    new&#x5B;1:rNew:2, :] = (new&#x5B;0:rNew-1:2, :] + new&#x5B;2:rNew:2, :]) \/ 2\r\n    ''' alternative approach\r\n    for r in range(1, rows, 2) :\r\n        for c in range(0, cols, 2) :\r\n            # top + bottom\r\n            new&#x5B;r,c] = ( new&#x5B;r-1,c] + new&#x5B;r+1,c] ) \/\/ 2\r\n    '''\r\n\r\n    # produce horizontal values\r\n    new&#x5B;:, 1:cNew:2] = (new&#x5B;:, 0:cNew-1:2] + new&#x5B;:, 2:cNew:2]) \/ 2\r\n    ''' alternative approach\r\n    for r in range(0, rows, 2) :\r\n        for c in range(1, cols, 2) :\r\n            # left + right\r\n            new&#x5B;r,c] = ( new&#x5B;r,c-1] + new&#x5B;r,c+1] ) \/\/ 2\r\n    '''\r\n\r\n    # produce center values\r\n    new&#x5B;1:rNew:2, 1:cNew:2] = (new&#x5B;0:rNew-2:2, 0:cNew-2:2] + \r\n                               new&#x5B;0:rNew-2:2, 2:cNew:2] + \r\n                               new&#x5B;2:rNew:2, 0:cNew-2:2] + \r\n                               new&#x5B;2:rNew:2, 2:cNew:2] ) \/ 4\r\n    ''' alternative approach\r\n    for r in range(1, rows, 2) :\r\n        for c in range(1, cols, 2) :\r\n            # top + bottom + left + right\r\n            new&#x5B;r,c] = ( new&#x5B;r-1,c] + new&#x5B;r+1,c] + new&#x5B;r,c-1] + new&#x5B;r,c+1] ) \/\/ 4\r\n    '''\r\n\r\n    return new\r\n\r\n####################  main  ####################\r\n'''\r\ntest = np.array(&#x5B;&#x5B;1, 3,  5],\r\n                 &#x5B;3, 5,  7],\r\n                 &#x5B;5, 7,  9],\r\n                 &#x5B;7, 9, 11]])\r\nprint(test)\r\ntest = resizeLayer(test)\r\nprint()\r\nprint(test)\r\n'''\r\n\r\nfilename = 'book_fausett_small.jpg'\r\nresizeImage(filename) \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bilinear interpolation can be used to resize an image, in particular to make it larger.\u00a0 See my video at\u00a0https:\/\/youtu.be\/mxTUZW4CR_w for the the details. Here is the test image that I process in that video: Here is the code: &#8221;&#8217; use bilinear interpolation to resize an image &#8221;&#8217; import numpy as np from PIL import Image def resizeImage(name) : img1 = Image.open(name) old = np.asarray(img1) # convert to Numpy array rows, cols, layers = old.shape new = np.zeros( (2*rows &#8211; 1,&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=812\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[11,12,5],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-image","hentry","category-math","category-numerical-methods-math","category-python-programming","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=812"}],"version-history":[{"count":10,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/812\/revisions"}],"predecessor-version":[{"id":824,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/812\/revisions\/824"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}