我有一个poster_folder
包含 jpg 文件的文件夹,例如 1.jpg,2.jpg, 3.jpg
此文件夹的路径是:
from pathlib import Path
from PIL import Image
images_dir = Path('C:\\Users\\HP\\Desktop\\PGDinML_AI_IIITB\\MS_LJMU\\Dissertation topics\\Project_2_Classification of Genre for Movies using Machine Leaning and Deep Learning\\Final_movieScraping_data_textclasification\\posters_final').expanduser()
我有一个带有 jpg 图像信息的数据框:
df_subset_cleaned_poster.head(3)
movie_name movie_image
Lion_king 1.jpg
avengers 2.jpg
iron_man 3.jpg
我试图在文件夹中绘制所有 jpg 文件(因为它们具有不同的分辨率)的宽度和高度的散点图,如下所示:
height, width = np.empty(len(df_subset_cleaned_poster)), np.empty(len(df_subset_cleaned_poster))
for i in range(len(df_subset_cleaned_poster.movie_image)):
w, h = Image.open(images_dir.joinpath(df_subset_cleaned_poster['movie_image'][i])).size
width[i], height[i] = w, h
plt.scatter(width, height, alpha=0.5)
plt.xlabel('Width'); plt.ylabel('Height'); plt.show()
这是抛出错误: KeyError: 208
df_subset_cleaned_poster.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10225 entries, 0 to 10986
Data columns (total 2 columns):
movie_name 10225 non-null object
movie_image 10225 non-null object
dtypes: object(2)
正如评论中所讨论的:问题似乎出在数据框的创建或 csv 文件本身中。
我能够使用以下代码创建适当的散点图:
from pathlib import Path
import numpy as np
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
from io import StringIO
if __name__ == '__main__':
images_dir = Path("../data/images")
infile = StringIO("""movie_name,movie_image
Lion_king,1.jpg
avengers,2.jpg
iron_man,3.jpg
""")
df_subset_cleaned_poster = pd.read_csv(infile)
n = len(df_subset_cleaned_poster)
height, width = np.empty(n), np.empty(n)
for i, filename in enumerate(df_subset_cleaned_poster.movie_image):
w, h = Image.open(images_dir / filename).size
width[i], height[i] = w, h
plt.scatter(width, height, alpha=0.5)
plt.xlabel('Width')
plt.ylabel('Height')
plt.show()
我建议您使用此代码作为进一步实验的起点。我正在使用enumerate
迭代中的所有行df_subset_cleaned_poster.movie_image
。这本身应该对 IndexErrors 更健壮。
如您所见,我用infile
模拟字符串替换了StringIO
. 只需将其替换infile = open("your_file.txt")
为再次使用真实数据即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句