Cut image into grid with filenames corresponding to positions

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
Kavelach
Posts: 2
Joined: 2020-03-08T09:55:42-07:00
Authentication code: 1152

Cut image into grid with filenames corresponding to positions

Post by Kavelach »

I have a file

Code: Select all

magick identify photo.jpg
photo.jpg JPEG 1920x1080 1920x1080+0+0 8-bit sRGB 1.37997MiB 0.000u 0:00.000
What I want, is to cut this image into a grid of images that have a specified width and height while preserving tile order — the first tile should be named 0.0, second 1.0 and so on. I tried to do

Code: Select all

magick convert -crop 160x135 -set filename:xpos "%[fx:page.x/160]" -set filename:ypos "%[fx:page.y/135]" photo.jpg "tiles/%[filename:xpos].%[filename:ypos].jpg"
This is the result I get:

Code: Select all

ls tiles/
 11.7.jpg                                   '%[filename:xpos].%[filename:ypos]-52.jpg'
'%[filename:xpos].%[filename:ypos]-0.jpg'   '%[filename:xpos].%[filename:ypos]-53.jpg'
'%[filename:xpos].%[filename:ypos]-10.jpg'  '%[filename:xpos].%[filename:ypos]-54.jpg'
'%[filename:xpos].%[filename:ypos]-11.jpg'  '%[filename:xpos].%[filename:ypos]-55.jpg'
'%[filename:xpos].%[filename:ypos]-12.jpg'  '%[filename:xpos].%[filename:ypos]-56.jpg'
'%[filename:xpos].%[filename:ypos]-13.jpg'  '%[filename:xpos].%[filename:ypos]-57.jpg'
'%[filename:xpos].%[filename:ypos]-14.jpg'  '%[filename:xpos].%[filename:ypos]-58.jpg'
'%[filename:xpos].%[filename:ypos]-15.jpg'  '%[filename:xpos].%[filename:ypos]-59.jpg'
'%[filename:xpos].%[filename:ypos]-16.jpg'  '%[filename:xpos].%[filename:ypos]-5.jpg'
'%[filename:xpos].%[filename:ypos]-17.jpg'  '%[filename:xpos].%[filename:ypos]-60.jpg'
'%[filename:xpos].%[filename:ypos]-18.jpg'  '%[filename:xpos].%[filename:ypos]-61.jpg'
'%[filename:xpos].%[filename:ypos]-19.jpg'  '%[filename:xpos].%[filename:ypos]-62.jpg'
'%[filename:xpos].%[filename:ypos]-1.jpg'   '%[filename:xpos].%[filename:ypos]-63.jpg'
'%[filename:xpos].%[filename:ypos]-20.jpg'  '%[filename:xpos].%[filename:ypos]-64.jpg'
'%[filename:xpos].%[filename:ypos]-21.jpg'  '%[filename:xpos].%[filename:ypos]-65.jpg'
'%[filename:xpos].%[filename:ypos]-22.jpg'  '%[filename:xpos].%[filename:ypos]-66.jpg'
'%[filename:xpos].%[filename:ypos]-23.jpg'  '%[filename:xpos].%[filename:ypos]-67.jpg'
'%[filename:xpos].%[filename:ypos]-24.jpg'  '%[filename:xpos].%[filename:ypos]-68.jpg'
'%[filename:xpos].%[filename:ypos]-25.jpg'  '%[filename:xpos].%[filename:ypos]-69.jpg'
'%[filename:xpos].%[filename:ypos]-26.jpg'  '%[filename:xpos].%[filename:ypos]-6.jpg'
'%[filename:xpos].%[filename:ypos]-27.jpg'  '%[filename:xpos].%[filename:ypos]-70.jpg'
'%[filename:xpos].%[filename:ypos]-28.jpg'  '%[filename:xpos].%[filename:ypos]-71.jpg'
'%[filename:xpos].%[filename:ypos]-29.jpg'  '%[filename:xpos].%[filename:ypos]-72.jpg'
'%[filename:xpos].%[filename:ypos]-2.jpg'   '%[filename:xpos].%[filename:ypos]-73.jpg'
'%[filename:xpos].%[filename:ypos]-30.jpg'  '%[filename:xpos].%[filename:ypos]-74.jpg'
'%[filename:xpos].%[filename:ypos]-31.jpg'  '%[filename:xpos].%[filename:ypos]-75.jpg'
'%[filename:xpos].%[filename:ypos]-32.jpg'  '%[filename:xpos].%[filename:ypos]-76.jpg'
'%[filename:xpos].%[filename:ypos]-33.jpg'  '%[filename:xpos].%[filename:ypos]-77.jpg'
'%[filename:xpos].%[filename:ypos]-34.jpg'  '%[filename:xpos].%[filename:ypos]-78.jpg'
'%[filename:xpos].%[filename:ypos]-35.jpg'  '%[filename:xpos].%[filename:ypos]-79.jpg'
'%[filename:xpos].%[filename:ypos]-36.jpg'  '%[filename:xpos].%[filename:ypos]-7.jpg'
'%[filename:xpos].%[filename:ypos]-37.jpg'  '%[filename:xpos].%[filename:ypos]-80.jpg'
'%[filename:xpos].%[filename:ypos]-38.jpg'  '%[filename:xpos].%[filename:ypos]-81.jpg'
'%[filename:xpos].%[filename:ypos]-39.jpg'  '%[filename:xpos].%[filename:ypos]-82.jpg'
'%[filename:xpos].%[filename:ypos]-3.jpg'   '%[filename:xpos].%[filename:ypos]-83.jpg'
'%[filename:xpos].%[filename:ypos]-40.jpg'  '%[filename:xpos].%[filename:ypos]-84.jpg'
'%[filename:xpos].%[filename:ypos]-41.jpg'  '%[filename:xpos].%[filename:ypos]-85.jpg'
'%[filename:xpos].%[filename:ypos]-42.jpg'  '%[filename:xpos].%[filename:ypos]-86.jpg'
'%[filename:xpos].%[filename:ypos]-43.jpg'  '%[filename:xpos].%[filename:ypos]-87.jpg'
'%[filename:xpos].%[filename:ypos]-44.jpg'  '%[filename:xpos].%[filename:ypos]-88.jpg'
'%[filename:xpos].%[filename:ypos]-45.jpg'  '%[filename:xpos].%[filename:ypos]-89.jpg'
'%[filename:xpos].%[filename:ypos]-46.jpg'  '%[filename:xpos].%[filename:ypos]-8.jpg'
'%[filename:xpos].%[filename:ypos]-47.jpg'  '%[filename:xpos].%[filename:ypos]-90.jpg'
'%[filename:xpos].%[filename:ypos]-48.jpg'  '%[filename:xpos].%[filename:ypos]-91.jpg'
'%[filename:xpos].%[filename:ypos]-49.jpg'  '%[filename:xpos].%[filename:ypos]-92.jpg'
'%[filename:xpos].%[filename:ypos]-4.jpg'   '%[filename:xpos].%[filename:ypos]-93.jpg'
'%[filename:xpos].%[filename:ypos]-50.jpg'  '%[filename:xpos].%[filename:ypos]-94.jpg'
'%[filename:xpos].%[filename:ypos]-51.jpg'  '%[filename:xpos].%[filename:ypos]-9.jpg'
Why is that? While searching for it, I found this thread in which the advice is to also use +repage +adjoin parameters, but adding them doesn't change the names of the outputted files.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cut image into grid with filenames corresponding to positions

Post by snibgo »

What version of IM, on what platform?

If you use IM v7, I suggest you use "magick", not "convert" or "magick convert".

Your command is in the wrong order. It should read the input first, then crop it, then write the output.
snibgo's IM pages: im.snibgo.com

Kavelach
Posts: 2
Joined: 2020-03-08T09:55:42-07:00
Authentication code: 1152

Re: Cut image into grid with filenames corresponding to positions

Post by Kavelach »

I use Linux, and version is:

Code: Select all

magick -version
Version: ImageMagick 7.0.9-27 Q16 x86_64 2020-03-02 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5)
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc heic jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps raqm raw rsvg tiff webp wmf x xml zlib
Changing the command like so:

Code: Select all

magick photo.jpg -crop 160x135 -set filename:ypos "%[fx:page.x/160]" -set filename:xpos "%[fx:page.y/135]" 'tiles/%[filename:xpos].%[filename:ypos].jpg'
Actually worked, so yeah, thank you! I also used convert, because the docs in the same use case used it.

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Cut image into grid with filenames corresponding to positions

Post by GeeMack »

Kavelach wrote:
2020-03-08T10:31:14-07:00

Code: Select all

magick photo.jpg -crop 160x135 -set filename:ypos "%[fx:page.x/160]" -set filename:xpos "%[fx:page.y/135]" 'tiles/%[filename:xpos].%[filename:ypos].jpg'
Actually worked, so yeah, thank you! I also used convert, because the docs in the same use case used it.
You can even set the entire filename in a single setting like this...

Code: Select all

magick photo.jpg -crop 160x135 -set filename: "%[fx:page.x/160].%[fx:page.y/135]" 'tiles/%[filename:].jpg'

Locked