Image resolution detection and change Python code

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


Detection:

'''
如有需要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))

change:


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/")

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114882856