如何使用Python实现图像尺寸调整?

2026-06-09 13:232阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计204个文字,预计阅读时间需要1分钟。

如何使用Python实现图像尺寸调整?

pythonimport sysimport ossys.path.append('/usr/local/lib/python2.7/site-packages')sys.path.append('/usr/lib/python2.7/dist-packages')import matplotlib.pyplot as pltimport cv2

def resize_img(filename): img=cv2.imread(filename) shape=img.shape

如何使用Python实现图像尺寸调整?

import sys
import os
sys.path.append('/usr/local/lib/python2.7/site-packages')
sys.path.append('/usr/lib/python2.7/dist-packages')
import matplotlib.pyplot as plt
import cv2

def resizeimg(filename):
img=cv2.imread(filename)
shape=img.shape

w=shape[1]
h=shape[0]
ww=w
hh=h
bresize=False
if w > h and w > 800:
ww=800
hh=h/(w/800)
bresize=True
if h > w and h > 800:
hh=800
ww=w/(h/800)
bresize=True
if bresize:
print filename
img=cv2.resize(img,(ww,hh))
cv2.imwrite(filename,img)

for r, d, files in os.walk( '/var/Public/img/' ):
for ffile in files:
if ffile.endswith( 'jpg' ):
resizeimg(r+'/'+ffile)





本文共计204个文字,预计阅读时间需要1分钟。

如何使用Python实现图像尺寸调整?

pythonimport sysimport ossys.path.append('/usr/local/lib/python2.7/site-packages')sys.path.append('/usr/lib/python2.7/dist-packages')import matplotlib.pyplot as pltimport cv2

def resize_img(filename): img=cv2.imread(filename) shape=img.shape

如何使用Python实现图像尺寸调整?

import sys
import os
sys.path.append('/usr/local/lib/python2.7/site-packages')
sys.path.append('/usr/lib/python2.7/dist-packages')
import matplotlib.pyplot as plt
import cv2

def resizeimg(filename):
img=cv2.imread(filename)
shape=img.shape

w=shape[1]
h=shape[0]
ww=w
hh=h
bresize=False
if w > h and w > 800:
ww=800
hh=h/(w/800)
bresize=True
if h > w and h > 800:
hh=800
ww=w/(h/800)
bresize=True
if bresize:
print filename
img=cv2.resize(img,(ww,hh))
cv2.imwrite(filename,img)

for r, d, files in os.walk( '/var/Public/img/' ):
for ffile in files:
if ffile.endswith( 'jpg' ):
resizeimg(r+'/'+ffile)