thumbnails make terrible canvas prints

Posted on Sun 12 April 2020 in Projects

upscaling and sharpening images:

scipy.ndimage.zoom performs spline interpolation to biggify image.

cv2.filter2D with your choice of sharpening kernel

In [1]:
import numpy as np
import scipy.ndimage as ndimage
import matplotlib.image as mpimg 
import matplotlib.pyplot as plt
import cv2
In [10]:
img = mpimg.imread('img.jpg')
plt.imshow(img);
In [6]:
img.shape
Out[6]:
(2447, 3009, 3)
In [7]:
big_img = ndimage.zoom(img, (2,2,1))   #first two numbers of tuple are zoom factor (for x and y)
In [8]:
big_img.shape
Out[8]:
(4894, 6018, 3)

note the new axes:

In [11]:
plt.imshow(big_img)

sharpen:

In [24]:
kernel1 = np.array([[0, -1, 0], 
                   [-1, 5, -1], 
                   [0, -1, 0]])

kernel2 = np.array([[-1, -1, -1],
                    [-1, 9, -1],
                    [-1, -1, -1]])


# Sharpen image
sharpened = cv2.filter2D(big_img, -1, kernel2)   # choose which kernel

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(14,9))
ax1.imshow(big_img)
ax1.set_title('original')
ax2.imshow(sharpened)
ax2.set_title('sharpened, kernel2');
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: