且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中使用OpenCV对图像进行像素化?

更新时间:2022-06-16 23:30:25

EDIT ^ 2

在自己的帮助下,我将 Mark Setchell的答案(上面提到的最重要的答案)移到了简单的OpenCV Python代码. (请查看我的答案的修订历史,以循环查看旧版本.)

EDIT^2

With the help of himself, I moved Mark Setchell's answer, which is the above mentioned top answer, to plain OpenCV Python code. (Have a look at the revision history of my answer to see the old version using a loop.)

import cv2

# Input image
input = cv2.imread('images/paddington.png')

# Get input size
height, width = input.shape[:2]

# Desired "pixelated" size
w, h = (16, 16)

# Resize input to "pixelated" size
temp = cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR)

# Initialize output image
output = cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)

cv2.imshow('Input', input)
cv2.imshow('Output', output)

cv2.waitKey(0)

输入(来自链接的问题):

Input (from linked question):

输出:

免责声明:我是Python的新手,尤其是OpenCV(胜利的C ++)的Python API.非常欢迎评论,改进,强调Python的执行!!

Disclaimer: I'm new to Python in general, and specially to the Python API of OpenCV (C++ for the win). Comments, improvements, highlighting Python no-gos are highly welcome!