Skip to content

Add option to store IPTC metadata #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/long-coats-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tidaltheory/lens': minor
---

Add option to store IPTC metadata

Only stores `object_name` (as `title`) and `caption` currently.
72 changes: 47 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@
"setup": "zazen configure"
},
"lint-staged": {
"*.{js,ts}": "eslint --fix",
"*.{js,ts}": [
"eslint --fix",
"prettier --write"
],
"package.json": "prettier --write"
},
"dependencies": {
"exif-reader": "1.0.3",
"globby": "13.1.2",
"is-plain-object": "5.0.0",
"lowdb": "3.0.0",
"lowdb": "5.0.3",
"multimatch": "6.0.0",
"node-vibrant": "3.2.1-alpha.1",
"ora": "6.1.2",
"pkg-conf": "4.0.0",
"sade": "1.8.1",
"sharp": "0.30.7"
"sharp": "0.31.1",
"ts-node-iptc": "1.0.11"
},
"devDependencies": {
"@changesets/changelog-github": "0.4.7",
Expand Down
16 changes: 13 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import path, { parse } from 'node:path'
import process from 'node:process'

import { globby } from 'globby'
import { JSONFile, Low } from 'lowdb'
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'
import ora, { oraPromise } from 'ora'
import sade from 'sade'
import sharp from 'sharp'
import { IptcParser } from 'ts-node-iptc'
import { PackageJson } from 'type-fest'

import { ImageRecord, ImageThumbnails } from '../types/types.js'
import { ImageMeta, ImageRecord, ImageThumbnails } from '../types/types.js'

import { loadConfig } from './lib/context.js'
import { getDominantPalette } from './lib/dominant.js'
Expand Down Expand Up @@ -74,7 +76,8 @@ prog.command('add <src>')
processed++

let sharpImage = sharp(source)
let { width, height } = await sharpImage.metadata()
let { width, height, iptc } = await sharpImage.metadata()
let iptcData = IptcParser.readIPTCData(iptc)
let fingerprint = await generateFingerprint(sharpImage)
let dominantPalette = await oraPromise(
getDominantPalette(sharpImage),
Expand Down Expand Up @@ -156,6 +159,12 @@ prog.command('add <src>')
}
}

let entryMeta: ImageMeta
if (config.includeMetadata) {
entryMeta.title = iptcData.object_name
entryMeta.caption = iptcData.caption
}

spinner.start('Adding entry to library...')

/**
Expand All @@ -167,6 +176,7 @@ prog.command('add <src>')
formats,
colors: dominantPalette,
thumbnails: entryThumbnails,
meta: entryMeta,
}

let store = options.store || config.store || 'imagemeta.json'
Expand Down
9 changes: 9 additions & 0 deletions types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@ interface ImageColors {
dominant: string
}

export interface ImageMeta {
/** Title field from the image’s EXIF data. */
title?: string
/** Description field from the image’s EXIF data. */
caption?: string
}

/** Data for each image stored in the library. */
export interface ImageRecord extends ImageFile {
colors: ImageColors
/** Encoded blurha.sh placeholder. */
blurhash?: string
/** Resized versions, keyed to thumbnail size label. */
thumbnails?: ImageThumbnails
meta?: ImageMeta
}

type ThumbResizeOptions = RequireAtLeastOne<ResizeOptions, 'width' | 'height'>
Expand All @@ -59,6 +67,7 @@ export interface LensConfig {
/** Use the filename as a subdirectory for generated files. */
useFilenameDirectory?: boolean
thumbnails?: ThumbnailOption[]
includeMetadata?: boolean
}

export interface PathParts {
Expand Down
1 change: 1 addition & 0 deletions zazen.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
'error',
{ allowObject: true },
],
'n/file-extension-in-import': 'off',
},
}),
}