学无先后,达者为师

网站首页 编程语言 正文

python如何修改图像的分辨率_python

更新时间: 2022-12-26 编程语言

如何修改图像的分辨率

使用 PIL 模块修改图像分辨率

计算公式,像素用cv2.resize() 调整,分辨率用下面代码调整,得出尺寸。

import cv2
from PIL import Image as ImagePIL, ImageFont, ImageDraw
from PIL import Image
im = ImagePIL.open('qq.jpg')  #读取图片bgr 格式<class 'PIL.JpegImagePlugin.JpegImageFile'>
print(im)
print(type(im))
im = cv2.imread('qq.jpg')   #读取图片rgb 格式<class 'numpy.ndarray'>
image = Image.fromarray(cv2.cvtColor(im,cv2.COLOR_BGR2RGB))  #格式转换,bgr转rgb
image.save('qq1.jpg',quality=95,dpi=(300.0,300.0))    #调整图像的分辨率为300,dpi可以更改

python批量修改图片分辨率

from PIL import Image
import os.path
import glob
 
 
def convertjpg(jpgfile,outdir,width=640, height=640):
    img=Image.open(jpgfile)
    try:
        new_img=img.resize((width,height),Image.BILINEAR)
        new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
    except Exception as e:
        print(e)
 
for jpgfile in glob.glob("E:\\Aubo\\tomatoo\\*.jpg"):
    convertjpg(jpgfile, "E:\\Aubo\\tomatoo\\enhance")

原文链接:https://blog.csdn.net/Dawn__Z/article/details/86468173

栏目分类
最近更新