[Python class notes] Python data processing (1)

Python data processing (1)

Data collation


Count the number of characters in a file

The file is day.txtas follows:
Insert picture description here

#统计这个文件中day的个数
import re     #re是匹配字符串的模块,该模块中提供的很多功能是基于正则表达式实现的
f=open(r'文件路径 day.txt')
source=f.read()
print(source)
f.close()
r='day'
s=len(re.findall(r,source)) 
#在字符串中找到所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
print(s)

Output:

have a good day,have a good day,have a good day
3

Replace the string and save it to another text file

# 把day.txt 中的day全部换为"day2",并把结果保存day2.txt中
import re
f1 = open(r'文件路径 day.txt')
f2 = open(r'文件路径 day2.txt','w')
for s in f1.readlines():
    f2.write(s.replace('day','day2'))
f1.close()
f2.close()

day2.txtAs shown:
Insert picture description here


Image Processing

PIL: Python Imaging Library image processing library.
There are nine different modes in PIL, namely: 1, L, P, RGB, RGBA, CMYK, YCbCr, I, F. common:
  • Mode "1" is a binary image, either black or white. Each pixel is represented by 8 bits, 0 for black and 255 for white.
  • Mode "L" is a gray image, each pixel of which is represented by 8 bits, 0 means black, 255 means white, and other numbers indicate different gray levels.
  • Mode "P" is an 8-bit color image, each pixel of which is represented by 8 bits, and the corresponding color value is queried according to the color palette.
  • The mode "RGBA" is a 32-bit color image, each pixel of which is represented by 32 bits, of which 24 bits represent three channels of red, green and blue, and 8 bits represent an alpha channel, which is a transparent channel.
  • The mode "CMYK" is a 32-bit color image, each pixel of which is represented by 32 bits, printing a four-color mode.
  • The mode "YCbCr" is a 24-bit color image, and each pixel is represented by 24 bits.
#将模式为"RGB"的图像转换为模式"1"
from PIL import Image
lena =Image.open(r"图片路径 qs.jpg")
lena.show()
print(lena.mode)
print(lena.getpixel((0,0)))
lena_1 = lena.convert("1")
print(lena_1.mode)
print(lena_1.size)
print(lena_1.getpixel((0,0)))
print(lena_1.getpixel((10,10)))
lena_1.show()

Output:

RGB
(255, 255, 255)
1
(510, 723)
255
255

Original image:
Insert picture description here
After conversion:
Insert picture description here


Image rotation

#图像的旋转
from PIL import Image
from pylab import *
im=Image.open(r"C:\Users\图片路径 qs.jpg")
out = im.resize((128, 128))     
out.show()
out = im.rotate(45)   
out.show()
out = im.transpose(Image.FLIP_LEFT_RIGHT)  #左右对换。
out.show()
out = im.transpose(Image.FLIP_TOP_BOTTOM)  #上下对换
out.show()
out = im.transpose(Image.ROTATE_90)   
out.show()

Output rotated photos

Please make up


Image graying

#图像的灰化
from PIL import Image
from pylab import *
im=Image.open(r"图片路径 qs.jpg")
im.show()
#from PIL import Image
#from pylab import *
im = array(Image.open(r"图片路径 qs.jpg").convert('L'))
im2 = 255 - im 
im3 = (100.0/255) * im + 100   
im4 = 255.0 * (im/255.0)**2
figure(figsize=(16,8))
subplot(221)
title('f(x) = x')
gray()
imshow(im) 
subplot(222)
title('f(x) = 255 - x')
imshow(im2)
subplot(223)
title('f(x) = (100/255)*x + 100')
imshow(im3)
subplot(224)
title('f(x) =255 *(x/255)^2')
imshow(im4)
print(int(im.min()),int(im.max()))
print(int(im2.min()),int(im2.max()))
print(int(im3.min()),int(im3.max()))
print(int(im4.min()),int(im4.max()))
show()

Output:

0 255
0 255
100 200
0 255

Insert picture description here


Adjust pixels

from PIL import Image
im=Image.open(r"C:\Users\午后阳光\Desktop\qs.jpg")
im.show()
w,h=im.size
print(w)
print(h)
out = im.resize((800,800),Image.ANTIALIAS)
out.show()
w1,h1=out.size
print(w1)
print(h1)

Output:

510
723
800
800

Insert picture description here


Published 18 original articles · Likes6 · Visits 1859

Guess you like

Origin blog.csdn.net/qq_43479203/article/details/105363798