add the initial implementation of kn_cover_image
- ID
53fd213- date
2023-05-22 03:02:29+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
b58203a- message
add the initial implementation of kn_cover_image- changed files
5 files, 69 additions, 31 deletions
Changed files
images/README.md (3364) → images/README.md (4243)
diff --git a/images/README.md b/images/README.md
index 54cfa05..29e273f 100644
--- a/images/README.md
+++ b/images/README.md
@@ -36,36 +36,39 @@ These scripts are for working with images and other visual material.
</dd>
<dt>
- <a href="https://github.com/alexwlchan/scripts/blob/main/images/create_tinted_image">
- <code>create_tinted_image [PATH] [HEX_COLOUR]</code>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/images/images_only_pdf">
+ <code>images_only_pdf [PATH]</code>
</a>
</dt>
<dd>
- take a greyscale image, and create a version which is tinted with the specified colour.
- This works by creating an RGBA image which has the specified colour on every pixel, but controlling the intensity with the alpha value.
+ take a PDF, and create a new PDF which just has the images filling the page.
+ I use this to work around an odd behaviour of the “Scan Document” feature in Notes.app, where it adds a large white border around scanned images that I don’t want.
+ <p><strong>Note:</strong> this script overwrites the original file.</p>
+ </dd>
+
+ <dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/images/kn_cover_image">
+ <code>kn_cover_image [PATH]</code>
+ </a>
+ </dt>
+ <dd>
+ prepare a cover image for an article on my website.
+ <p>
+ I use Keynote to compose a lot of my promo images, then I export the slide to an image.
+ The slide includes a white rectangle that marks the rough boundary of the image; this script extracts the selected region, adjusts the crop so it's an exact 2:1 ratio, and converts the colour profile to sRGB.
+ </p>
<p>
<table>
<tr>
- <td><img src="examples/grayscale_circle.png"></td>
+ <td><img src="examples/kn_example.jpeg"></td>
<td>→</td>
- <td><img src="examples/grayscale_circle.ff0000.png"></td>
+ <td><img src="examples/kn_example.cropped.jpg"></td>
</tr>
</table>
</p>
</dd>
<dt>
- <a href="https://github.com/alexwlchan/scripts/blob/main/images/images_only_pdf">
- <code>images_only_pdf [PATH]</code>
- </a>
- </dt>
- <dd>
- take a PDF, and create a new PDF which just has the images filling the page.
- I use this to work around an odd behaviour of the “Scan Document” feature in Notes.app, where it adds a large white border around scanned images that I don’t want.
- <p><strong>Note:</strong> this script overwrites the original file.</p>
- </dd>
-
- <dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/images/pdfthumb">
<code>pdfthumb</code>
</a>
@@ -101,4 +104,24 @@ These scripts are for working with images and other visual material.
<dd>
an alias for running the Retrobatch image processor <a href="https://flyingmeat.com/retrobatch/docs-1.0/commandline/">from the command-line</a>
</dd>
+
+ <dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/images/tint_image">
+ <code>tint_image [PATH] [HEX_COLOUR]</code>
+ </a>
+ </dt>
+ <dd>
+ take a greyscale image, and create a version which is tinted with the specified colour.
+ This works by creating an RGBA image which has the specified colour on every pixel, but controlling the intensity with the alpha value.
+ <p>
+ <table>
+ <tr>
+ <td><img src="examples/grayscale_circle.png"></td>
+ <td>→</td>
+ <td><img src="examples/grayscale_circle.ff0000.png"></td>
+ </tr>
+ </table>
+ </p>
+ I don’t use this script very often, but I checked it in because I thought it was a neat trick I didn’t want to forget.
+ </dd>
</dl>
images/examples/kn_example.cropped.jpg (0) → images/examples/kn_example.cropped.jpg (49580)
diff --git a/images/examples/kn_example.cropped.jpg b/images/examples/kn_example.cropped.jpg
new file mode 100644
index 0000000..5dccdff
Binary files /dev/null and b/images/examples/kn_example.cropped.jpg differ
images/examples/kn_example.jpeg (0) → images/examples/kn_example.jpeg (515368)
diff --git a/images/examples/kn_example.jpeg b/images/examples/kn_example.jpeg
new file mode 100644
index 0000000..04a325c
Binary files /dev/null and b/images/examples/kn_example.jpeg differ
images/extract_white (4497) → images/kn_cover_image (4716)
diff --git a/images/extract_white b/images/kn_cover_image
similarity index 80%
rename from images/extract_white
rename to images/kn_cover_image
index 61f3c88..bad85cf 100755
--- a/images/extract_white
+++ b/images/kn_cover_image
@@ -1,4 +1,7 @@
#!/usr/bin/env python3
+"""
+Prepare a cover image for my website, based on an image I've created in Keynote.
+"""
import collections
import io
@@ -13,10 +16,10 @@ import termcolor
def hilight(info):
- return termcolor.colored(info, 'blue')
+ return termcolor.colored(info, "blue")
-if __name__ == '__main__':
+if __name__ == "__main__":
try:
path = sys.argv[1]
except IndexError:
@@ -55,7 +58,10 @@ if __name__ == '__main__':
diagonal = {(x - 1, y - 1), (x - 1, y + 1), (x + 1, y - 1), (x + 1, y + 1)}
orthogonal = {(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)}
- if len(white_pixels.intersection(diagonal)) == 3 and len(white_pixels.intersection(orthogonal)) == 4:
+ if (
+ len(white_pixels.intersection(diagonal)) == 3
+ and len(white_pixels.intersection(orthogonal)) == 4
+ ):
corner_pixels.add((x, y))
# Group the corners by row.
@@ -82,19 +88,24 @@ if __name__ == '__main__':
if len(y_coords) == 2:
rectangles[tuple(sorted(y_coords))].append(x)
- rectangles = {y_coords: sorted(x_coords) for y_coords, x_coords in rectangles.items() if len(x_coords) == 2}
+ rectangles = {
+ y_coords: sorted(x_coords)
+ for y_coords, x_coords in rectangles.items()
+ if len(x_coords) == 2
+ }
# The outline rectangle is the biggest rectangle; sort by area.
(y0, y1), (x0, x1) = max(
- rectangles.items(),
- key=lambda xy: (xy[1][0] - xy[1][1]) * (xy[0][0] - xy[0][1])
+ rectangles.items(), key=lambda xy: (xy[1][0] - xy[1][1]) * (xy[0][0] - xy[0][1])
)
# There may be a little slop here, so slice an extra two pixels off
# either side to account for light grey pixels that have slipped in.
# This cropping is only approximate, so this is fine.
- x0 += 2; y0 += 2
- x1 -= 2; y1 -= 2
+ x0 += 2
+ y0 += 2
+ x1 -= 2
+ y1 -= 2
# Now adjust the pixels so we get a 2:1 aspect ratio in the final image.
# Again, we don't care about exactness here, slicing a few pixels off
@@ -115,19 +126,24 @@ if __name__ == '__main__':
cropped_im = im.crop((x0, y0, x1, y1))
name, ext = os.path.splitext(path)
- ext = ext.replace('.jpeg', '.jpg')
+ ext = ext.replace(".jpeg", ".jpg")
out_path = f"{name}.cropped{ext}"
cropped_im.save(out_path, icc_profile=im.info.get("icc_profile"))
- subprocess.check_call([
- 'retrobatch', '--workflow', '~/repos/scripts/images/overwrite_with_srgb.retrobatch', out_path
- ], stdout=subprocess.DEVNULL)
+ subprocess.check_call(
+ [
+ "retrobatch",
+ "--workflow",
+ "~/repos/scripts/images/overwrite_with_srgb.retrobatch",
+ out_path,
+ ],
+ stdout=subprocess.DEVNULL,
+ )
print(out_path)
assert cropped_im.width == cropped_im.height * 2
-
outline_im = Image.new("RGBA", im.size)
for (x, y) in white_pixels:
@@ -146,5 +162,4 @@ if __name__ == '__main__':
# print(x, y)
outline_im.putpixel((x, y), (255, 0, 255, 255))
-
outline_im.save("outline5.png")
images/create_tinted_image (1096) → images/tint_image (1096)
diff --git a/images/create_tinted_image b/images/tint_image
similarity index 100%
rename from images/create_tinted_image
rename to images/tint_image