Reputation: 73
I have an image and I want to resize and expand this image without spoiling it.
The image:
The image size:
The image size I want it resized to:
I tried all the known methods:
from PIL import Image
path = "image.png"
w, h = 2000, 2100
img = Image.open(path)
img = img.resize((w, h), Image.ANTIALIAS)
img.save("re_image.png", "PNG")
import cv2
path = "image.png"
w, h = 2000, 2100
img = cv2.imread(path)
img = cv2.resize(img, (w, h), interpolation = cv2.INTER_AREA)
cv2.imwrite("re_image.png", img)
The result is:
Upvotes: 5
Views: 3409
Reputation: 73
I created a simple function. Please give me your opinion.
The function is:
import cv2, numpy as np
def resize(path, sizes = (500, 800)):
nw, nh = sizes
g_img = cv2.imread(path)
gw, gh = g_img.shape[0], g_img.shape[1]
nw, nh = gw+510, gh+510
pw, ph = int(nw/gw), int(nh/gh)
if (pw == 0):pw = 1
if (ph == 0):ph = 1
nw, nh = pw*gw, ph*gh
cvimg = np.zeros((nh,nw,3), np.uint8)
for gy in range(gh):
y, h = gy*ph, (gy*ph)+ph
for gx in range(gw):
x, w = gx*pw, (gx*pw)+pw
cvimg[y:h, x:w] = g_img[gy:gy+1, gx:gx+1]
nsize = [nw, nh]
return cvimg, nsize
The result is:
Upvotes: -1
Reputation: 32189
Don't use the ANTIALIAS
filter. Since you want to preserve the sharpness of the edges, simply use the NEAREST
filter. This filter is the default if no argument is passed to the resample
parameter of Image.resize
img = img.resize((w, h))
This gives the expected result
Upvotes: 6
Reputation: 785
For your specific case which is a binary image, I think that you want to keep the edges sharp. Then I would suggest to use the flag cv2.INTER_NEAREST in your openCV implementation. You can check the effect of the different interpolation types, given in the documentation: https://docs.opencv.org/4.1.1/da/d54/group__imgproc__transform.html
I hope this helps!
Upvotes: 1