重装Win10系统没有激活怎么办?Win10
863 2023-04-03 04:22:39
有时候对于网络识别,将原始图片放进网络中并不能达到自己想要的效果,但是有时候如果将图片放大之后,识别率却能够达到意想不到的结果,现在提供一种将文件中的图片进行批处理放大的代码:
?123456789101112131415161718192021222324252627282930313233import
os
from
PIL
import
Image
import
sys
#获取path目录下的所有文件
def
get_imlist(path):
return
[os.path.join(path,f)
for
f
in
os.listdir(path)]
def
change_size(path):
directorys
=
get_imlist(path)
for
directory
in
directorys:
#不是图片文件就跳过
print
(directory)
if
not
(directory.endswith(
'.webp'
)
or
directory.endswith(
'.webp'
)
or
directory.endswith(
'.bmp'
)):
##b
pass
else
:
img
=
Image.
open
(directory)
s
=
"/"
#获取文件名(含后缀)
oimage_name
=
directory[directory.rfind(s)
+
1
:]
(oimage_width,oimage_height)
=
img.size
new_width
=
oimage_width
*
3
new_height
=
oimage_height
*
3
out
=
img.resize((new_width,new_height),Image.ANTIALIAS)
out.save(
"%s"
%
oimage_name)
#直接替换
if
__name__
=
=
'__main__'
:
change_size(
"F:\桌面\\test"
)
问题:实现将像素n*n的图片 等比例缩放为 30*30
0、环境要求:Python3.x 需要安装pillow模块:
?1pip
install
pillow
1、缩放一个图片:
?12345from
PIL
import
Image
img
=
Image.
open
(
"2.webp"
)
# 读取图片
out
=
img.resize((
30
,
30
))
# 等比例缩放为30*30像素
out.save(
"2.webp"
)
# 保存图片(覆盖原图)
2、批处理:
?1234567891011121314151617181920212223242526272829303132333435/
*
目录结构
E:\data_eg\lip_train:
-
-
-
-
-
-
-
-
-
-
folder1:
-
-
-
-
-
-
-
-
pic1.webp
-
-
-
-
-
-
-
-
pic2.webp
-
-
-
-
-
-
-
-
pic3.webp
-
-
-
-
-
-
-
-
-
-
folder2:
-
-
-
-
-
-
-
-
pic1.webp
-
-
-
-
-
-
-
-
pic2.webp
-
-
-
-
-
-
-
-
pic3.webp
-
-
-
-
-
-
-
-
pic4.webp
-
-
-
-
-
-
-
-
pic5.webp
-
-
-
-
-
-
-
-
-
-
folder3:
-
-
-
-
-
-
-
-
pic1.webp
-
-
-
-
-
-
-
-
pic2.webp
*
/
import
os
from
PIL
import
Image
path_target
=
"E:\data_eg\lip_train"
# liptrain路径下是文件夹,文件夹下是多张图片
path_list
=
os.walk(path_target)
# 使用os.walk()方法,遍历所有路径,文件
# filenames 保存所有图片名的 list
# root 保存二级目录
for
root, dirs, filenames
in
path_list:
for
filename
in
filenames:
# 遍历包含 所有文件名 的list
file_path
=
os.path.join(root,filename)
# 合成得到每个图片的 绝对路径
print
(file_path)
# 打印 绝对路径 至屏幕
img
=
Image.
open
(file_path)
# 打开 此绝对路径 的图片
out
=
img.resize((
30
,
30
))
# 等比例缩放至 30*30 像素
#out = img.convert('L') # 转换为灰度图像
out.save(file_path)
# 保存,覆盖原图片
到此这篇关于python批处理将图片进行放大的文章就介绍到这了,更多相关python批处理图片放大内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章: