Skip to content

Commit 02977ac

Browse files
authored
Merge pull request #2 from githubocto/image-processing
Image processing library functions
2 parents 50e9853 + 0b12891 commit 02977ac

18 files changed

+84
-16
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ console.log(newfile)
2424

2525
Can be found in the examples folder:
2626

27-
* `deno run --allow-read --allow-write examples/csv-example.ts`
28-
* `deno run --allow-read --allow-write examples/arquero-example.ts`
27+
* `deno run --allow-read --allow-write examples/csv/csv-example.ts`
28+
* `deno run --allow-read --allow-write examples/csv/arquero-example.ts`
29+
* `deno run -A examples/image/image-example.ts`
2930

3031
## Testing
3132

3233
Run all the tests:
3334

34-
`deno test --allow-read --allow-write tests/*`
35+
`deno test -A tests/*`
3536

3637
Run separate tests
3738

38-
`deno test --allow-read --allow-write tests/csv-test.ts`
39+
`deno test -A tests/csv-test.ts`
3940

4041

4142
## License

examples/arquero-example.ts renamed to examples/csv/arquero-example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as aq from "https://cdn.skypack.dev/arquero";
22

3-
let t = await aq.fromCSV(await Deno.readTextFile("./examples/flights.csv"));
3+
let t = await aq.fromCSV(await Deno.readTextFile("./examples/csv/flights.csv"));
44

55
t = t
66
.derive({
@@ -22,4 +22,4 @@ t = t
2222
// Sort rows by duration, descreasing
2323
.orderby(aq.desc("duration"));
2424

25-
await Deno.writeTextFile("./examples/flights2.csv", t.toCSV());
25+
await Deno.writeTextFile("./examples/csv/flights2.csv", t.toCSV());

examples/csv-example.ts renamed to examples/csv/csv-example.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Column, DataItem, stringify } from "https://deno.land/[email protected]/encoding/csv.ts";
2-
import { readCSV, writeCSV } from '../csv.ts'
1+
import { Column, DataItem, stringify } from 'https://deno.land/[email protected]/encoding/csv.ts';
2+
import { readCSV, writeCSV } from 'https://deno.land/x/[email protected]/mod.ts'
33

44
// Path to a csv file
5-
const csvPath = './examples/read-example.csv';
5+
const csvPath = './examples/csv/prices.csv';
66

77
/*
88
Parse a csv file and return a string[][]
@@ -98,7 +98,7 @@ const columns: Column[] = ["name", "age"];
9898

9999
// we have to stringify the data with a row header
100100
const dataString = await stringify(data, columns)
101-
writeCSV('./examples/write-example.csv', dataString)
101+
writeCSV('./examples/csv/names.csv', dataString)
102102

103103
/*
104104
Write one of the previously parsed csv examples
@@ -111,4 +111,4 @@ Three,-0.3,$3000
111111
const data2 = parseColumnCSV as DataItem[]; // have to recast the output
112112
const columns2: Column[] = ["id", "quantity", "cost"];
113113
const dataString2 = await stringify(data2, columns2)
114-
writeCSV('./examples/write-example2.csv', dataString2)
114+
writeCSV('./examples/csv/prices-write.csv', dataString2)
File renamed without changes.
File renamed without changes.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
name,age
2-
Rick,70
3-
Smith,14
1+
name,age
2+
Rick,70
3+
Smith,14
File renamed without changes.
File renamed without changes.
147 KB
Loading

examples/image/cat-cropped.jpeg

176 KB
Loading

examples/image/cat-gif.gif

167 KB
Loading

examples/image/cat-resized.jpeg

162 KB
Loading

examples/image/cat.jpeg

74.7 KB
Loading

examples/image/image-example.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { loadImage, loadImageBytes } from '../../image.ts'
2+
import { Image } from 'https://cdn.deno.land/imagescript/versions/1.2.0/raw/mod.ts'; // library for image manipulations
3+
4+
const url = 'https://api.creativecommons.engineering/v1/thumbs/c8fe5f5b-cc1a-4794-91c5-7488c60f4914'
5+
const url2 = 'https://live.staticflickr.com/962/41906373431_72c25d0dfd_b.jpg'
6+
const url3 = 'https://i.giphy.com/media/5wWf7HapUvpOumiXZRK/giphy.gif'
7+
8+
// Can specify a filename to rename the image
9+
// (image url, path to save image, image to save name)
10+
loadImage(url, './examples/image/', 'cat')
11+
12+
// Image will be saved with the default name if not included
13+
loadImage(url2, './examples/image/')
14+
15+
// Can load gifs, pngs, jpgs
16+
loadImage(url3, './examples/image/', 'cat-gif')
17+
18+
// Image manipulation example - reading from a file
19+
const bytes = await Deno.readFile('./examples/image/cat.jpeg') // local file
20+
const image = await Image.decode(bytes);
21+
image.crop(image.width/4, image.height/4, image.width/2, image.height/2); // x, y, width, height
22+
await Deno.writeFile('./examples/image/cat-cropped.jpeg', await image.encode());
23+
24+
// Image manipulation example - fetching from a url
25+
const { imageBytes, mimeType } = await loadImageBytes(url) // url
26+
const image2 = await Image.decode(imageBytes);
27+
image2.resize(image2.width/2, image2.height/2)
28+
image2.opacity(0.5).invert()
29+
await Deno.writeFile('./examples/image/cat-resized.jpeg', await image2.encode());

image.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { mime } from "https://cdn.deno.land/mimetypes/versions/v1.0.0/raw/mod.ts"
2+
import { urlParse } from 'https://cdn.deno.land/url_parse/versions/1.0.0/raw/mod.ts';
3+
import { basename } from "https://deno.land/[email protected]/path/mod.ts";
4+
5+
export async function loadImageBytes(url: string) {
6+
const response = await fetch(url); // fetch an image
7+
const mimeType = response.headers.get('content-type');
8+
const imageBytes = new Uint8Array(await response.arrayBuffer());
9+
return { imageBytes, mimeType }
10+
}
11+
12+
export async function loadImage(url: string, path: string, name?: string) {
13+
const { imageBytes, mimeType } = await loadImageBytes(url)
14+
const extension = mime.getExtension(mimeType as string);
15+
let imagePath
16+
17+
if (name) {
18+
imagePath = `${path}${name}.${extension}`
19+
20+
} else {
21+
let defaultName = basename(urlParse(url).pathname)
22+
defaultName = defaultName.split('.')[0]
23+
imagePath = `${path}${defaultName}.${extension}`
24+
}
25+
26+
await Deno.writeFile(imagePath, imageBytes); // create a jpg file with Deno
27+
}

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './json.ts'
22
export * from './csv.ts'
3+
export * from './image.ts'

tests/csv-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { assertArrayIncludes } from "https://deno.land/[email protected]/testing/asserts.ts"
22
import { readCSV, writeCSV } from '../csv.ts'
33

4-
const csvReadPath = './examples/read-example.csv'
5-
const csvWritePath = './examples/write-example.csv'
4+
const csvReadPath = './examples/csv/prices.csv'
5+
const csvWritePath = './examples/csv/names.csv'
66

77
Deno.test("reads a csv file", async () => {
88
const csv = await readCSV(csvReadPath) as ArrayLike<unknown>

tests/image-test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"
2+
import { loadImage } from '../image.ts'
3+
4+
const url = 'https://api.creativecommons.engineering/v1/thumbs/c8fe5f5b-cc1a-4794-91c5-7488c60f4914'
5+
6+
Deno.test("loads an image", async () => {
7+
await loadImage(url, './examples/image/', 'cat')
8+
const bytes = await Deno.readFile('./examples/image/cat.jpeg') // local file
9+
assertEquals(bytes.length, 76507)
10+
});

0 commit comments

Comments
 (0)