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 the special effect processing of pictures with python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the python how to achieve picture special effects processing related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python how to achieve picture special effects processing article will have a harvest, let's take a look.

Foreword:

For picture processing, we can often see it in our daily life.

For example, before posting a moments, we need to add a filter to our photos; when uploading avatars, we need to crop the photos, which are all photo processing.

Original image to be processed:

Black and white special effects

After the picture is processed, it becomes black and white.

Set the values of the three channels of the pixel to: r*0.299+g*0.587+b*0.114

Effect.

Black and white special effects:

Code:

#! / usr/bin/env python# encoding: utf-8import numpy as npfrom PIL import Imageclass picture:'''This is a main Class The file contains all documents.One document contains paragraphs that have several sentencesIt loads the original file and converts the original file to new contentThen the new content will be saved by this class'''def _ _ init__ (self): self.path = 'assets/picture.jpeg'def hello (self):' 'This is a welcome speech:return: self'''print (' *'* 50) print ('* 20 + 'black and white image conversion effects') print ('* 5 +' Author: autofelix Date: 2022-01-17 13 return selfdef run 14') print ('* 50) return selfdef run (self):''The program entry'''im = self.to_black_white () im.show () im.save (' assets/black_white.jpeg') def to_black_white (self):''Picture to black white'''im = np.asarray (Image.open (self.path). Convert (' RGB')) trans = np.array ([0.299) 0.587, 0.114], [0.299, 0.587, 0.114], [0.299, 0.587, 0.114]). Transpose () im = np.dot (im, trans) return Image.fromarray (np.array (im) .astype ('uint8')) if _ name__ = =' _ main__':picture (). Run () II. Special effects

After the picture is processed, it becomes a fleeting special effect.

Square the value of the R channel and multiply it by a parameter

Effect.

Fleeting special effects:

Code:

#! / usr/bin/env python# encoding: utf-8import numpy as npfrom PIL import Imageclass picture:'''This is a main Class The file contains all documents.One document contains paragraphs that have several sentencesIt loads the original file and converts the original file to new contentThen the new content will be saved by this class'''def _ _ init__ (self): self.path = 'assets/picture.jpeg'def hello (self):' 'This is a welcome speech:return: self'''print (' *'* 50) print ('* 20 + 'year of image conversion special effects') print ('* 5 +' Author: autofelix Date: 2022-01-17 13 return selfdef run 14') print ('* 50) return selfdef run (self):''The program entry'''im = self.fleeting () im.show () im.save (' assets/fleeting.jpeg') def fleeting (self) Params=12):''Picture to fleeting'''im = np.asarray (Image.open (self.path). Convert (' RGB')) im1 = np.sqrt (im * [1.0,0.0,0.0]) * paramsim2 = im * [0.0,1.0 Im = im1 + im2return Image.fromarray (np.array (im) .astype ('uint8')) if _ _ name__ = =' _ main__':picture (). Hello (). Run () 3. Special effects of old movies

After the picture is processed, it becomes an old movie special effect.

The values of the three channels of the pixel, the values of the three channels, and the values of the three channels are multiplied by three parameters, respectively, and then summed. Finally, the value of more than 255 is set to 255.

Effect.

Old movie special effects:

Code:

#! / usr/bin/env python# encoding: utf-8import numpy as npfrom PIL import Imageclass picture:'''This is a main Class The file contains all documents.One document contains paragraphs that have several sentencesIt loads the original file and converts the original file to new contentThen the new content will be saved by this class'''def _ _ init__ (self): self.path = 'assets/picture.jpeg'def hello (self):' 'This is a welcome speech:return: self'''print (' *'* 50) print ('* 20 + 'old movie with special effects for picture conversion') print ('* 5 + Author: autofelix Date: 2022-01-17 13 return selfdef run 14') print ('*'* 50) return selfdef run (self):''The program entry'''im = self.old_film () im.show () im.save (' assets/old_film.jpeg') def old_film (self): 'Picture to old film'''im = np.asarray (Image.open (self.path). Convert (' RGB')) trans = np.array ([0.393) 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]). Transpose () im = np.dot (im, trans) .clip (max=255) return Image.fromarray (np.array (im) .astype ('uint8')) if _ name__ = =' _ main__':picture (). Hello () .run () IV, reverse color special effects

After the picture is processed, it becomes a reverse color special effect.

This is the simplest, using 255 to subtract the original value of each channel

Effect.

Reverse color special effects:

Code:

#! / usr/bin/env python# encoding: utf-8import numpy as npfrom PIL import Imageclass picture:'''This is a main Class The file contains all documents.One document contains paragraphs that have several sentencesIt loads the original file and converts the original file to new contentThen the new content will be saved by this class'''def _ _ init__ (self): self.path = 'assets/picture.jpeg'def hello (self):' 'This is a welcome speech:return: self'''print (' *'* 50) print ('* 20 + 'image conversion effects') print ('* 5 +' Author: autofelix Date: 2022-01-17 13 return selfdef run 14') print ('* 50) return selfdef run (self):''The program entry'''im = self.reverse () im.show () im.save (' assets/reverse.jpeg') def reverse (self):''Picture to reverse'''im = 255-np.asarray (Image.open (self.path) .convert (' RGB')) return Image.fromarray (np.array (im) .astype ('uint8) ) if _ _ name__ ='_ _ main__':picture (). Hello (). Run () on "how to implement image special effects in python" is introduced here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve picture special effects in python". If you want to learn more knowledge, 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