5

I want to resize png picture 476x402 to 439x371, and I used resize method of PIL(image) or opencv, however, it will loss some sharp. After resize, The picture becomes blurred. How to resize(shrink) image without losing sharpness with use python?

from skimage import transform, data, io
from PIL import Image
import os
import cv2

infile = 'D:/files/script/org/test.png'
outfile = 'D:/files/script/out/test.png'

''' PIL'''
def fixed_size1(width, height):
    im = Image.open(infile)
    out = im.resize((width, height),Image.ANTIALIAS)
    out.save(outfile)

''' open cv'''
def fixed_size2(width, height):
    img_array = cv2.imread(infile)
    new_array = cv2.resize(img_array, (width, height), interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(outfile, new_array)


def fixed_size3(width, height):
    img = io.imread(infile)
    dst = transform.resize(img, (439, 371))
    io.imsave(outfile, dst)

fixed_size2(371, 439)

src:476x402 resized:439x371

GGS of Future
  • 53
  • 1
  • 4

1 Answers1

5

How can you pack 2000 pixels into a box that only holds 1800? You can't.

Putting the same amount of information (stored as pixels in your source image) into a smaller pixelarea only works by

  • throwing away pixels (i.e. discarding single values or by cropping an image which is not what you want to do)
  • blending neighbouring pixels into some kind of weighted average and replace say 476 pixels with slightly altered 439 pixels

That is exactly what happens when resizing images. Some kind of algorithm (interpolation=cv2.INTER_CUBIC, others here) tweaks the pixel values to merge/average them so you do not loose too much of information.

You can try to change the algorithm or you can apply further postprocessing ("sharpening") to enrich the contrasts again.

Upon storing the image, certain formats do "lossy" storage to minimize file size (JPG) others are lossless (PNG, TIFF, JPG2000, ...) which further might blur your image if you choose a lossy image format.


See

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Patrick Artner
  • 48,339
  • 8
  • 43
  • 63
  • I tried other InterpolationFlags arg. it still get blur. and I just transform png, opencv has other method for Sharpen image, I trying use them to restore source as possible – GGS of Future Oct 10 '20 at 11:34