macosをcatalinaに上げたらperlで書いていた画像をリサイズしてサーバーにアップロードするプログラムが動かなくなって困ったのでpythonで書き直した。
それにしてもこれいつ書いたんだろう?
#!/usr/bin/env perl use strict; use warnings; use DateTime::Format::Epoch::Unix; use DateTime; use Image::Imlib2; use Net::OpenSSH; my $tp = $ARGV[0] || ""; my @files = do{ opendir my $dh, '.' or return; my @f = grep /\.(JPG|jpg|jpeg|PNG|png)$/, grep !/^\./, readdir($dh); closedir $dh; @f; }; my $ssh = Net::OpenSSH->new('XXXXXXX'); $ssh->error and die "SSH connection failed: " . $ssh->error; for my $infile (@files) { my $now = DateTime::Format::Epoch::Unix->format_datetime(DateTime->now); my $outfile = "/Users/kzfm/images/blog/" . $now . ".jpg"; my $image = Image::Imlib2->load($infile); unless ($infile =~ /^DSCN/ ) { unless ($tp) { if ($image->width > $image->height) { $image->image_orientate(1); }}} my $image2 = $image->create_scaled_image(320,0); $image2->set_quality(95); $image2->save($outfile); $ssh->scp_put($outfile, "/usr/local/html/images/blog/" . $now . ".jpg") or die "scp failed: " . $ssh->error; printf "![%s](http://www.kzfmix.com/images/blog/%s.jpg)\n",$now,$now; sleep(1); }
これをpythonで書き直した。
#!/usr/bin/env python from PIL import Image import time from datetime import datetime from glob import glob import paramiko import scp import os width = 400 img_dir = "/Users/kzfm/images/blog" remote_dir = "/usr/local/html/images/blog/" def get_epoc_name(): return str(int(time.mktime(datetime.now().timetuple()))) def save_resize_image(filename): en = get_epoc_name() resize_file_name = "{}.jpg".format(en) f = os.path.join(img_dir, resize_file_name) img = Image.open(filename) height = round(img.height * width / img.width) img = img.resize((width, height), Image.LANCZOS) img = img.rotate(-90, expand=True) img.save(f) return (f, en) if __name__ == "__main__": with paramiko.SSHClient() as sshc: sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) sshc.connect(hostname='0.0.0.0', port=0000, username='XXX') with scp.SCPClient(sshc.get_transport()) as scpc: for f in glob("*.jpeg"): fname, en = save_resize_image(f) scpc.put(fname, remote_dir) print("![{0}](http://www.kzfmix.com/images/blog/{0}.jpg)".format(en)) time.sleep(1)