Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Oil painting effect in Image Special effects by Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to achieve Python image special effects in the oil painting effect, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

A basic principle

As shown in the two pictures below, the use of oil painting in the right place will make the image look artistic all of a sudden.

The image taken.

Turn into oil painting

So how do you convert an image into an oil painting pattern? In order to convert an ordinary image into an oil painting, the following steps are generally required:

(1) convert an image to a grayscale image

(2) divide the image into small boxes, and count the pixel value of each pixel.

(3) quantify the gray value of the pixels in the other box (please refer to my previous article on quantization), and count the number of pixels of different levels.

(4) find the pixels with the most gray levels in the box, and average the gray values of these pixels.

(5) replace the original pixel value with the average value.

Two code implementation

First of all, there is a classic operation to read image information:

"Author:XiaoMadate:2021/12/10" import cv2import numpy as npimg = cv2.imread (rattlee:\ From Zhihu\ For the desk\ cvyouhua.jpg') img = cv2.resize (img, dsize = None, fx = 0.2, fy = 0.2) # because the algorithm has a large amount of computation, first adjust its size cv2.imshow ('W0, img) cv2.waitKey (0) # to get the image width and height height, width = img.shape [: 2] print (height, width)

The image information is as follows:

Oil painting special effects on the image, the code has been added comments, there are unclear comments can be discussed in the comments area or private messages, see will reply to you:

Gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # convert an image to a grayscale image dst = np.zeros ((height, width, 3) Np.uint8) # create an all-zero matrix the same size as the original image #-# use for loop nesting to traverse every pixel in the image #- -for i in range (2 Height-2): for j in range (2, width-2): #-# box is 4x4 Quantify the pixels in the other frame and record the number of pixels of different grades #-array1 = np.zeros (8, np.uint8) # quantify the value of the pixels to 8 Define the number of pixel points recorded in the array for m in range (- 2,2): for n in range (- 2,2): P1 = int (gray [iSIMM, junin] / 32) # quantization operation array1 [p1] = array1 [p1] + this array is used to record pixels at different quantization levels For example, array1 [0] represents the number of pixels below the rank, that is, the number of pixels with a pixel value of (0,64) #-# find the maximum value in the array above That is, to find the largest number of pixel levels #-currentMax = array1 [0] l = encapsulate the position of the maximum in the array for k in range (0,8): if currentMax

< array1[k]: currentMax = array1[k] l = k #------------------------ #求数目最多的像素等级的平均 #------------------------ for m in range(-2, 2): for n in range(-2, 2): if gray[i + m, j + n] >

= (l * 32) and gray [I + m, j + n] = (l * 32) and gray [I + m, j + n]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report