想要使用Python以千字节获取图像尺寸

发布于 2025-02-08 08:59:46 字数 2026 浏览 3 评论 0原文

我正在尝试从目录中获取图像的图像名称,分辨率和大小。我得到了图像名称,分辨率和尺寸,但图像大小不在千字中,它变得像像素一样。因此,请建议我如何使用Python脚本获取图像大小。

    # Required Libraries
from os import listdir
from os.path import isfile, join
from pathlib import Path
import numpy
import cv2
import argparse
import numpy
import csv
from PIL import Image
  
# Check whether the CSV 
# exists or not if not then create one.
my_file = Path("csv/details.csv")
  
if my_file.is_file():
    f = open(my_file, "w+")
    with open('csv/details.csv', 'a', newline='') as file:
        writer = csv.writer(file)
          
        writer.writerow(["S.No.", "Name", "Resolution", "Size"
                        ])
    f.close()
    pass
    
else:
    with open('csv/details.csv', 'w', newline = '') as file:
        writer = csv.writer(file)
          
        writer.writerow(["S.No.", "Name", "Resolution", "Size"
                        ])
  
# Argparse function to get
# the path of the image directory
ap = argparse.ArgumentParser()
  
ap.add_argument("-i", "--image", 
                required = True, 
                help = "Path to folder")
  
args = vars(ap.parse_args())
  
# Program to find the
# colors and embed in the CSV
mypath = args["image"]
  
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype = object)
  
for n in range(0, len(onlyfiles)):
    
    path = join(mypath,onlyfiles[n])
    images[n] = cv2.imread(join(mypath,onlyfiles[n]),
                           cv2.IMREAD_UNCHANGED)
      
    img = cv2.imread(path)
    h,w,c = img.shape
    resolution = f"{h} X {w}"
    size = img.size
    print(h, w)
      
    avg_color_per_row = numpy.average(img, axis = 0)
    avg_color = numpy.average(avg_color_per_row, axis = 0)
      
    with open('csv/details.csv', 'a', newline = '') as file:
        writer = csv.writer(file)
        writer.writerow([n+1, onlyfiles[n], resolution, size  
                        ])
        file.close() 

另外,请在下面找到您的参考。 .net/ihvkx.png“ alt =”在此处输入图像说明”

I am trying to get the image name, resolution, and size of the image from the directory. I got image name, resolution and size but image size is not in kilobytes it is getting like pixels. so please suggest me how to get the image size with the python script.

    # Required Libraries
from os import listdir
from os.path import isfile, join
from pathlib import Path
import numpy
import cv2
import argparse
import numpy
import csv
from PIL import Image
  
# Check whether the CSV 
# exists or not if not then create one.
my_file = Path("csv/details.csv")
  
if my_file.is_file():
    f = open(my_file, "w+")
    with open('csv/details.csv', 'a', newline='') as file:
        writer = csv.writer(file)
          
        writer.writerow(["S.No.", "Name", "Resolution", "Size"
                        ])
    f.close()
    pass
    
else:
    with open('csv/details.csv', 'w', newline = '') as file:
        writer = csv.writer(file)
          
        writer.writerow(["S.No.", "Name", "Resolution", "Size"
                        ])
  
# Argparse function to get
# the path of the image directory
ap = argparse.ArgumentParser()
  
ap.add_argument("-i", "--image", 
                required = True, 
                help = "Path to folder")
  
args = vars(ap.parse_args())
  
# Program to find the
# colors and embed in the CSV
mypath = args["image"]
  
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype = object)
  
for n in range(0, len(onlyfiles)):
    
    path = join(mypath,onlyfiles[n])
    images[n] = cv2.imread(join(mypath,onlyfiles[n]),
                           cv2.IMREAD_UNCHANGED)
      
    img = cv2.imread(path)
    h,w,c = img.shape
    resolution = f"{h} X {w}"
    size = img.size
    print(h, w)
      
    avg_color_per_row = numpy.average(img, axis = 0)
    avg_color = numpy.average(avg_color_per_row, axis = 0)
      
    with open('csv/details.csv', 'a', newline = '') as file:
        writer = csv.writer(file)
        writer.writerow([n+1, onlyfiles[n], resolution, size  
                        ])
        file.close() 

and also please find below is the screenshot for your reference.enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2025-02-15 08:59:46

嗨,您可以在您的内部使用此循环

import os

file_size = os.path.getsize('d:/file.jpg')
print("File Size is :", file_size, "bytes")

Hi you can use this inside your for loop

import os

file_size = os.path.getsize('d:/file.jpg')
print("File Size is :", file_size, "bytes")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文