Last active
December 30, 2022 23:11
-
-
Save kvnkho/a61284de0ece5697f196f3857450a460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from typing import Any, Dict, Iterable | |
from PIL import Image | |
from io import BytesIO | |
def transform_img(df: List[Dict[str, Any]]) -> Iterable[str, Any]: | |
for row in df: | |
try: | |
response = requests.get(row["ImgUrl"], timeout=5) | |
img = Image.open(BytesIO(response.content)) | |
xmin = float(row["xmin"]) * img.width | |
xmax = float(row["xmax"]) * img.width | |
ymin = float(row["ymin"]) * img.height | |
ymax = float(row["ymax"]) * img.height | |
img = img.crop((xmin, ymin, xmax, ymax)) | |
# logic to save image to cloud storage | |
yield row | |
except Exception as e: | |
logger.error(e) | |
yield row | |
results = transform_img(df.to_dict("records")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment