This challenge is simple: Take a path to an png image file as input and overwrite that file with a copy of itself rotated 90 degrees clockwise. You may assume that the file has dimensions of 128x128 pixels. You may not use image metadata modifications (like Exif) to rotate the image; the pixels themselves must be transformed.
Examples
Input:
Output:
This is code-golf, so shortest answer wins.
-
3\$\begingroup\$ Can the program take in the PNG as a file or buffer instead of a path, and output a modified file? \$\endgroup\$noodle person– noodle person2023年02月16日 16:08:41 +00:00Commented Feb 16, 2023 at 16:08
-
2\$\begingroup\$ @Jacob I think this suggestion ignores one of the main part of competition "overwrite the file" \$\endgroup\$EzioMercer– EzioMercer2023年02月16日 19:06:55 +00:00Commented Feb 16, 2023 at 19:06
-
\$\begingroup\$ Is a HTTP PUT an acceptable method of "overwriting"? \$\endgroup\$qarz– qarz2023年02月20日 23:59:30 +00:00Commented Feb 20, 2023 at 23:59
-
1\$\begingroup\$ @qarz That depends on the behavior of the server; if the server actually overwrites the file then yes, it does. \$\endgroup\$Ginger– Ginger2023年02月22日 13:53:44 +00:00Commented Feb 22, 2023 at 13:53
15 Answers 15
Bash/ImageMagick, 21 bytes
mogrify -rotate 90 1ドル
Conveniently this command overwrites the source file, and does not need the image to be of any specific dimensions.
AutoHotkey + MSPaint, 77 Bytes
f(s){
run mspaint %s%
sleep 999
click 189,116
sleep 200
click 26,13
send ^s
}
Autohotkey is not a language that lends itself to golfing very well, though it is uniquely very good at pretending to be a user.
This simply opens paint with the target image, clicks the Rotate, then 90 degrees buttons, then presses CTRL+S to save the image.
-
4\$\begingroup\$ You can send keys instead of clicking,
send !hror^s\$\endgroup\$tsh– tsh2023年02月17日 08:21:52 +00:00Commented Feb 17, 2023 at 8:21 -
\$\begingroup\$ I love this. This is one of those things that would probably be a new standard loophole if it were more space-efficient, but it's not, so it remains a fun novelty that occasionally shows up and is occasionally surprisingly small. \$\endgroup\$qarz– qarz2023年02月21日 00:07:20 +00:00Commented Feb 21, 2023 at 0:07
Python + Pillow, (削除) 64 (削除ここまで) 59 bytes
from PIL.Image import*
open(p:=input()).rotate(-90).save(p)
-5 thanks to @tsh
Commented
from PIL.Image import* # Import module
open( # Open an image
p:=input() # Input the filename and store in p
).rotate( # Rotate the image
-90 # -90 degrees anti-clockwise
# (= 90 degrees clockwise)
).save(p) # And save it back in the original file
-
\$\begingroup\$ Maybe you can
from PIL.Image import*and thenopen(p:=...\$\endgroup\$tsh– tsh2023年02月17日 03:00:46 +00:00Commented Feb 17, 2023 at 3:00 -
\$\begingroup\$ @tsh yes, you're right! Thanks. \$\endgroup\$The Thonnu– The Thonnu2023年02月17日 08:12:58 +00:00Commented Feb 17, 2023 at 8:12
MATL, 8 bytes
YiIX!GYG
Unfortunately the online compilers cannot load/save images so here is a demo that uses an image defined within the interpreter itself.
Explanation
% Implicitly grab the first input (the file path) as a string
Yi % Read in the image
IX! % Rotate 270 degrees counter-clockwise
G % Grab the file path again
YG % Write the rotated image to this file
NodeJS + JIMP, 74 bytes
require('jimp').read(n=process.argv[2]).then(x=>x.rotate(270,!1).write(n))
The path to image must be passed as third arg:
node filename.js imageSrc.png
UPD 87 -> 84
Thanks to Arnauld for the tip to reduce bytes count
UPD 84 -> 74
-
\$\begingroup\$ Aren't you supposed to take input rather than just hard-coding it as
c.png? \$\endgroup\$The Thonnu– The Thonnu2023年02月16日 14:54:33 +00:00Commented Feb 16, 2023 at 14:54 -
\$\begingroup\$ @TheThonnu Yes, you are right! \$\endgroup\$EzioMercer– EzioMercer2023年02月16日 14:57:08 +00:00Commented Feb 16, 2023 at 14:57
-
\$\begingroup\$ You can get rid of the
(async()=>{...})()if you use Node16 or newer (top-level await) \$\endgroup\$noodle person– noodle person2023年02月16日 16:13:55 +00:00Commented Feb 16, 2023 at 16:13 -
1\$\begingroup\$ When I get the chance I will, busy at the moment; \$\endgroup\$noodle person– noodle person2023年02月16日 17:08:04 +00:00Commented Feb 16, 2023 at 17:08
-
1\$\begingroup\$
require("jimp").read(n=process.argv[2]).then(x=>x.rotate(270,!1).write(n))is 74 bytes (regular.jsfile) \$\endgroup\$noodle person– noodle person2023年02月17日 02:38:52 +00:00Commented Feb 17, 2023 at 2:38
MacOS zsh + sips, 13
sips -r 90 1ドル
sips (Scriptable Image Processing System) is a lesser-known built-in command-line utility for image manipulation. Confusingly, nothing to do with SIP (System Integrity Protection).
MacOS zsh alias + sips, 10
sips -r 90
Create the alias with alias r=sips -r 90, then run with r test.png.
-
\$\begingroup\$ Do you actually need the
1ドル? If the input is given as a command-line argument, isn't the 'program' justsips -r 90(10 bytes)? \$\endgroup\$Dominic van Essen– Dominic van Essen2023年02月17日 09:58:25 +00:00Commented Feb 17, 2023 at 9:58 -
1\$\begingroup\$ @DominicvanEssen To my knowledge, there is no way to implicitly do this without providing a positional parameter. In some CG asnwers, a script can read from STDIN - the stream gets piped to the first command in the script that reads it. But in this case the input is the filename, as we need to both read from and write to the input filename. Also,
sipsdoesn't appear to read from STDIN \$\endgroup\$Digital Trauma– Digital Trauma2023年02月17日 17:50:19 +00:00Commented Feb 17, 2023 at 17:50 -
\$\begingroup\$ I'm not sure that I understood your explanation: typing
sips -r 90 2iORu.pngon my Mac seems to rotate the image file "2iORu.png" and overwrite the original (without using zsh). Isn't that the objective? \$\endgroup\$Dominic van Essen– Dominic van Essen2023年02月17日 21:13:15 +00:00Commented Feb 17, 2023 at 21:13 -
1\$\begingroup\$ If you're typing
sips -r 90 2iORu.png(presumably into the terminal), then you are using the shell, whichzshby default on modern MacOS. Thus my answer is azshshell script (that could be saved as a script file). Am I missing something? If I create a zsh script file with the contentsips -r 90, it doesn't do what is required. \$\endgroup\$Digital Trauma– Digital Trauma2023年02月17日 22:08:50 +00:00Commented Feb 17, 2023 at 22:08 -
1\$\begingroup\$ @DominicvanEssen I suppose one could submit an answer in the form of a shell alias
alias r='sips -r 90', which may be run asr test.pngthen claim that the alias is only 10 bytes long... hmm, not a bad idea for this challenge actually \$\endgroup\$Digital Trauma– Digital Trauma2023年02月17日 22:21:27 +00:00Commented Feb 17, 2023 at 22:21
APL (Dyalog Unicode), 64 bytes
Anonymous prefix lambda.
{b.MakePNG⎕NREPLACE,⍨⍵⎕NTIE-≡b.CBits←⌽⍉'CBits'⍎⍨'b'⎕WC'Bitmap'⍵}
{...} "dfn"; argument is denoted ⍵:
'b'⎕WC'Bitmap'⍵ create a Bitmap b from the file
'CBits'⍎⍨ extract the Colour Bits from that
⍉ transpose
⌽ mirror
b.CBits← update b's Colour Bits to that value
≡ get the array nesting depth of that (returns 1)
- negate (returns −1)
⍵⎕NTIE use that as file handle to tie the file (returns −1)
,⍨ self-concatenate (we need a list of file handle and start position, where −1 means current position, i.e. at the start of the file)
b.MakePNG⎕NREPLACE replace the file contents with a render of b to PNG file contents
Python + opencv-python, (削除) 84 (削除ここまで) 62 bytes
import cv2
cv2.imwrite(p:=input(),cv2.rotate(cv2.imread(p),0))
-22 thanks to @Stef
Unfortunately for some reason when I do from cv2 import* it just doesn't work:
Commented
import cv2 # Import module
cv2.imwrite( # Write to a file:
p:=input() # Get the filename as input and store in p
cv2.rotate( # A rotated version of:
cv2.imread(p), # The contents of p
0 # Rotated 90 degrees clockwise
)) # Close the parentheses
-
1\$\begingroup\$ It looks like
cv2.ROTATE_90_CLOCKWISEis just an enum with value 0. So you could just docv2.imwrite(p:=input(),cv2.rotate(cv2.imread(p),0))\$\endgroup\$Stef– Stef2023年02月16日 22:34:58 +00:00Commented Feb 16, 2023 at 22:34 -
\$\begingroup\$ BTW the
from cv2 import*version works for me. Have you tried with a linebreak instead of a;? \$\endgroup\$Stef– Stef2023年02月16日 22:40:07 +00:00Commented Feb 16, 2023 at 22:40 -
\$\begingroup\$ @Stef yeah I did, it still didn't work. I'll update it with the
0later. \$\endgroup\$The Thonnu– The Thonnu2023年02月17日 07:41:05 +00:00Commented Feb 17, 2023 at 7:41
Python + Pygame, 77 bytes
from pygame import*
image.save(transform.rotate(image.load(a:=input()),90),a)
Guess in terms of golfiness PIL/Pillow > Pygame > OpenCV
-
\$\begingroup\$ Now Pillow > OpenCV > Pygame \$\endgroup\$The Thonnu– The Thonnu2023年02月17日 08:15:42 +00:00Commented Feb 17, 2023 at 8:15
Julia, 46 bytes
using Images
~f=save(f,imrotate(load(f),π/2))
PowerShell Core, 58 bytes
%{$p=[Drawing.Image]::FromFile($_);$p|% Ro* 1;$p.Save($_)}
Input comes from the pipeline.
TIO won't work, what with the file, so the following assumes the cat above was downloaded to C:\Temp.
Try it in a PS 7 Console:
$path = 'C:\Temp2円iORu.png'
$path |
%{$p=[Drawing.Image]::FromFile($_);$p|% Ro* 1;$p.Save($_)}
Invoke-Item $path
Windows PowerShell, 78 bytes
The System.Drawing assembly must be loaded, which costs 20 bytes as compared to Core.
%{Add-Type -A *.*wing;$p=[Drawing.Image]::FromFile($_);$p|% Ro* 1;$p.Save($_)}
Try it in a Windows PowerShell console:
$path = 'C:\Temp2円iORu.png'
$path |
%{Add-Type -A *.*wing;$p=[Drawing.Image]::FromFile($_);$p|% Ro* 1;$p.Save($_)}
Invoke-Item $path
Pretty straightforward:
Starts the % cmdlet (alias for ForEach-Object)
Adds the assembly System.Drawing (not required in PS Core).
Loads the image file from the pipeline (in $_) into $p.
Then a bit of golfing can take place: $p|% Ro* 1 pipes the image to % (ForEach-Object) and calls the image's method RotateFlip(RotateFlipType), with 1 being Rotate90FlipNone.
Then the image gets saved to the original location.
Invoke-Item shows the image in the default app for .png. You can of course leave out that line and open it with your favorite viewer.
MATLAB/Octave, 33 bytes
@(x)imwrite(rot90(imread(x),3),x)
PHP 7.4.33 + WideImage 11.02.09 - 88 bytes
This relies on the WideImage library, which is an old library to manipulate images.
<?php include($x=WideImage)."/$x.php";$x::load($a=$argv[1])->rotate(90)->saveToFile($a);
You can use from PHP 5.2.0 to 7.4.33, but I've confirmed it works in 7.4.33 and 5.6.44.
This is meant to be executed from a command line/shell.
It does exactly what the code says: loads an image, rotates by 90o and overwrites the file.
How to test this
Just follow these simple steps:
- Install PHP 7.4.33
- Create a PHP file somewhere convenient
- Put the code in that file
- Download WideImage - DO NOT DOWNLOAD THE GITHUB VERSION
- Extract the
libfolder - Rename it to
WideImage
To run it, just execute something like php -f file.php image.png.
feh image viewer, 1
feh is an image viewer for the Unix X window system, first developed in 1999.
>
From linux.com:
Type > to rotate an image 90 degrees clockwise, and < to rotate it 90 degrees counterclockwise. Your changes are immediately saved to the file.