Input sources¶
Pass Machyon whatever holds your URLs. Everything is read row by row, so a file with a hundred million lines never has to fit in memory.
| Source | Pass | Notes |
|---|---|---|
| CSV | "urls.csv" |
Header row expected; a file whose first line is already a URL is read as headerless. Comma or tab, detected |
| TSV | "urls.tsv" |
Tab-separated, otherwise as CSV |
| Text | "urls.txt" (or any other extension) |
One URL per line. Blank lines and lines starting with # are ignored |
| JSON Lines | "urls.jsonl", "urls.ndjson" |
One object (or URL string) per line |
| JSON | "urls.json" |
A list of URLs or objects, or an object holding exactly one such list ({"images": [...]}) |
| Parquet | "urls.parquet" |
Read in batches of 65,536 rows. Needs pip install "machyon[parquet]" |
| DataFrame | a pandas or polars DataFrame |
Every column is available to name templates |
| Iterable | a list, tuple or generator | Of URL strings, (url, filename) pairs, or dicts |
| One URL | "https://…/a.jpg" |
Downloads that one image |
Finding the URL column¶
For tables (CSV, TSV, JSON objects, Parquet, DataFrames, dicts), Machyon looks for a column named, in order of
preference: url, image_url, image_link, img_url, link, src, image, href, uri (in any case). If
none of those exists, it picks the first column whose value in the first row starts with http:// or
https://. If that fails too, it stops with an error listing the columns.
Name it explicitly whenever you know it:
Values are stripped of surrounding whitespace. A row whose URL is empty or isn't http(s):// with a host is
recorded as invalid_url, so it's never silently skipped.
CSV and TSV¶
- Header: the first line names the columns, and every column can be used in a
name template. If the first line is already a URL, the file is read as having no header, and each
line's first cell that starts with
httpis its URL. - Separator: a
.tsvfile is always tab-separated. A.csvfile is read as comma-separated unless its first 4 KB holds more tabs than commas. - Encoding: UTF-8, with or without the byte-order mark that Excel adds.
- Blank lines are skipped, and a row with fewer cells than the header gets empty values for the rest.
The file is read row by row, so its size doesn't matter.
JSON and JSON Lines¶
{"url": "https://example.com/photos/cat.jpg", "label": "cat"}
{"url": "https://example.com/photos/dog.jpg", "label": "dog"}
A .jsonl or .ndjson file holds one value per line: an object (a row with columns, like a CSV row), a URL
string, or a [url, filename] pair. Blank lines are skipped, and the file is streamed, so it suits very long lists.
A .json file is parsed whole. It can be a list of the same values, or an object holding exactly one such list,
such as {"images": [...]}. For millions of rows, prefer JSON Lines.
In both, the first value decides how the rest are read, so keep them all the same kind.
Python iterables¶
import machyon
with open("urls.txt", encoding="utf-8") as f:
urls = [line.strip() for line in f if line.strip() and not line.startswith("#")]
# 1. A plain list (or any iterable / generator) of URLs
machyon.download(urls, "out/plain")
# 2. (url, filename) pairs: you choose every name
machyon.download([(u, f"{i:05d}.jpg") for i, u in enumerate(urls)], "out/pairs")
# 3. URLs plus a list of names, matched one to one
machyon.download(urls, "out/named", [f"photo_{i}.jpg" for i in range(len(urls))])
# 4. Dicts: every key is a column you can use in a template
rows = [{"image_url": u, "split": "train" if i % 5 else "val"} for i, u in enumerate(urls)]
machyon.download(rows, "out/dicts", names="{split}/{index}{ext}")
Machyon decides how to read an iterable from its first item: a string means a list of URLs, a tuple or list
means (url, filename) pairs, a dict means rows with columns. Keep all items the same kind.
A generator is consumed lazily, in shards, so you can stream URLs from a database cursor or an API without building a list. (The progress bar can't show a total for a generator.)
DataFrames¶
# needs: pip install pandas (polars works the same way)
import pandas as pd
import machyon
df = pd.read_csv("products.csv")
df = df[df["label"] != "birds"] # filter with pandas first
report = machyon.download(df, "images/", url_col="image_link", names="{label}/{index}{ext}")
print(report)
Filter, sample or join with pandas or polars first, then hand the result to Machyon. Neither library is a dependency of Machyon; whichever one you pass is used.
Parquet¶
# needs: pip install "machyon[parquet]"
import machyon
# Parquet is read in batches of 65,536 rows, so the file never has to fit in memory.
report = machyon.download("products.parquet", "images/", url_col="image_link", names="{label}/{hash}{ext}")
print(report)
Ready-made filenames¶
If your table already has a filename column, use it instead of a template:
A row whose filename cell is blank is recorded as
bad_name.
Part of a source¶
start and end select rows [start, end) of any source, counted from 0 in input order:
{index} in a name template keeps the row's original position, so slices produce the same names as a full run.
Large runs