Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple Image scaling and rotation by Python

Shulou Source: shulou.com Published: 2022-06-02 02:10:07 10月04日 Update

This article mainly introduces Python how to achieve simple image scaling and rotation, the article is very detailed, has a certain reference value, interested friends must read it!

1. The image is scaled 1.2. Use the command import cv2# to scale def resize (img, k, inter): res = cv2.resize (img, None, fx=k, fy=k, interpolation=inter) return res

Parameter setting (interpolation):

0: nearest neighbor interpolation

1: bilinear interpolation

2: resampling based on local pixels

3: cubic interpolation based on the neighborhood of 4 pixels.

4: Lanczos interpolation based on 8 pixel neighborhood

Img_row = cv2.imread (ringing Picture4.jpg') cv2.imshow ('origin_picture', img_row) cv2.waitKey (0) inters = [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4] for inter in inters: res1 = resize (img_row, 2, inter) cv2.imshow (' res1') Res1) cv2.waitKey (0) position = 'pic_resize' +' _'+ str (inter) + '.jpg' cv2.imwrite (position, res1) 1.2. Introduction of principle

Brief introduction to the principles of several methods:

Nearest neighbor interpolation: bilinear interpolation of the pixel values of the nearest neighbor integer coordinate points in the target image: F (xline y) is a binary function, assuming that we know the values of four points f (x _ 0), f (x _ 1 ~ 1), f (x _ 0 ~ 1) and f (x _ 1 ~ 1 ~ y _ 0). These four points determine a rectangle, and we hope to get the function value of any point in the rectangle by interpolation. So we do two linear interpolation in the x direction and one linear interpolation in the y direction. After synthesis, the following formulas are obtained:

Resampling of local pixels: if the scale of the enlarged image is an integer multiple, similar to the nearest neighbor interpolation, if the magnification is not an integer multiple, linear interpolation cubic interpolation will be used: given 1 point, a=x0

Tags: Interpolation image nearest neighbor pixel effect complex function local interpolation integer weight condition edge information content region principle command polynomial direction Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB NVidia Shulou Information Huawei