如何根据 CSV 文件中的数据 Urlretrieve 和裁剪图像?
How to Urlretrieve and crop image based on data from CSV file?
我有一个 CSV 文件,其中包含 url 和方框坐标(左上角的 x 坐标、左上角的 y 坐标、右下角的 x 坐标和右下角),我想获取图像,根据坐标裁剪(到 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]
类型错误:'JpegImageFile' 对象不可订阅
我使用 Bytes.IO 和一些 cropping/resizing 技巧解决了这个问题。
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]))
希望对大家有所帮助。仍然欢迎任何优化建议。
我有一个 CSV 文件,其中包含 url 和方框坐标(左上角的 x 坐标、左上角的 y 坐标、右下角的 x 坐标和右下角),我想获取图像,根据坐标裁剪(到 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] 类型错误:'JpegImageFile' 对象不可订阅
我使用 Bytes.IO 和一些 cropping/resizing 技巧解决了这个问题。
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]))
希望对大家有所帮助。仍然欢迎任何优化建议。