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 use Python to convert a picture into a dynamic character painting

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use Python to convert pictures into dynamic character paintings". In daily operation, I believe many people have doubts about how to use Python to convert pictures into dynamic character paintings. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how to use Python to convert pictures into dynamic character paintings"! Next, please follow the small series to learn together!

still pictures

First of all, we will demonstrate the conversion of static images to character paintings. The Python library mainly used for function implementation is OpenCV, and the installation can be done by using the ip install opencv-python command.

The basic idea of function realization is: cluster target information into 3 or 5 categories by clustering, the darkest category is represented by numerical density, the shaded category is represented by horizontal bar (-), and the bright part is represented by blank.

The main code implementation is as follows:

def img2strimg(frame, K=5): if type(frame) != np.ndarray: frame = np.array(frame) height, width, *_ = frame.shape frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame_array = np.float32(frame_gray.reshape(-1)) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) flags = cv2.KMEANS_RANDOM_CENTERS #Get labels(category), centroids(centroid) compactness, labels, centroids = cv2.kmeans(frame_array, K, None, criteria, 10, flags) centroids = np.uint8(centroids) # labels have several centroids arranged in random order, so you need to handle the centroids simply centroids = centroids.flatten() centroids_sorted = sorted(centroids) #Get different degrees of centroids, 0 being the darkest centroids_index = np.array([centroids_sorted.index(value) for value in centroids]) bright = [abs((3 * i - 2 * K) / (3 * K)) for i in range(1, 1 + K)] bright_bound = bright.index(np.min(bright)) shadow = [abs((3 * i - K) / (3 * K)) for i in range(1, 1 + K)] shadow_bound = shadow.index(np.min(shadow)) labels = labels.flatten() #Convert labels to actual shading list labels = centroids_index[labels] #Parse List labels_picked = [labels[rows * width:(rows + 1) * width:2] for rows in range(0, height, 2)] canvas = np.zeros((3 * height, 3 * width, 3), np.uint8) #Create a white canvas that is three times the width of the original canvas.fill(255) y = 8 for rows in labels_picked: x = 0 for cols in rows: if cols

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

Internet Technology

Wechat

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

12
Report