Create PNG/JPG file with commandline on Mac
Asked Answered
S

4

15

is there a command to create a simple PNG/JPG picture file, can be run in terminal/script command on Mac?

For now, I'm going to create a simple one color PNG picture file, with parameter to specify wide and length. if it can put text on it, is much better.

Subjective answered 19/9, 2012 at 17:18 Comment(1)
See also: superuser.com/questions/571003/…Claireclairobscure
F
31

The command-line convert utility in the ImageMagick suite can easily create an image of specified dimensions:

convert -size 200x200 xc:white canvas.png

To create an image with text, use additional options:

convert -size 200x200 -gravity center -background white -fill black \
        label:"text goes here" canvas.png

The ImageMagick site has many more examples of usage.

To get ImageMagick on OSX, you can either use an automated build system such as MacPorts or run a pre-made installer package.

Folks answered 19/9, 2012 at 18:38 Comment(10)
It's really what I want and more, Fantastic!Subjective
Installed ImageMagick a minute ago. First command produces error convert: no encode delegate for this image format 'canvas.png' @ error/constitute.c/WriteImage/1220. The second produces thousand similar errors. OSX 10.8.1Valerievalerio
ImageMagick-6.7.9-6. Validation passed all checks. No one command from examples imagemagick.org/Usage/text/#text_operators work. But errors different.Valerievalerio
@ValeriyVan: If the first command failed, your install of IM went wrong somehow. How did you install it? I just tested with Homebrew (brew install imagemagick) on a clean 10.8.1 machine, and the "experimental binary installer for Lion" on a 10.7.4 machine, and both work fine. (The second one is more likely to fail, because it might rely on ghostscript-fonts, which neither Homebrew nor the IM binary installer sets up for you… but get the first working first.)Ruralize
I did steps described here imagemagick.org/script/install-source.php#unix, downloaded tar by link from this page. Everything passed smoothly, but doesn't work as supposed.Valerievalerio
@ValeriyVan Why would you want to install from source? Unless you specifically want to change something in the source, you should install the binary release for MacOS, or follow abarnert's suggestion.Folks
@ValeriyVan: The instructions specifically say "We recommend MacPorts which custom builds ImageMagick in your environment." If you look at the brew recipe (for Homebrew) or portfile (for MacPorts) they're both pretty complicated, involving all kinds of dependencies and patches; that represents someone else doing the hard work of getting ImageMagick to build properly on the Mac and automating it, so you don't have to. Yet another option is cactuslab.com/imagemagick where they have installer packages. Out of all of the options out there, you chose the most difficult.Ruralize
"sudo port install ImageMagick" worked for an hour on my MBP, installed tonnes of software. But fixed issue and ImageMagick commands work now as supposed. It's really magic. I spend a day recently writing utility for text watermarking of images. And my daughter asked me today writing stuff for doing what exactly montage command does. ImageMagick is really a find for me.Valerievalerio
I've now updated the answer to explicitly recommend MacPorts or a pre-made installer.Folks
the cactus pre-made installer says ; Not compatible with Mac OS 10.11 (El Capitan) or macOS 10.12 (Sierra). cactuslab.com/imagemagickOilcloth
R
5

OS X doesn't come with a shell command for this. But you have three options.

OS X does come with a way to do this in Applescript: Image Events. You could write a three-liner in Applescript—or Python or Ruby with appscript or ScriptingBridge—that does what you want, and then run that from the shell.

OS X also has some pretty high-level image support built into Cocoa, which you can access from Python, Ruby, ObjC, Applescript, etc., so you could write a five-liner that way that does what you want.

Or you could use a third-party tool that gives you what you want. ImageMagick, as suggested by user4815162342, is probably the best one.

(I wouldn't even mention the first two possibilities if you posted on SU rather than SO.)

Ruralize answered 19/9, 2012 at 20:12 Comment(4)
Glad to help. But if you're going with ImageMagick (which I think is probably the best answer for you), you should click accept on the other user's answer.Ruralize
This is new to me. Accept both of you. Both are useful. Actually, I prefer with no extra software installation, so you second "Cocoa" is more attract me, and more practice needed. But ImageMagick provide so much brilliant functionality brings huge capability to command line too. Lucky to ask questions here.Subjective
While you can upvote both answers, you can only accept one. Anyway, you're going to run into more questions once you start using Cocoa image support, but if you search around this site and CocoaDev you'll find answers to many of them. The main thing you'll notice is that NSImage is very high-level; it models the general concept of an image, possibly with multiple representations, etc., so the first thing everyone asks is "where is [NSImage writeToFile:]?, and the answer is… well, already on SO.Ruralize
@Ruralize - I'm not seeing anything in the man pages for sips (Image Events) that would let me create an image.Philippine
S
2

If you don't want to install anything at all, you can use macOS's built-in PHP interpreter which automatically includes the gd library. So, that would give you a script like this:

#!/usr/bin/php -f

<?php
// Create a 200*100 image
$im = imagecreate(200, 100);

// Blue background and red text
$bg = imagecolorallocate($im, 0, 0, 255);
$fg = imagecolorallocate($im, 255, 0, 0);

// Write the string and save to disk
imagestring($im, 5, 10, 30, 'Nothing to install', $fg);
imagepng($im, "result.png");
?>

enter image description here

Struble answered 14/12, 2020 at 9:2 Comment(0)
E
2

An adaptation of @mark-setchell's answer:

php -r '$i = imagecreate(500, 500); imagecolorallocate($i, 0, 0, 255); imagepng($i, "sample.png");'

This works so as long as Mac's default PHP version is installed.

This will create a blue png called sample.php in the same directory you execute the command from.

Earlap answered 26/8, 2021 at 14:22 Comment(3)
Do you know if you can create a file that will be a certain size in KB? I wish to create a PNG that is bigger than 10MB and I don't seem to be able to do it that way :/Dr
Great question. Since some image formats are compressed, they can be small files if there's not a lot of content. For this reason an uncompressed format such as .bmp is probably a better format for manipulating file size... You can create a BMP by replacing the command imagepng with imagebmpEarlap
Thank you!! Doing this, I was able to generate a 25MB file :) php -r '$i = imagecreate(40000, 40000); imagecolorallocate($i, 0, 0, 255); imagebmp($i, "sample.png");'Dr

© 2022 - 2024 — McMap. All rights reserved.