15
\$\begingroup\$

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:

A cat

Output:

A weird cat

This is , so shortest answer wins.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ Can the program take in the PNG as a file or buffer instead of a path, and output a modified file? \$\endgroup\$ Commented 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
    Commented Feb 16, 2023 at 19:06
  • \$\begingroup\$ Is a HTTP PUT an acceptable method of "overwriting"? \$\endgroup\$
    – qarz
    Commented 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
    Commented Feb 22, 2023 at 13:53

15 Answers 15

12
\$\begingroup\$

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.

\$\endgroup\$
0
10
\$\begingroup\$

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.

Video of it Running

\$\endgroup\$
2
  • 4
    \$\begingroup\$ You can send keys instead of clicking, send !hror^s \$\endgroup\$
    – tsh
    Commented 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
    Commented Feb 21, 2023 at 0:07
7
\$\begingroup\$

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
\$\endgroup\$
7
\$\begingroup\$

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

Thanks to Jacob for the tip to reduce bytes count

\$\endgroup\$
12
  • \$\begingroup\$ Aren't you supposed to take input rather than just hard-coding it as c.png? \$\endgroup\$
    – The Thonnu
    Commented Feb 16, 2023 at 14:54
  • \$\begingroup\$ @TheThonnu Yes, you are right! \$\endgroup\$
    – EzioMercer
    Commented Feb 16, 2023 at 14:57
  • \$\begingroup\$ You can get rid of the (async()=>{...})() if you use Node16 or newer (top-level await) \$\endgroup\$ Commented Feb 16, 2023 at 16:13
  • 1
    \$\begingroup\$ When I get the chance I will, busy at the moment; \$\endgroup\$ Commented 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 .js file) \$\endgroup\$ Commented Feb 17, 2023 at 2:38
7
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Maybe you can from PIL.Image import* and then open(p:=... \$\endgroup\$
    – tsh
    Commented Feb 17, 2023 at 3:00
  • \$\begingroup\$ @tsh yes, you're right! Thanks. \$\endgroup\$
    – The Thonnu
    Commented Feb 17, 2023 at 8:12
7
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ Do you actually need the $1? If the input is given as a command-line argument, isn't the 'program' just sips -r 90 (10 bytes)? \$\endgroup\$ Commented 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, sips doesn't appear to read from STDIN \$\endgroup\$ Commented Feb 17, 2023 at 17:50
  • \$\begingroup\$ I'm not sure that I understood your explanation: typing sips -r 90 2iORu.png on my Mac seems to rotate the image file "2iORu.png" and overwrite the original (without using zsh). Isn't that the objective? \$\endgroup\$ Commented 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, which zsh by default on modern MacOS. Thus my answer is a zsh shell script (that could be saved as a script file). Am I missing something? If I create a zsh script file with the content sips -r 90, it doesn't do what is required. \$\endgroup\$ Commented 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 as r test.png then claim that the alias is only 10 bytes long... hmm, not a bad idea for this challenge actually \$\endgroup\$ Commented Feb 17, 2023 at 22:21
6
\$\begingroup\$

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

\$\endgroup\$
6
\$\begingroup\$

Factor + image.processing.rotation, 52 bytes

[ dup load-image 90 rotate swap save-graphic-image ]

Testing the function in Factor's REPL:

enter image description here

\$\endgroup\$
5
\$\begingroup\$

Python + opencv-python, 84 62 bytes

import cv2
cv2.imwrite(p:=input(),cv2.rotate(cv2.imread(p),0))

Screenshot

-22 thanks to @Stef

Unfortunately for some reason when I do from cv2 import* it just doesn't work:

Screenshot

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
\$\endgroup\$
3
  • 1
    \$\begingroup\$ It looks like cv2.ROTATE_90_CLOCKWISE is just an enum with value 0. So you could just do cv2.imwrite(p:=input(),cv2.rotate(cv2.imread(p),0)) \$\endgroup\$
    – Stef
    Commented 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
    Commented Feb 16, 2023 at 22:40
  • \$\begingroup\$ @Stef yeah I did, it still didn't work. I'll update it with the 0 later. \$\endgroup\$
    – The Thonnu
    Commented Feb 17, 2023 at 7:41
4
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ Now Pillow > OpenCV > Pygame \$\endgroup\$
    – The Thonnu
    Commented Feb 17, 2023 at 8:15
4
\$\begingroup\$

Julia, 46 bytes

using Images
~f=save(f,imrotate(load(f),π/2))
\$\endgroup\$
3
\$\begingroup\$

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:\Temp\2iORu.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:\Temp\2iORu.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.

\$\endgroup\$
3
\$\begingroup\$

MATLAB/Octave, 33 bytes

@(x)imwrite(rot90(imread(x),3),x)
\$\endgroup\$
1
\$\begingroup\$

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 90º 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 lib folder
  • Rename it to WideImage

To run it, just execute something like php -f file.php image.png.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.