python中图片文件路径格式如何使用
作者:凯二七
这篇文章主要介绍了python中图片文件路径格式如何使用问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
python中图片文件路径格式使用
在Python中,使用文件路径的格式可以用来访问图片文件。
常见的文件路径格式有两种:
- 相对路径
- 绝对路径
相对路径是相对于当前工作目录的路径,例如:
'./images/picture.jpg'
绝对路径是从根目录开始的完整路径,例如:
'/User/<username>/images/picture.jpg'
注意:
如果文件路径中包含空格,请确保使用引号(单引号或双引号)将路径括起来。
通过使用正确的文件路径格式,可以使用 Python 的图像处理库(如 PIL,OpenCV 等)读取和处理图片文件。
利用python转换图片格式
常见的图像任务通常需要把照片统一成相同的格式,所以此文章正是为了统一格式而生,常见的主要有cv2和PIL.Image的相关操作,照片格式是一串数字加上后缀名
工具一:cv2
pip install opencv-python
之后就可以
import cv2
1.1 导包
import os import cv2 import sys import numpy as np
1.2 路径设置
path = r"C:\\Users\\86775\\test_change\\" path2 = r"C:\\Users\\86775\\test_change2\\"
1.3 改格式重新传
1.3.1 用os.path.splitext()来判断
images = os.listdir(path) for i in os.listdir(path): print(os.path.splitext(i)) # ('34474006827920603', '.png') if os.path.splitext(i)[1] == ".jpeg": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".jpeg", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif os.path.splitext(i)[1] == ".png": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".png", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif os.path.splitext(i)[1] == ".JPG": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".JPG", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif os.path.splitext(i)[1] == ".PNG": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".PNG", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif os.path.splitext(i)[1] == ".jpg": img = cv2.imread(path + i) # print(img) cv2.imwrite(path2 + i, img)
1.3.2 用split()来判断
images = os.listdir(path) for i in os.listdir(path): print(i.split(".")) if i.split(".")[1] == "jpeg": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".jpeg", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif i.split(".")[1] == "png": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".png", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif i.split(".")[1] == "JPG": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".JPG", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif i.split(".")[1] == "PNG": img = cv2.imread(path + i) # print(img) new_imagename = i.replace(".PNG", ".jpg") cv2.imwrite(path2 + new_imagename, img) elif i.split(".")[1] == "jpg": img = cv2.imread(path + i) # print(img) cv2.imwrite(path2 + i, img)
1.4 查看结果
工具二:pillow
pip install pillow
之后就可以
from PIL import Image
2.1 导包
import os from PIL import Image
2.2 路径设置
path = r"C:\\Users\\86775\\test_change\\" path2 = r"C:\\Users\\86775\\test_change2\\"
2.3 写格式修改函数
RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度,而JPG不支持透明度。
所以其他格式不转jpg可以直接保存为.png格式,转jpg就需要丢弃Alpha
img = img.convert(“RGB”),转换成RGB,保证和jpg格式的通道数相同,也算一种图片类型变换的防报错机制
def jpeg2jpg(path_in, path_out): img = Image.open(path_in) img = img.convert("RGB") img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True) def png2jpg(path_in, path_out): img = Image.open(path_in) img = img.convert("RGB") img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True) def JPG2jpg(path_in, path_out): img = Image.open(path_in) img = img.convert("RGB") img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True) def PNG2jpg(path_in, path_out): img = Image.open(path_in) img = img.convert("RGB") img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True) def jpg2jpg(path_in, path_out): img = Image.open(path_in) img = img.convert("RGB") img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)
2.4 开始转换
images = os.listdir(path1) for i in images: print(i) if os.path.splitext(i)[1] == ".jpeg": source = path1 + "\\" + str(i.split(".")[0]) + ".jpeg" target = path2 + "\\" + str(i.split(".")[0]) + ".jpg" jpeg2jpg(source, target) elif os.path.splitext(i)[1] == ".png": source = path1 + "\\" + str(i.split(".")[0]) + ".png" target = path2 + "\\" + str(i.split(".")[0]) + ".jpg" png2jpg(source, target) elif os.path.splitext(i)[1] == ".JPG": source = path1 + "\\" + str(i.split(".")[0]) + ".JPG" target = path2 + "\\" + str(i.split(".")[0]) + ".jpg" JPG2jpg(source, target) elif os.path.splitext(i)[1] == ".PNG": source = path1 + "\\" + str(i.split(".")[0]) + ".PNG" target = path2 + "\\" + str(i.split(".")[0]) + ".jpg" PNG2jpg(source, target) elif os.path.splitext(i)[1] == ".jpg": source = path1 + "\\" + str(i.split(".")[0]) + ".jpg" target = path2 + "\\" + str(i.split(".")[0]) + ".jpg" jpg2jpg(source, target)
2.5 查看结果
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。