python mosaic pictures
classification
Image processing-PIL-python to mosaic pictures
Start
rely
Pillow 5.2.0
python 3.7.7
Offer code
# magic remark
# -*- encoding:utf-8 -*-
from PIL import Image
def _mosaic(img):
s = img.size
img = img.resize((10,10))
img = img.resize(s)
return img
def mosaic(img,fx,fy,tx,ty):
c = img.crop((fx,fy,tx,ty))
c = _mosaic(c)
img.paste(c,(fx,fy,tx,ty))
return img
if(__name__ == '__main__'):
while(1):
flag = False
name = input('请输入文件名:')
try:
img = Image.open(name)
flag = True
except:
print('无法读取文件')
if(flag):
break
fx,fy,tx,ty = None,None,None,None
while(1):
flag = False
name = input('请输入需要打马赛克的区域(x1,y1,x2,y2):')
try:
name = [int(i) for i in name.split(',')]
if(len(name) != 4):
raise ValueError
fx,fy,tx,ty = name
flag = True
except:
print('错误的格式')
if(flag):
break
img = mosaic(img,fx,fy,tx,ty)
while(1):
cmd = input('处理完成,按1存储文件,按2显示文件,按3退出:')
if(cmd == '1'):
path = input('请输入存储路径:')
try:
img.save(path)
exit()
except:
print('无法存储文件')
elif(cmd == '2'):
img.show()
elif(cmd == '3'):
exit()
else:
print('未知的命令:%s'%cmd)
effect
Code analysis
Import library
# magic remark
# -*- encoding:utf-8 -*-
from PIL import Image
Overall code and ideas
Ideas:
The main essence is in these few lines of code:
img = img.resize((10,10))
img = img.resize(s)
When the picture is zoomed out, the pixels will be merged. When the
picture is zoomed in, the pixels
will be copied. So if the picture is zoomed out and then zoomed in, the merged pixels will be copied a few minutes, and it will have a mosaic effect.
Code:
def _mosaic(img):
s = img.size
img = img.resize((10,10))
img = img.resize(s)
return img
Partial mosaic code and ideas
Ideas:
Cut out some parts, beat the cut out picture as a whole, and glue it back
Code:
def mosaic(img,fx,fy,tx,ty):
c = img.crop((fx,fy,tx,ty))
c = _mosaic(c)
img.paste(c,(fx,fy,tx,ty))
return img
Input file name
if(__name__ == '__main__'):
while(1):
flag = False
name = input('请输入文件名:')
try:
img = Image.open(name)
flag = True
except:
print('无法读取文件')
if(flag):
break
Enter the cutting position
fx,fy,tx,ty = None,None,None,None
while(1):
flag = False
name = input('请输入需要打马赛克的区域(x1,y1,x2,y2):')
try:
name = [int(i) for i in name.split(',')]
if(len(name) != 4):
raise ValueError
fx,fy,tx,ty = name
flag = True
except:
print('错误的格式')
if(flag):
break
Local fight
img = mosaic(img,fx,fy,tx,ty)
Output section
while(1):
cmd = input('处理完成,按1存储文件,按2显示文件,按3退出:')
if(cmd == '1'):
path = input('请输入存储路径:')
try:
img.save(path)
exit()
except:
print('无法存储文件')
elif(cmd == '2'):
img.show()
elif(cmd == '3'):
exit()
else:
print('未知的命令:%s'%cmd)
Code github
Author
hit-road
Bye, get out of class is over!
Hit-road will be updated from time to time, see or leave!