📜 ⬆️ ⬇️

Playing with images in Python

In this article, I would like to make out various ways to convert images using Python. For examples, I decided to take a few of the most famous. The article will not be anything complicated, it is focused mainly on beginners.
Test picture:


Training

import random from PIL import Image, ImageDraw #  . mode = int(input('mode:')) #  . image = Image.open("temp.jpg") # . draw = ImageDraw.Draw(image) #   . width = image.size[0] # . height = image.size[1] # . pix = image.load() #  . 


Grayscale

To obtain this transformation, it is necessary to “average” each pixel.
 if (mode == 0): for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] S = (a + b + c) // 3 draw.point((i, j), (S, S, S)) 


')
Sepia


To get sepia, you need to calculate the average value and take some factor.
middle = (R + G + B) / 3
The first pixel value (R) = middle + 2 * k
The second pixel value (G) = middle + k
Third pixel value (B) = middle
 if (mode == 1): depth = int(input('depth:')) for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] S = (a + b + c) // 3 a = S + depth * 2 b = S + depth c = S if (a > 255): a = 255 if (b > 255): b = 255 if (c > 255): c = 255 draw.point((i, j), (a, b, c)) 


depth = 30

Negative

Now we learn how to get negative.
It's very simple, just subtract each pixel value from 255.
 if (mode == 2): for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] draw.point((i, j), (255 - a, 255 - b, 255 - c)) 



Add noise

Here, everything is quite simple.
We will always add a random value to the pixel. The greater the spread of these values, the more noise.
 if (mode == 3): factor = int(input('factor:')) for i in range(width): for j in range(height): rand = random.randint(-factor, factor) a = pix[i, j][0] + rand b = pix[i, j][1] + rand c = pix[i, j][2] + rand if (a < 0): a = 0 if (b < 0): b = 0 if (c < 0): c = 0 if (a > 255): a = 255 if (b > 255): b = 255 if (c > 255): c = 255 draw.point((i, j), (a, b, c)) 


factor = 70

Brightness

To adjust the brightness to each pixel we will add a certain value. If it is> 0, then the picture becomes brighter, otherwise darker.
 if (mode == 4): factor = int(input('factor:')) for i in range(width): for j in range(height): a = pix[i, j][0] + factor b = pix[i, j][1] + factor c = pix[i, j][2] + factor if (a < 0): a = 0 if (b < 0): b = 0 if (c < 0): c = 0 if (a > 255): a = 255 if (b > 255): b = 255 if (c > 255): c = 255 draw.point((i, j), (a, b, c)) 


factor = 100

factor = -100

Black and white image

Now all pixels should be divided into 2 groups: black and white.
To check the belonging to a particular group, we will look at what the pixel value is closer to: white or black.
 if (mode == 5): factor = int(input('factor:')) for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] S = a + b + c if (S > (((255 + factor) // 2) * 3)): a, b, c = 255, 255, 255 else: a, b, c = 0, 0, 0 draw.point((i, j), (a, b, c)) 


factor = 100

Conclusion

Save the result and delete the brush.
 image.save("ans.jpg", "JPEG") del draw 


Final code
 import random from PIL import Image, ImageDraw mode = int(input('mode:')) image = Image.open("temp.jpg") draw = ImageDraw.Draw(image) width = image.size[0] height = image.size[1] pix = image.load() if (mode == 0): for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] S = (a + b + c) // 3 draw.point((i, j), (S, S, S)) if (mode == 1): depth = int(input('depth:')) for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] S = (a + b + c) // 3 a = S + depth * 2 b = S + depth c = S if (a > 255): a = 255 if (b > 255): b = 255 if (c > 255): c = 255 draw.point((i, j), (a, b, c)) if (mode == 2): for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] draw.point((i, j), (255 - a, 255 - b, 255 - c)) if (mode == 3): factor = int(input('factor:')) for i in range(width): for j in range(height): rand = random.randint(-factor, factor) a = pix[i, j][0] + rand b = pix[i, j][1] + rand c = pix[i, j][2] + rand if (a < 0): a = 0 if (b < 0): b = 0 if (c < 0): c = 0 if (a > 255): a = 255 if (b > 255): b = 255 if (c > 255): c = 255 draw.point((i, j), (a, b, c)) if (mode == 4): factor = int(input('factor:')) for i in range(width): for j in range(height): a = pix[i, j][0] + factor b = pix[i, j][1] + factor c = pix[i, j][2] + factor if (a < 0): a = 0 if (b < 0): b = 0 if (c < 0): c = 0 if (a > 255): a = 255 if (b > 255): b = 255 if (c > 255): c = 255 draw.point((i, j), (a, b, c)) if (mode == 5): factor = int(input('factor:')) for i in range(width): for j in range(height): a = pix[i, j][0] b = pix[i, j][1] c = pix[i, j][2] S = a + b + c if (S > (((255 + factor) // 2) * 3)): a, b, c = 255, 255, 255 else: a, b, c = 0, 0, 0 draw.point((i, j), (a, b, c)) image.save("ans.jpg", "JPEG") del draw 

Source: https://habr.com/ru/post/163663/


All Articles