Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options:
-a, --aa_factor FLOAT Antialias factor
-s, --style TEXT
-l, --lang TEXT
-c, --clipboard Copy image to clipboard
-f, --image_format [png|jpeg|bmp|gif]
Antialias factor
-o, --output FILE Output path for image
Expand Down
51 changes: 35 additions & 16 deletions src/codepic/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import io
import os
import sys
import tempfile
from subprocess import run

import click
from PIL import Image
Expand All @@ -26,6 +28,7 @@
@click.option('-a', '--aa_factor', type=float, default=1, help='Antialias factor')
@click.option('-s', '--style', type=str, default='one-dark')
@click.option('-l', '--lang', type=str)
@click.option('-c', '--clipboard', is_flag=True, help='Copy image to clipboard')
@click.option(
'-f',
'--image_format',
Expand Down Expand Up @@ -65,32 +68,37 @@ def cli(
image_format: str | None,
style: str,
lang: str | None,
clipboard: bool,
):
code = ''

if font_name is None:
font_name = ''

if not image_format and output:
ext = os.path.splitext(source_file)[1]
if ext:
ext = ext.lower()
if ext in ['png', 'jpeg', 'jpg', 'bmp', 'gif']:
image_format = ext
if image_format == 'jpg':
image_format = 'jpeg'

if not image_format:
image_format = 'png'
if output:
ext = os.path.splitext(source_file)[1]
if ext:
ext = ext.lower()
if ext in ['png', 'jpeg', 'jpg', 'bmp', 'gif']:
image_format = ext
if image_format == 'jpg':
image_format = 'jpeg'

if clipboard and image_format != 'png':
exit('Image format must be png to use clipboard')

write_to_stdout = False
if output == '-':
write_to_stdout = True

elif not output:
if source_file == '-':
write_to_stdout = True
else:
output = os.path.splitext(source_file)[0] + '.' + image_format.lower()
if not clipboard:
if source_file == '-':
write_to_stdout = True
else:
output = os.path.splitext(source_file)[0] + '.' + image_format.lower()

formatter = ImageFormatter(
font_name=font_name,
Expand Down Expand Up @@ -141,18 +149,21 @@ def cli(
if height.endswith('%'):
perc = int(height[:-1]) / 100
height = int(img.height * perc)

else:
height = int(height)

if width:
if width.endswith('%'):
perc = int(width[:-1]) / 100
width = int(img.width * perc)

else:
width = int(width)

if not width and height:
width = int(height / aspect)

if not height and width:
height = int(width * aspect)

Expand All @@ -162,9 +173,17 @@ def cli(
buff = io.BytesIO()
img.save(buff, format='PNG')

buff = buff.getbuffer()

if clipboard:
with tempfile.NamedTemporaryFile('wb', delete=True) as fp:
fp.write(buff)
run(f'xclip -selection clipboard -target image/png < {fp.name}', shell=True)
fp.flush()

if write_to_stdout:
sys.stdout.buffer.write(buff.getbuffer())
sys.stdout.buffer.write(buff)

else:
elif output and output != '-':
with open(output, 'wb') as f:
f.write(buff.getbuffer())
f.write(buff)