Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking for a oneliner command that would slice an image into given proportions.

Say input would be filename and slice dimensions. Are there any standard command-line tools for this? I tried ImageMagick to no avail since it garbled my image immensely (it's big, mostly transparent, png).

share|improve this question
    
By "slice" you mean crop? I would've said ImageMagick; are you sure you used it right? –  Michael Mrozek Apr 5 '11 at 12:59
    
Yes, ImageMagick considers it as cropping. Here's what I've used: $ convert source.png -crop 1024x1024 destination.png –  Eimantas Apr 5 '11 at 13:17
    
@Eimantas.. I'm a bit unsure of what you actually want...but based on what you wrote in a comment to JRW's answer. It seems that you want to divide your original image into two seperate smaller images... That would mean cropping the image two times.. once for the first section, and then again for the second section.... Maybe there is something which will do it in a single pass... (I don't know of anything but someone else may)... Is what I just described actually what you want? ie.. a program which will cut up a picture into two (or more) sections in a single command? –  Peter.O Apr 5 '11 at 16:45
    
@fred.bear - yes, that's exactly what i'm looking for. –  Eimantas Apr 5 '11 at 17:07

2 Answers 2

netpbm tools can do this. If you are talking about cropping it:

$ pngtopnm image.png | pnmcut -width 500 -height 500 | pnmtopng > image_cropped.png

If you want to scale it use pnmscale.

share|improve this answer
    
Thanks for your answer! Will this output subsequence cropped pieces of an image? –  Eimantas Apr 5 '11 at 15:55
    
I'm not sure what you mean by subsequence cropped pieces. Can you explain? –  JRW Apr 5 '11 at 15:58
    
If i have image of 2048x1024 - will it output two images of 1024x1024? –  Eimantas Apr 5 '11 at 16:28

I know that question is little old, but I wrote script that is using JRW solution. Script is splitting image file into series of images of given size:

#!/bin/bash
FILE=$1
FILENOEXT=${FILE%.*}
SLICEWIDTH=$2
WIDTH=`file $FILE | cut -f5 -d" "`
NUMOFSLICES=`echo "scale=2; $WIDTH/$SLICEWIDTH+1" | bc`
for i in `seq $NUMOFSLICES`
do
    LAST=$(($SLICEWIDTH * $i - $SLICEWIDTH))
    pngtopnm $FILE | pnmcut -left $LAST -width $SLICEWIDTH | pnmtopng > cropped-$FILENOEXT-$i.png
done
pngtopnm $FILE | pnmcut -left $LAST | pnmtopng > cropped-$FILENOEXT-$i.png

Argument one is filename and second width of chunk. In most cases the script return error on last chunk, but after that last chunk is cropped outside loop again and everything is ok… :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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