我有一個包含大約 5 張影像的檔案夾,我想搜索該檔案夾并查看是否有任何影像符合我正在尋找的標準(例如 350 寬 400 高)。我不知道從哪里開始或者我需要什么模塊我沒有任何影像,我的代碼可以在下面看到,以查看存盤寬度和高度的格式。
image1 = str(input("Please enter the image filename: "))
open_img1 = Image.open(image1)
w, h = open_img1.size
任何解決方案或建議都會有所幫助。
uj5u.com熱心網友回復:
您需要匯入枕頭(pip install 枕頭安裝庫,但匯入 PIL 以在您的代碼中使用它),然后您將擁有類似的東西:
import os
import PIL.Image
for filename in os.listdir():
if filename.split(".")[-1] in ["png", "jpeg"]:
image = PIL.Image.open(filename)
if image.size == (350, 400):
print(filename)
uj5u.com熱心網友回復:
import os
from PIL import Image
img_dir = 'images/'
img_width = 350
img_height = 400
for image in os.listdir(img_dir):
img = Image.open(img_dir image)
width = img.width
height = img.height
if width == img_width and height == img_height:
print(img_dir image " height: ", height)
print(img_dir image " width: ", width)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/449016.html
標籤:Python python-3.x 目录 python成像库
上一篇:檢查df中的字典是否相等
下一篇:強制運行特定方法
