图片分辨率检测与更改Python代码

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以点击下方链接自行获取

Python免费学习资料、代码以及交流解答点击即可加入


 今天,给大家介绍一个可以自动检测图片分辨率的简单代码,下面是用来检测的代码以及用来更改分辨率的代码,超实用

检测:

'''
如有需要Python学习资料的小伙伴可以加群领取:1136201545
'''


import cv2
path = "D:/picture/source_image.png"
img = cv2.imread(path)
sp = img.shape
height = sp[0]  # height(rows) of image
width = sp[1]  # width(colums) of image
chanael = sp[2]  # the pixels value is made up of three primary colors
print ( 'width: %d \nheight: %d \nnumber: %d' % (width, height, chanael))

更改:


from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=1280,height=720):
    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("D:/picture/source_image.png"):
    convertjpg(jpgfile,"D:/picture/")
posted @ 2021-03-16 16:02  酸菜鱼学Python  阅读(605)  评论(0编辑  收藏  举报