在python中调整图像代码的大小并读取脚本文件
我已经编写了一个使用Python图像库调整图像大小的脚本。代码运行正常,但脚本也正在读取.py脚本文件。我怎样才能避免呢在python中调整图像代码的大小并读取脚本文件,python,image,python-imaging-library,Python,Image,Python Imaging Library,我已经编写了一个使用Python图像库调整图像大小的脚本。代码运行正常,但脚本也正在读取.py脚本文件。我怎样才能避免呢 import os import sys import PIL import PIL as pillow from PIL import Image from resizeimage import resizeimage opts, args = getopt.getopt(sys.argv[1:], 'd:w:h:') filepath = r'C://python27
import os
import sys
import PIL
import PIL as pillow
from PIL import Image
from resizeimage import resizeimage
opts, args = getopt.getopt(sys.argv[1:], 'd:w:h:')
filepath = r'C://python27//test'
saved = 'C://python27//test2'
basewidth = 4193
for opt, arg in opts:
if opt == '-d':
filepath = arg
if opt == '-w':
basewidth = int(arg)
if basewidth == -1 or filepath == '':
print('Invalid command line arguments. -d [directory] ' \
'-w [width] are required')
exit()
for image in os.listdir(filepath):
print('Resizing image ' + image)
name,ext = os.path.splitext(image)
if ext.lower() in [".png", ".jpg", ".bmp"]:
img = Image.open(image)
width = (basewidth / float(img.size[0]))
height = int((float(img.size[1]) * float(width)))
img = img.resize((basewidth, height), PIL.Image.ANTIALIAS)
img = resizeimage.resize('crop', img, [3000, 2800])
img.save(os.path.join(saved, 'resized-' + image))
输出:
调整图像大小852A9961.JPG
调整图像大小pc1.PNG
调整图像try13.py的大小
基本上,如果代码块,您必须在内部移动行打印('Resizing image'+image)
。这样,它将仅在处理文件时打印消息,而不是针对目录中的每个文件
出于风格上的原因,在os.listdir(filepath)中图像的行中:
您应该更改文件的image
,因为从概念上讲,在该列表中有通用文件。只有在if
语句之后,您才有点确定您有一个可以处理的图像。基本上,您必须在if
代码块内移动行print('Resizing image'+image)
。这样,它将仅在处理文件时打印消息,而不是针对目录中的每个文件
出于风格上的原因,在os.listdir(filepath)中图像的行中:
您应该更改文件的image
,因为从概念上讲,在该列表中有通用文件。只有在if
声明之后,您才有点确定您有一个可以处理的图像。非常感谢您,它成功了。我可以再问一个与这篇文章相关的问题吗?或者我必须提出一个新问题?也许最好是一个新问题。非常感谢你,它奏效了。我可以再问一个与这篇文章相关的问题吗?或者我必须提出一个新问题?最好是一个新问题。