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 code for you

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

Share

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

In this issue, the editor will bring you about how to use Python to help you type the code. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Introduction

The so-called pixel map is to make a granulated effect on the image, making it have a wonderful sense of obscurity. Without saying much, let's take a look at an effect picture first.

How's it going? is it good? Now, we use Python to achieve this pixel effect.

1 Environment

Operating system: Windows

Python version: 3.7.3

2 demand analysis

One of the simplest implementation ideas is to divide the picture into some pixel blocks after opening the picture, and then process the image information in these pixel blocks (modify the RGB value in the image).

Here we use Numpy library and PIL library to achieve this requirement, the latter is used to read and save images, and all the image processing actions involved are realized by Numpy.

For the introduction of NumPy module and PIL module, please refer to the following.

NumPy (Numerical Python) is an extension library of Python language, which not only supports a large number of dimensional array and matrix operations, but also provides a large number of mathematical function libraries for array operations.

PIL (Python Imaging Library) is a commonly used image processing library of Python, while Pillow is a friendly Fork of PIL, which provides a wide range of file format support and powerful image processing capabilities, including image storage, image display, format conversion and basic image processing operations.

These two modules are not built in Python, are third-party modules, and can be installed directly in the following ways.

Pip install numpypip install Pillow note that to use the PIL module, you need a direct install Pillow module.

3 code implementation

First, import the module we want to use.

Import numpy as npfrom PIL import Image

Next, to deal with the picture, we first have to open a picture, as follows

Data = Image.open ("P:\ Personal\ LuoShen.xpg")

Then convert the image into the Numpy array for the next step.

Im1 = np.array (data)

The core idea dealt with here is also very simple, mainly through the RGB of the intermediate value to reassign the RGB of the selected range block.

Im1 [y pixel + pixel, x pixel] = im1 [y + (pixel / / 2)] [x + (pixel / / 2)]

The x and y here respectively refer to the coordinate values of the horizontal and vertical pixels of our image, while pixel refers to the size of the pixel block we want to deal with this image. The smaller the unit pixel block (Pixel value) we set, the more accurate the pixel image will be.

Of course, if the unit pixel block is set too small, the image will not be able to see the effect, as for how large the value is appropriate, you need to try it yourself. For images of different sizes, in order to achieve the best pixel display effect, the size of the unit pixel block that needs to be set is also different.

We need to specify a processing range of the image, and pixel-based processing of each coordinate (pixel) point in that range.

For y in range (Start_coordinate [1], End_coordinate [1], pixel): for x in range (Start_coordinate [0], End_coordinate [0], pixel): pass

After the processing is complete, we convert the Numpy array back to the image.

Im2 = Image.fromarray (im1.astype (np.uint8))

Finally, the processed image is shown.

Im2.show ()

4 code panoramic display

Import numpy as np

From PIL import Image

Def to_pixelBlock (pixel, Start_coordinate, End_coordinate):

''

: param pixel: the element size per pixel block

: param Start_coordinate: the starting coordinate (pixel) point of processing, in tuple form

: param End_coordinate: the end coordinate (pixel) point of the process, in tuple form

: return:

Through the RGB of the intermediate value, the RGB of the selected range block is reassigned. The smaller the unit pixel block (Pixel value) is set, the more accurate the pixel map is generated.

''

# read pictures and convert them from PIL image to NumPy array

Im1 = np.array (Image.open ("P:\ Personal\ LuoShen.jpg"))

# traverse all coordinate (pixel) points in the range to be processed

For y in range (Start_coordinate [1], End_coordinate [1], pixel):

For x in range (Start_coordinate [0], End_coordinate [0], pixel):

# reassign the RGB of the selected range block through the RGB of the intermediate value

Im1 [y pixel + pixel, x pixel] = im1 [y + (pixel / / 2)] [x + (pixel / / 2)]

# convert NumPy array to PIL image

Im2 = Image.fromarray (im1.astype (np.uint8))

# display the processed image

Im2.show ()

If _ _ name__ = ='_ _ main__':

# set the range of pixels to be processed, and how many blocks of pixels are used to generate the final effect image

To_pixelBlock (10,0,0), (1280)

Using the cooperation of PIL and Numpy, the image pixel processing is realized in just a few lines of code. Of course, this is a simple implementation, and if you want to achieve richer processing effects, you can also use CV2 to achieve.

The above is the editor for you to share how to use Python to help you code, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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