4

I'm attempting to crop an image using ImageMagick and via PowerShell. I can crop the image fine with the following command, and it creates the 2000+ images:

convert -crop 16x16 .\original.png tileOut%d.png

However, I would like to take advantage of ImageMagick's ability to dynamically set the file name.

According to a post on their forums I should be able to run something like the following via a batch file:

convert ^
   bigimage.jpg ^
  -crop 256x256 ^
  -set filename:tile "%%[fx:page.x/256+1]_%%[fx:page.y/256+1]" ^
  +repage +adjoin ^
  tiled_%%[filename:tile].gif

I shouldn't need to escape the % since I'm running this in PowerShell directly, so I used the following:

convert -crop 16x16 .\original.png -set filename:tile "%[fx:page.x/16+1]_%[fx:page.y/16+1]" +repage +adjoin directory\tiled_%[filename:tile].png

However, when I run this command I end up with one file called tiled_%[filename and another called tiled_45_47.png.

So while it does seem to create the last file, it only creates the one. The first file is 0 bytes in size, but takes up over 8 MB of space on disc, according to properties on the file.

Trying to run the command in a batch file results in the same behavior, which makes me think PowerShell itself isn't the issue, but rather the command is.

According to the documentation +adjoin is required since I want different images. +repage doesn't make much sense to me, but I've kept it in the command since the original had it, and excluding it doesn't seem to change the output. -set filename seems pretty straightforward.

Large size of the first leads me to believe that all the previous images might be getting added to it. However, the file name also suggests it's getting hung up on the :, but it doesn't appear to be a special character in PowerShell. It's also creating an image for the very last crop. Baffling.

So what am I doing wrong?

Thanks in advance!

EDIT:

  • PowerShell 5.0.10586.0, on Windows 10.
  • ImageMagick 6.9.2 Q16 (64-bit)

From the comments, I'm thinking the issue might be with the ImageMagick command.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
  • Shot in the dark but have you tried putting those string in single quotes so that PowerShell wont attempt to interpet them. (From the looks of it there is nothing there that is bad.). FWIW the colon has meaning in context. It can be a scope specifier on variables among other things. `convert -crop '16x16' '.\original.png' -set 'filename:tile' '%[fx:page.x/16+1]_%[fx:page.y/16+1]' +repage +adjoin 'directory\tiled_%[filename:tile].png'` Everything does not have to be quoted. I just added some extras for testing. – Matt Dec 26 '15 at 16:45
  • Thanks for the suggestion! I did try replacing double quotes with single per Jacqueline's answer below, but I've tried your more vigorous suggestion and end up with the same results as before, which is the 2 files mentioned in the question. :( However, this makes me believe more and more that the syntax for ImageMagick might be the issue, despite the forum post being less than a year old. – James Skemp Dec 26 '15 at 16:54
  • 1
    I agree that PowerShell should not care about the characters used in those particular strings. The forum post might still be valid for the _version in use at that time_. – Matt Dec 26 '15 at 16:55
  • Good point. It looks to have been version 6.9.0, while I'm running 6.9.2 (question now includes version information), so if they follow correct versioning, suggests I should be okay. Guess I'll dig into the documentation further and hope an ImageMagick guru finds this. Thanks again! – James Skemp Dec 26 '15 at 17:00

2 Answers2

3

I'm not using Powershell, but I think you will have more success by specifying your image first, then the crop, then setting the filename:

convert original.png -crop 16x16 -set filename:tile "%[fx:page.x/16+1]_%[fx:page.y/16+1]" +repage "tiled_%[filename:tile].png"
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • +1, but yup, figured that out ~12 minutes ago. :) As I note in my answer, what's strange is that the normal `-crop` doesn't care if the image isn't the first parameter. Since I tweaked what was working with the new parameters, instead of starting fresh, I ran into that issue. I notice you only include `+repage`; can I safely exclude `+adjoin` (seems that flag is set by default when using `%d` in the file name, which I'm not), and what does `+repage` do, in simple terms (docs were a little confusing)? – James Skemp Dec 26 '15 at 17:28
  • `+repage` tells the image to forget where (i.e. the offset) in the original image it came from and make its top-left corner `0,0`. Run your command without it and then run `identify tiled*` and you will see the geometry is like `16x16+48+32` rather than `16x16+0+0` which is what it would be with `+repage`. – Mark Setchell Dec 26 '15 at 17:32
  • Your `+adjoin` is fine, it just stops IM writing multi-page files like you get in TIFF files. – Mark Setchell Dec 26 '15 at 17:36
  • 1
    Thanks for the additional information! The accepted answer is yours. – James Skemp Dec 26 '15 at 17:40
1

So in the past I was using the following command to crop images, with the %d being automatically converted to a number based upon the sequence.

convert -crop 16x16 .\original.png directory\tileOut%d.png

That works perfectly fine. However, the example provided on that forum had the original file name listed as the first argument to the convert command. Changing my command so that it was listed first results in the expected behavior.

convert .\original.png -crop '16x16' -set 'filename:tile' '%[fx:page.x/16+1]_%[fx:page.y/16+1]' +repage +adjoin 'directory\tiled_%[filename:tile].png'

The use of single quotes in so many locations may not be required, but it works.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
  • this is what i meant seems to work: start-process convert -ArgumentList "image.jpg -crop 256x256 $((Get-Date).Day).png" -NoNewWindow – Jaqueline Vanek Dec 26 '15 at 17:31