[Solved] Shift image 1 pixel to the right, keep size

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
ddrirc
Posts: 4
Joined: 2012-04-22T13:14:02-07:00
Authentication code: 13

[Solved] Shift image 1 pixel to the right, keep size

Post by ddrirc »

Hi, I'm trying to move all pixels (effectively the whole image) over by one pixel to the right and retain the original size of 120x150.

The below code does ALMOST what I want - it copies the image and moves it over a pixel, but it doesn't get rid of the original image pixels.

Code: Select all

convert test.png -alpha set \( +clone -crop  120x150+0+0 +repage \) \( -clone 0--1 -geometry +1+0 -composite \) -swap 0 +delete \( +clone -alpha opaque \) +swap +delete -gravity east -geometry +119+0 -compose DstOut -composite newtest.png
Any idea how I can fix (or simplify this) to move every pixel over 1 to the right while retaining the 120x150 original size/format?

Thanks!

What I ended up using in the end:

Code: Select all

convert -page +1+0 test.png -background none -flatten newtest.png
Much simpler :lol: Thanks, fmw42
Last edited by ddrirc on 2012-04-23T02:56:00-07:00, edited 4 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shift image 1 pixel to the right, keep size

Post by fmw42 »

You can use -roll to shift the image to the right, but it will wrap the rightmost pixel to the left side. Is that what you want to happen? If not what do you want to be on the left side of the image? You can insert a color if you want using -compose over -composite.

see
http://www.imagemagick.org/script/comma ... eilg1#roll


Alternately, you can crop the image and just mosaic or flatten or composite over some background color or background image, which could be a color or the image itself. Mosaic will expand the canvas.

see
http://www.imagemagick.org/Usage/layers/#mosaic
http://www.imagemagick.org/Usage/layers/#flatten


Another approach is to use -virtual-pixel .... -define distort:viewport=... -distort SRT 0 to shift the image

The virtual-pixel setting will control what fills in the left side.

see
http://www.imagemagick.org/Usage/distor ... t_viewport
Last edited by fmw42 on 2012-04-22T13:41:28-07:00, edited 1 time in total.
ddrirc
Posts: 4
Joined: 2012-04-22T13:14:02-07:00
Authentication code: 13

Re: Shift image 1 pixel to the right, keep size

Post by ddrirc »

I actually want the furthest right pixel to just be lost, no wrapping. And the background needs to remain transparent/alpha so I'm not sure how well flattening would work
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shift image 1 pixel to the right, keep size

Post by fmw42 »

ddrirc wrote:I actually want the furthest right pixel to just be lost, no wrapping. And the background needs to remain transparent/alpha so I'm not sure how well flattening would work
So you want to drop the right column and fill the left with transparency? Is that correct?

Try this:

convert rose: rose.png
convert -page +1+0 rose: -background none -flatten rose_tmp.png

Then alternate each image to see that it is shifted to the right and the same size.
ddrirc
Posts: 4
Joined: 2012-04-22T13:14:02-07:00
Authentication code: 13

Re: Shift image 1 pixel to the right, keep size

Post by ddrirc »

fmw42 wrote:
ddrirc wrote:I actually want the furthest right pixel to just be lost, no wrapping. And the background needs to remain transparent/alpha so I'm not sure how well flattening would work
So you want to drop the right column and fill the left with transparency? Is that correct?

Try this:

convert rose: rose.png
convert -page +1+0 rose: -background none -flatten rose_tmp.png

Then alternate each image to see that it is shifted to the right and the same size.
Works perfectly, thank you!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [Solved] Shift image 1 pixel to the right, keep size

Post by anthony »

Subject: [Solved] Shift image 1 pixel to the right, keep size
ddrirc wrote:What I ended up using in the end:

Code: Select all

convert test.png -page +1+0 -background none -flatten newtest.png
Much simpler :lol: Thanks, fmw42
Note that that will fail! -page is for setting virtual canvas size/offset BEFORE they are read. While -repage is for image already in memory, AFTER they have been read.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [Solved] Shift image 1 pixel to the right, keep size

Post by fmw42 »

Anthony is right. That is not what I wrote above:

convert -page +1+0 rose: -background none -flatten rose_tmp.png
ddrirc
Posts: 4
Joined: 2012-04-22T13:14:02-07:00
Authentication code: 13

Re: [Solved] Shift image 1 pixel to the right, keep size

Post by ddrirc »

Really? I get no error and it seems to do the job, but I will switch the order if you say so.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [Solved] Shift image 1 pixel to the right, keep size

Post by fmw42 »

ddrirc wrote:Really? I get no error and it seems to do the job, but I will switch the order if you say so.
or try

convert test.png -repage +1+0 -background none -flatten newtest.png

see
http://www.imagemagick.org/Usage/layers/#flatten for use of -page

also see for repage
http://www.imagemagick.org/Usage/basics/#page
Post Reply