Search code examples
linuximage-processingimagemagickimagemagick-convert

How to offset image contents by X,Y pixels using ImageMagick?


I need to offset the pixels in a PNG image by -1 in X and -4 in Y axis.

The images were converted from a PDF created by Corel Draw, which adds an offset, breaking the image processing system I'm working on. align_image_stack from hugin-tools package crashes when processing these files, that's why I resort to trying a fixed offest correction.

I tried this these commands:

$ convert a.png: -geometry 100%-100-100 b.png

$ convert -region '100%+500px+100px' a.png b.png

$ convert -page '100%+500px+100px' a.png b.png

$ convert -repage '100%+500px+100px' a.png b.png

$ convert -crop '100%+500px+100px' a.png b.png

$ convert a.png -geometry 100%-100px-100px b.png

They have all finished without an error, but the gave me the same image I fed them as input.

a.png = b.png

What am I doing wrong? Why the Covert command does not shift the image contents?

EDIT:

Here's a pair of images to illustrate my problem. The first image is what I want, the second is what comes out of Corel Draw, I want to apply an arbitrary X/Y offset to compensate for this difference. The images are faked only to illustrate the problem, this is not authentic data.

Two images

New point is that I was able to produce the offset once, but I can't reproduce this. It looks to me like a bug in ImageMagick, because I'm trying the same command that I used before and it doesn't work now.

I also tried using GraphicsMagick to doublechek this.

I was able to get an offset written to PNG header, but that doesn't make Blender use that offset, so I need to "burn" that offset into the bitmap data, not just specify it in the metadata.

This command did a change, but only GIMP seems understands that and I need to make Blender apply the offset:

convert a.png -repage '100%x100%+100+1000' b.png

I tried using -sample to apply the transformation, but it's not alpplied and stays in the metadata - I can check this with GIMP.

convert a.png -repage '100%x100%+100+1000' -sample 100% b.png

I can't believe I am unable to do such a simple thing.


Solution

  • Updated Answer

    It is hard to understand what you want without proper images, but here is another attempt at guessing a solution for you. Let's start with this image - ignore the colours as I only added them so you can see the extent of the images and you can remove/change them anyway:

    enter image description here

    The new plan is to trim your image so there is no border around the letters at all and then to add in whatever border you want afterwards. So, trimming the existing border and adding a 10px border left and right and a 50px border top and bottom:

    convert start.png -trim -bordercolor red -border 10x50 result.png
    

    enter image description here

    Or, trimming the original border and adding a 10px border to the right side only:

    convert start.png -trim -gravity east -background blue -splice 10x0 result.png
    

    enter image description here

    Hopefully that will give you an approach to achieve what you seek.

    Original Answer

    If you want to reset the page offsets back to zero, the easiest way is:

    mogrify +repage image.png
    

    Or, slightly harder:

    convert image.png +repage result.png