首页 > 解决方案 > 如何根据 CSV 文件中的数据检索和裁剪图像?

问题描述

我有一个 CSV 文件,其中包含 url 和框坐标(左上角的 x 坐标、左上角的 y 坐标、右下角的 x 坐标和右下角的 y 坐标),我想获取图像,根据坐标裁剪(到 256x256),然后保存图像。不幸的是,由于数据库的大小,下载整个数据库然后创建一个单独的裁剪图像的解决方案很困难。为此,有必要从一开始就创建带有裁剪图像的图像数据库。另一种方法是保存图像,然后对其进行裁剪并重写初始图像(然后 i += 1 迭代到下一个图像)。

当前的方法会起作用还是我应该使用不同的方法?另外,我如何将获取的图像保存到指定的文件夹,因为目前它下载到与脚本相同的文件夹。

import urllib.request
import csv
import numpy as np
import pandas as pd
from io import BytesIO
import requests
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

filename = "images"



# open file to read
with open("data_test.csv".format(filename), 'r') as csvfile:
    reader = csv.reader(csvfile)
    # pop header row (1st row in csv)
    header = next(reader)

    # iterate on all lines
    i = 0
    for line in csvfile:
        splitted_line = line.split(',')
        # check if we have an image URL
        if splitted_line[1] != '' and splitted_line[1] != "\n":
            response = requests.get(splitted_line[1])
            img = Image.open(BytesIO(response.content))
            #crop_img = img[splitted_line[2]:splitted_line[3], splitted_line[4]:splitted_line[5]]
            #crop_img = img[315:105, 370:173]
            img.save(str(i) + ".png")
            #crop_img = img[105:105+173,315:315+370]
            #[y: y + h, x: x + w]
            new_img = img.resize((256, 256))
            new_img.save(str(i) + ".png")
            imgplot = plt.imshow(img)
            plt.show()
            # urllib.request.urlopen(splitted_line[1])
            print("Image saved for {0}".format(splitted_line[0]))
            # img = cv2.imread(img_path, 0)

            i += 1
        else:
            print("No result for {0}".format(splitted_line[0]))

欢迎任何进一步的建议。

编辑:最新版本给了我错误:crop_img = img[105:105+173,315:315+370] TypeError: 'JpegImageFile' object is not subscriptable

标签: pandascsvurllib

解决方案


我使用 Bytes.IO 和一些裁剪/调整大小技术解决了这个问题。

import csv
from io import BytesIO
import requests
from PIL import Image
import matplotlib.pyplot as plt

filename = "images"

# open file to read
with open("data_test.csv".format(filename), 'r') as csvfile:
    reader = csv.reader(csvfile)
    # pop header row (1st row in csv)
    header = next(reader)

    # iterate on all lines
    i = 0
    for line in csvfile:
        splitted_line = line.split(',')
        # check if we have an image URL
        if splitted_line[1] != '' and splitted_line[1] != "\n":
            response = requests.get(splitted_line[1])
            img = Image.open(BytesIO(response.content))

            #im.crop(box) ⇒ 4-tuple defining the left, upper, right, and lower pixel coordinate
            left_x = int(splitted_line[2])
            top_y = int(splitted_line[3])
            right_x = int(splitted_line[4])
            bottom_y = int(splitted_line[5])
            crop = img.crop((left_x, top_y, right_x, bottom_y))
            new_img = crop.resize((256, 256))
            """ 
            # preview new images
            imgplot = plt.imshow(new_img)
            plt.show()
            """
            new_img.save(str(i) + ".png")
            print("Image saved for {0}".format(splitted_line[0]))
            i += 1
        else:
            print("No result for {0}".format(splitted_line[0]))

希望它会帮助某人。仍然欢迎任何优化建议。


推荐阅读