flipping every other line of pixel in an image

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
Hang

flipping every other line of pixel in an image

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: flipping every other line of pixel in an image

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: flipping every other line of pixel in an image

Post by anthony »

Hmmm as a matter of interest.. WHY

What produced such a horrible image that has every second line flipped?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Hang

Re: flipping every other line of pixel in an image

Post by Hang »

Thanks Anthony, I will try that out now and let you know how it works out.

Hang
Hang

Re: flipping every other line of pixel in an image

Post 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
Hang

Re: flipping every other line of pixel in an image

Post 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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: flipping every other line of pixel in an image

Post 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
Hang

Re: flipping every other line of pixel in an image

Post by Hang »

Thanks el_supremo I got it working now.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: flipping every other line of pixel in an image

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply