Skip to content

Instantly share code, notes, and snippets.

@kvnkho
Last active December 30, 2022 23:11
Show Gist options
  • Save kvnkho/a61284de0ece5697f196f3857450a460 to your computer and use it in GitHub Desktop.
Save kvnkho/a61284de0ece5697f196f3857450a460 to your computer and use it in GitHub Desktop.
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