Slice image and name by row and column

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?".
Post Reply
mkuhfuss
Posts: 4
Joined: 2019-06-10T02:23:03-07:00
Authentication code: 1152

Slice image and name by row and column

Post by mkuhfuss »

Hi,

i need to slice an image. For example:
full.jpg = 7296x5472

1 slice = max. 1024x1024px

At the moment i use this command:

Code: Select all

convert -crop 1024x1024 full.jpg %d.jpg
With this i got imagename like:
1.jpg
2.jpg


But i need the name with row and column index. For example:

First row:
0_1.jpg
0_2.jpg

Second row:
1_1.jpg
1_2.jpg


My Version is: 6.9.7-4

Thanks in Advance
Markus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Slice image and name by row and column

Post by snibgo »

snibgo's IM pages: im.snibgo.com
mkuhfuss
Posts: 4
Joined: 2019-06-10T02:23:03-07:00
Authentication code: 1152

Re: Slice image and name by row and column

Post by mkuhfuss »

Thansk for your answer…
This works for me:

Code: Select all

convert 7296.jpg -crop 1024x1024 -set filename:tile "%[fx:page.y/1024]_%[fx:page.x/1024]" 0_%[filename:tile].jpg
No i need to resize all Images in a loop and than slice these Thumbnails:

Example:

Original.jpg = 7296x5472px

First i need to resize this full Image to:
5472px (75%)
3648px (50%)
1824px (25%)
912px (12,5%)

Every one of these thumbnails i need to slice in 1024X1024px.

My command for resizing so far:
I realized that the following command is very slow. Is there a faster approach?

Code: Select all

convert 7296.jpg \
 \( +clone -resize 75% -write thumb_75.jpg +delete \) \
 \( +clone -resize 50% -write thumb_50.jpg +delete \) \
  \( +clone -resize 25% -write thumb_25.jpg +delete \) \
  -resize 12.5% thumb_12_5.jpg
Next my current command doesn't realy work :-(

Code: Select all

files=(thumb_*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
	echo index $i
	convert "$f" -crop 1024x1024 -set filename:tile "%[fx:page.y/1024]_%[fx:page.x/1024]" "$i"_%[filename:tile].jpg
	i=$(( i + 1 ))
done
I want to loop the 4 files (5472px, 3648px, 1824px,912px) and i expect this result:

5472px:
0_0_1.jpg
0_0_2.jpg
...
next row:
0_1_1.jpg
0_1_2.jpg
...

3648px:
1_0_1.jpg
1_0_2.jpg
...
next row:
1_1_1.jpg
1_1_2.jpg
...


and so on...
mkuhfuss
Posts: 4
Joined: 2019-06-10T02:23:03-07:00
Authentication code: 1152

Re: Slice image and name by row and column

Post by mkuhfuss »

Ok with this it works. But resizing is still very slow.
And it would be nice if i could do this in one command/Batch script!

Code: Select all

convert 7296.jpg \
\( +clone -resize 100% -write thumb_1.jpg +delete \) \
 \( +clone -resize 75% -write thumb_2.jpg +delete \) \
 \( +clone -resize 50% -write thumb_3.jpg +delete \) \
  \( +clone -resize 25% -write thumb_4.jpg +delete \) \
  -resize 12.5% thumb_5.jpg

Code: Select all

files=(thumb_*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
	echo index $i
	echo file $f
	convert "$f" -crop 1024x1024 -set filename:tile "%[fx:page.y/1024]_%[fx:page.x/1024]" "$i"_%[filename:tile].jpg
	i=$(( i + 1 ))
done
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Slice image and name by row and column

Post by snibgo »

mkuhfuss wrote:Is there a faster approach?
Instead of "-resize", you could use "-scale" or "-sample". These are faster, and usually lower-quality for ordinary photos, but may be good enough.
mkuhfuss wrote:Next my current command doesn't realy work
You increment "i" at every input file. But your required output filenames suggest you should increment "i" less often.
snibgo's IM pages: im.snibgo.com
mkuhfuss
Posts: 4
Joined: 2019-06-10T02:23:03-07:00
Authentication code: 1152

Re: Slice image and name by row and column

Post by mkuhfuss »

Oh thank you!
-sample and -scale are faster, much more faster :-)

Quality is good enough i think too!
Post Reply