Page 1 of 1

flipping every other line of pixel in an image

Posted: 2007-06-07T07:18:03-07:00
by Hang
Hi,

I'm new to ImageMagick, I was wondering is it possible to use IM to flip a line of pixel horizontal at every other line in an image.

I have an image that has a gradient from black to white. The first line of pixel is black to white gradient, from left to right. The second line of pixel is a gradient of white to black, from left to right again. The third is a repeat of first line, the fourth line is a repeat of the second line and so on so forth. I want to flip every other(even) line so the gradient goes from black to white gradient, from left to right. Any help will be greatly appreciated, thanks.


Hang

Re: flipping every other line of pixel in an image

Posted: 2007-06-07T17:19:22-07:00
by anthony
I gather that your image description is just an example of what you want done, and that you don't actually want to just directly generate a gradient image from white to black, left to right.

First lets create your described example, sized to the builtin rose: image, 70x46 pixels so everyone has a working image for later.

Code: Select all

   convert -size 1x70 gradient: -rotate 90 \( +clone -flop \) -append \
         -write mpr:tile +delete \
         -size 70x46 tile:mpr:tile   line_flipped.png
If you have such an image, you could use it as a distortion map to remap other images! See IM examples, displacement maps
http://www.imagemagick.org/Usage/distor ... distortion

For exampe lets create a rose image with every line flipped...

Code: Select all

  convert rose: line_flipped.png -fx 'p{v*(w-1),j}' rose_lflip.png
and unflip the the same lines again.

Code: Select all

  convert rose_lflip.png line_flipped.png -fx 'p{v*(w-1),j}' rose_restored.png
Note that the restore operation is also its inverse. That is flip every second line operation, will do and undo itself!

Alturnativally rather than use a distortion map, you could try to do this directly!
That is flip the X location, based on if the Y location is even or odd.

Code: Select all

  convert rose: -fx 'xx=j&1?(w-1)-i:i;  p{xx,j}'  rose_lflip.png
  convert rose_lflip.png -fx 'xx=j&1?(w-1)-i:i;  p{xx,j}' rose_restored.png

Re: flipping every other line of pixel in an image

Posted: 2007-06-07T17:20:10-07:00
by anthony
Hmmm as a matter of interest.. WHY

What produced such a horrible image that has every second line flipped?

Re: flipping every other line of pixel in an image

Posted: 2007-06-08T05:55:44-07:00
by Hang
Thanks Anthony, I will try that out now and let you know how it works out.

Hang

Re: flipping every other line of pixel in an image

Posted: 2007-06-08T07:39:03-07:00
by Hang
Hi Anthony,

Every time I try using the code you provide

Code: Select all

convert rose: line_flipped.png -fx 'p{v*(w-1),j}' rose_lflip.png
the following error message pop up,
convert: unable to parse expression `p{v*(w-1),j}'.
, and it create 2 images called rose_lflip-0.png,the original rose image, and rose_lflip-1.png, the original gradient line_flipped.png.

Also when I create the line_flipped.png from the code you give, the following messages is display
convert: unable to open image `\(': No such file or directory.
convert: unable to open image `\)': No such file or directory.
convert: unable to open image `\': No such file or directory.
however, the line_flipped.png image is still created.

Hang

Re: flipping every other line of pixel in an image

Posted: 2007-06-11T06:25:10-07:00
by Hang
Hmmm as a matter of interest.. WHY

What produced such a horrible image that has every second line flipped?
This is an assignment that is giving at school.

The real image is not a gradient of black to white, from left to right. But a normal picture that has every line of pixel reverse.
Alturnativally rather than use a distortion map, you could try to do this directly!
That is flip the X location, based on if the Y location is even or odd.
Code:
convert rose: -fx 'xx=j&1?(w-1)-i:i; p{xx,j}' rose_lflip.png
convert rose_lflip.png -fx 'xx=j&1?(w-1)-i:i; p{xx,j}' rose_restored.png
Just wondering if anyone try the above code on the rose image to see if it works. I try this and it keeps giving me a message saying
C:\Program Files\ImageMagick-6.3.4-Q16>convert rose: -fx 'xx=j&1?(w-1)-i:i; p{xx,j}' rose_lflip.png
convert: option requires an argument `-fx'.
'1?' is not recognized as an internal or external command,
operable program or batch file.

Re: flipping every other line of pixel in an image

Posted: 2007-06-11T08:39:56-07:00
by el_supremo
In windows you need to use the double quote instead of the single quote:

Code: Select all

convert rose: -fx "xx=j&1?(w-1)-i:i; p{xx,j}" rose_lflip.png
Pete

Re: flipping every other line of pixel in an image

Posted: 2007-06-11T11:39:09-07:00
by Hang
Thanks el_supremo I got it working now.

Re: flipping every other line of pixel in an image

Posted: 2007-06-11T18:36:23-07:00
by anthony
Thanks el_supremo, I would not have guessed that was the problem from that error.
I tend to use single quotes as I use a UNIX machine (linux actually) and don't want too much of shell interaction.

For More specific 'DOS-isms' see...
http://www.imagemagick.org/Usage/feedback.html#windows