Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
0
answers
65
views
How to automatically vertically unshear a scanned page in Debian?
Continuing https://unix.stackexchange.com/questions/795046/how-to-automatically-straighten-a-scanned-document-in-debian-linux, how to automatically vertically un-shear a scanned page? Sometimes a combination of an imprecise printing and an imprecise scanning results in the following layout of the te...
Continuing https://unix.stackexchange.com/questions/795046/how-to-automatically-straighten-a-scanned-document-in-debian-linux , how to automatically vertically un-shear a scanned page?
Sometimes a combination of an imprecise printing and an imprecise scanning results in the following layout of the text lines in the resulting image:
You see a vertical shear here. All the text lines go down here; we sometimes see also all the text lines going up. (For privacy reasons, I can't share a real scan here. An artificially created shear can be found in https://ibb.co/DHYWrGYT – it's a shear of page 6 of *The TeXbook* by Donald Erwin Knuth, 2020.)
Out of an image with the above text layout, we'd like to create an image with text lines going horizontally:
The un-shearing has to be done with freely available tools in Debian Linux with the least manual effort; ideally, this would boil down to calling a command that would determine the slope automatically, say,


vertical_shear_fixer scan.png vertically_unsheared_scan.png
. In this question, we assume for simplicity that all the text lines start at the same X value, i.e., that the original scan is not rotated and not horizontally sheared. (Rotating the original scan wouldn't solve the issue: for example, for the image above, rotating counterclockwise could somewhat straighten each text line (or black line) by itself but would introduce a horizontal shear, where the top text lines start more to the left than the bottom text lines; moreover, each letter could look tilted left a tiny bit.) We could start with gray images if it simplifies the matter.
user743115
(1 rep)
Jul 27, 2025, 02:44 PM
• Last activity: Jul 30, 2025, 09:19 PM
0
votes
0
answers
33
views
Relinking OCR data to downscaled images
I have a PDF consisting of scanned pages with OCR done by `tesseract`. I want to downscale the images (by around 4x) and retain the OCR. What would be an automatic way to relink the OCR data to the new coordinates on the pages? --- Original question (*Sudden surge in the size of PDF generated by Gho...
I have a PDF consisting of scanned pages with OCR done by
tesseract
. I want to downscale the images (by around 4x) and retain the OCR. What would be an automatic way to relink the OCR data to the new coordinates on the pages?
---
Original question (*Sudden surge in the size of PDF generated by Ghostscript 9.55.0*):
I have a large PDF and am unsuccessfully trying to reduce its size. (The file is copyrighted material, so I cannot post it; I am doing this for personal use. It must be a common problem, though: I had the same experience earlier with other files from different sources.)
The file is a scan of a book with 1500 B&W A4 pages of text, no pictures at all. The individual pages were mogrify
-ed into PNG images of equal height (around 1000px) and cleaned up via scantailor-advanced
. Then each of the pages (now in TIFF) was tesseract
ed. The results were pdfunite
d into a 200MB file.
This is way too large for this kind of book. I would like to be able to shrink it to around 30MB, perhaps 50. (The total text size extracted by pdftext
is 9MB.)
Most of the PDF compression methods I found on StackExchange and other sites boil down to gs
with varying parameters. On my machine they all behave in a very similar way. I start gs
in the terminal and switch to a GUI file manager. The size of the output file grows slowly and steadily from 0 to around 15MB (no matter the settings), and then it gallops in the last split second, as if gs
gives up and just dumps the input into the output verbatim. (I attributed this to memory shortage, but the program also exhibits similar behavior on a relatively small, 100-page, part of this file.)
If gs
is not told to change the DPI (300), the output file becomes as large as the input was. If the DPI is changed to 72, the file becomes 70MB; this is still too much for such a loss in image quality.
Is there an explanation of this surge? Should I perhaps use some other toolchain on the raw scans, or a different optimization tool? pdfsizeopt
is very slow and seems to lead to 10% reduction. tiff2pdf -j 50
saves 5% (which will be re-added during OCR).
Dilettante
(101 rep)
Jul 25, 2025, 07:17 PM
• Last activity: Jul 26, 2025, 07:19 PM
39
votes
8
answers
37254
views
Is there a way to flatten a .pdf image from the command line?
In GIMP, I can import a PDF, and use the GUI to flatten it (if it was made with many layers) by selecting `Flatten Image` in the `Image` dropdown menu. I can then export the PDF with a new filename. I would like to automate this. Is there some way to do it via the terminal?
In GIMP, I can import a PDF, and use the GUI to flatten it (if it was made with many layers) by selecting
Flatten Image
in the Image
dropdown menu. I can then export the PDF with a new filename.
I would like to automate this. Is there some way to do it via the terminal?
generic_user
(637 rep)
Oct 19, 2014, 12:20 AM
• Last activity: Jul 26, 2025, 01:31 PM
11
votes
4
answers
33239
views
Cropping a JPG image
This is a question about image file cropping. I recently posted a public notice to an Indian newspaper called the Free Press Journal. This is available online as [The Free Press Epaper](http://epaper.freepressjournal.in/). Now, one can download a notice that was posted in the paper as a JPG file. Ho...
This is a question about image file cropping.
I recently posted a public notice to an Indian newspaper called the Free Press Journal. This is available online as [The Free Press Epaper](http://epaper.freepressjournal.in/) .
Now, one can download a notice that was posted in the paper as a JPG file. However, this JPG can, and in my case, does, include multiple notices. These notices are mostly long thin rectangles running vertically, filling up the image.
I'm wondering if anyone can suggest a simple approach to "slice" the segment containing the desired notice out of the image file. I could also convert to another format if it would make this slicing process easier. A method to make the image larger if necessary would also be a plus.
Faheem Mitha
(36008 rep)
Nov 2, 2013, 12:07 PM
• Last activity: Jun 14, 2025, 11:08 PM
0
votes
0
answers
19
views
Image or Photo Mgmt App - 2 visible open images in same frame
Cross posted from linux mint forum, where no response was received. My workflow involves having multiple images open and simultaneously visible and editable, in a single frame, in a single instance of the application. This allows me to paste back and forth between images, without having to (in effec...
Cross posted from linux mint forum, where no response was received.
My workflow involves having multiple images open and simultaneously visible and editable, in a single frame, in a single instance of the application. This allows me to paste back and forth between images, without having to (in effect) make the image in a tab visible. My workflow also involves being able to copy an on-focus image, and then paste it into a new image "automatically" (again in the same frame), where I don't have to first create the new canvas.
This functionality is provided by Paint Shop pro 5, which I have been using under wine in Mint 20. I would very much like to ditch Paint Shop Pro it in favor of a native linux app, that offers this specific functionality.
I have explored the following apps somewhat superficially - to the best of my knowledge, none of them will do what I want:
pinta, gwenview, nomacs, digikam, image magick, gtthumb, krita, gphoto, showfoto.
I also briefly attempted to explore gimp plug-ins; again no joy.
Please let me know if you know of a native linux app or gimp plugin that replicates this particular Paint Shop Pro functionality.
For what it's worth, prior to posting this question, I searched this forum and others for questions directly on point with respect to this particular functionality, and didn't find anything.
Given the situation, and given that Corel Draw (the makers of Paint Shop Pro) services the Apple and Windows operating systems but not linux, I am beginning to suspect that I am beating a dead horse. That is, there may be something inherent in the Linux "windowing" system that makes this functionality problematic.
I also welcome any technical opinions, re the previous paragraph. Further, if you think that I am beating a dead horse, I welcome any suggested app, that you think "comes closest" to what I want.
user2661923
(323 rep)
Feb 3, 2025, 02:33 PM
• Last activity: Feb 3, 2025, 05:29 PM
0
votes
0
answers
20
views
Check Geometrical Match of Two Scanned Images with Standard Linux Tools
I have two images (taken with a scanner at the same resolution) and I want to superimpose them on the screen to see how well two features match to each other after applying the right shift and rotation to one of them (like a combination of a front and the back side of a printed circuit board). The t...
I have two images (taken with a scanner at the same resolution) and I want to superimpose them on the screen to see how well two features match to each other after applying the right shift and rotation to one of them (like a combination of a front and the back side of a printed circuit board).
The two images are shifted and rotated with respect to each other with unknown parameters (
dX
, dY
and rotation angle phi
). In my case there are no straight outlines to calculate a matching corner or measure the relative rotation angle between the two scans.
In Gimp there are tools to flip images, to shift an image and to rotate them. One can superimpose one onto the other by using different layers and applying some degree of transparency to the uppermost layer. Using Gimp's **Tools>Move** and **Tools>Rotate** one after the other is very cumbersome.
There also is an uniform transformation tool in Gimp, but this one also interactively changes the size and applies other geometrical transformations, not only translation and rotation. I see nothing in them to pin the additional parameters.
I want to align the two images by visual inspection, shifting and rotating one of them (e.g. in 50% transparent mode) to the other one for a best fit.
- Did I oversee the right tool in Gimp?
- Or which other standard FOSS tool (and which function in it) do you
recommend for this purpose?
Adalbert Hanßen
(303 rep)
Dec 11, 2024, 10:03 PM
2
votes
4
answers
124
views
How to auto-rotate a rectangular image area by a few degrees so that its left border gets vertical (using command line tools like imagemagick)
I have several thousand images whose main characteristics are as in the attached picture: All images are in a (nearly) black frame, while the actual image content nearly always is on a white background. Now I would like to rotate the actual image content so that this content's left border is vertica...
I have several thousand images whose main characteristics are as in the attached picture: All images are in a (nearly) black frame, while the actual image content nearly always is on a white background.
Now I would like to rotate the actual image content so that this content's left border is vertical. Then I would like to crop (trim) the image so that the black border is thrown away, but keeping the actual content completely. That is, even if the area that holds the actual content is not perfectly rectangular, everything from that area must be kept, which means that small rests of the black frame could still be visible afterwards.
Given the number of images that I'd like to treat this way, I guess I'll have to do that using command line tools. In the past, I have used ImageMagick for that kind of jobs (for transformations that are a lot easier), but I really wouldn't mind combining several different tools to achieve the task.
What I have already tried:
I have researched how to deskew images, and this works most time. However, the deskewing methods I have found work *by making text lines horizontal.* This is nice because it makes reading easier, but of course in most cases the borders of the area that hold the text are not vertical or horizontal afterwards, respectively. That's not what I am after.
To explain in more, detail, I would like to:
1. Detect the left edge between the black frame and the area that holds the actual image content.
2. Fit a (invisible) straight line along that edge.
3. Determine the angle between that line and a vertical line.
4. Rotate the whole image by (the negative) of that angle so that the edge from step 1 becomes vertical.
5. Trim the image, throwing away as much of the black frame as possible, but thereby keeping the complete area that holds the actual content (and thus accepting that rests of the black frame are kept if that area is not perfectly rectangular).
Could somebody explain how to do this, preferably using command line tools?

Binarus
(3891 rep)
Nov 17, 2024, 11:19 AM
• Last activity: Nov 27, 2024, 06:39 AM
1
votes
2
answers
724
views
How to use ImageMagick's convert to rescale an image, adding letterboxing to produce a fixed size result?
I have some arbitrarily-sized images for which I want to produce fixed-size 800x600 thumbnails, which keep the original image ratio and add white letterboxing to make up the rest of the space. I'd like the letterboxing to be as minimal as possible, which means that the process would do the following...
I have some arbitrarily-sized images for which I want to produce fixed-size 800x600 thumbnails, which keep the original image ratio and add white letterboxing to make up the rest of the space. I'd like the letterboxing to be as minimal as possible, which means that the process would do the following:
- choose whichever axis is largest
- if x is largest, scale the image (keeping the ratio) so that new x is 800.
- if y is largest, scale the image (keeping the ratio) so that new y is 600.
- the result, at this point, will not be 800x600 (unless the original happened to be exactly 4:3 ratio), so centre the result in an 800x600 rectangle and add a white background.
I can do the first 3 steps with
convert -resize 800x600 $input $output
so I suppose I could overlay that on top of another image using some other imagemagick tool, but it would be great if I could do it in one hit. Anyone know if this is possible?
Max Williams
(1157 rep)
Feb 29, 2016, 04:48 PM
• Last activity: Nov 19, 2024, 08:46 AM
0
votes
2
answers
63
views
Split a group of images vertically with output in different directories
I have a directory with 170 png photos on a Linux Mint machine, which I want to split in half. I came up with a solution, but it didn't work properly, because it overloaded the cache. The command I had written and has worked for 1 image was: convert -crop 50%x100% ../image_location.png image_locatio...
I have a directory with 170 png photos on a Linux Mint machine, which I want to split in half. I came up with a solution, but it didn't work properly, because it overloaded the cache. The command I had written and has worked for 1 image was:
convert -crop 50%x100% ../image_location.png image_location_cropped.png
I tried to write a loop, but I wasn't successful. Something like this:
~~~
for i in Testovnica_chemia-???.png
do
convert -crop 50%x100% ../Testovnica_chemia-???.png Testovnica_chemia-???_upravene.png
done
~~~
Error message looked like this:
convert-im6.q16: cache resources exhausted `../Testovnica_chemia-056.png' @ error/cache.c/OpenPixelCache/4083.
How should I modify it?
Linux_user_1654
(3 rep)
Sep 9, 2024, 02:44 PM
• Last activity: Sep 9, 2024, 08:49 PM
3
votes
1
answers
106
views
Reshape an image with ImageMagick
I know I can resize a image with ImageMagick using `-resize` or `-scale`, but how can I ***reshape*** a image with ImageMagick? If I use `-resize` or `-scale`: magick image.png -resize 160x a.png magick image.png -scale 160x b.png I will get this: [![enter image description here][1]][1] But I want t...
I know I can resize a image with ImageMagick using
But I want to have exactly the same number of pixels with exactly the same values in exactly the same order, but they will be *"re-flowed"* if that is a word into a different shape.
I have already worked out how to do it with Python Imaging Library and Numpy. My starting image is 80x80 and looks like this:
from PIL import Image
import numpy as np
image = Image.open('a.png').convert('RGB')
npArray = np.array(image)
Image.fromarray(npArray.reshape(-1,160,3)).save('largeur160.png')
Image.fromarray(npArray.reshape(-1,40,3)).save('largeur40.png')
Image.fromarray(npArray.reshape(-1,10,3)).save('largeur10.png')
I get these images with width 160, and 40 and 10:
-resize
or -scale
, but how can I ***reshape*** a image with ImageMagick? If I use -resize
or -scale
:
magick image.png -resize 160x a.png
magick image.png -scale 160x b.png
I will get this:





Sophie Bertillon
(83 rep)
Aug 25, 2024, 09:17 AM
• Last activity: Aug 25, 2024, 09:52 AM
0
votes
1
answers
348
views
Convert SVG to PNG using the command line with '-w' and '-h' values applied as a "box"
Here is an SVG image from Wikipedia: (Downloadable version: https://upload.wikimedia.org/wikipedia/commons/3/3d/CH-Hinweissignal-Wegweiser_f%C3%BCr_Hauptstrassen.svg) ``` image/svg+xml ``` The following commands successfully converts it to 2000×493 and 406×100 pixel PNGs respectively: - `i...
Here is an SVG image from Wikipedia:
(Downloadable version: https://upload.wikimedia.org/wikipedia/commons/3/3d/CH-Hinweissignal-Wegweiser_f%C3%BCr_Hauptstrassen.svg)
image/svg+xml
The following commands successfully converts it to 2000×493 and 406×100 pixel PNGs respectively:
- inkscape -w 2000 input.svg -o output.png
- inkscape -h 100 input.svg -o output.png
But if I specify both width and height (-w 2000 -h 100
), both the values are used forcefully, and so the resulting image is distorted (2000×100 px).
Is it possible to specify them in such a way that they are applied as a "box" instead, that is,
- inkscape -w 2000 -h 100 input.svg -o output.png
should results in 2000×493 px image
- inkscape -w 100 -h 2000 input.svg -o output.png
should results in 100×25 px image
jsx97
(1347 rep)
Aug 2, 2024, 01:29 PM
• Last activity: Aug 19, 2024, 04:38 PM
0
votes
2
answers
75
views
imagemagick convert: limit trim to max X px
Using ImageMagick (6.9.11-60 on debian stable), I'd like to shave off 24px left and right of an scanned page (jpg or TIFF), but only if that area would actually be "empty" (i.e. mostly white). I'd use `-shave`, but that removes pixels even if the area isn't white. I then thought of `-trim` but I lac...
Using ImageMagick (6.9.11-60 on debian stable), I'd like to shave off 24px left and right of an scanned page (jpg or TIFF), but only if that area would actually be "empty" (i.e. mostly white).
I'd use
-shave
, but that removes pixels even if the area isn't white.
I then thought of -trim
but I lack the idea how to limit it to a max of 24px.
Question: How can I remove 24px left and right of a scanned image, but _only if the area is empty_?
I'm open to not using IM, it was just what first came to mind.
Jan
(7962 rep)
Jan 22, 2024, 09:54 AM
• Last activity: Jun 5, 2024, 12:43 PM
46
votes
4
answers
40247
views
How to split an image vertically using the command line?
Say I have a large 800x5000 image; how would I split that into 5 separate images with dimensions 800x1000 using the command line?
Say I have a large 800x5000 image; how would I split that into 5 separate images with dimensions 800x1000 using the command line?
shley
(1121 rep)
Nov 23, 2014, 01:40 PM
• Last activity: May 23, 2024, 02:59 PM
10
votes
1
answers
10990
views
overlay an image
I have two photos ( 1.jpg and 2.jpg) I want to make photo 2.jpg as a background and photo 1.jpg red colored overlay and I want the output to be like photo 3.jpg as follow ( I want to put photo number 1 on top of photo number 2 and make it transparent 50%): **Photo 1.jpg ( overlay)** [


user88036
Nov 16, 2015, 10:04 PM
• Last activity: Mar 17, 2024, 03:20 PM
1
votes
1
answers
649
views
difference between convert -quality and convert -resize and convert -density
I'm trying to reduce file size of a .png in the terminal with Imagemagick `convert` Does anyone know the difference between `convert -quality` , `convert -resize` and `convert -density` ? `convert -quality` does not seem to work at all, whether you give it 99, 30, 10 or 1. It changes the file size s...
I'm trying to reduce file size of a .png in the terminal with Imagemagick
convert
Does anyone know the difference between convert -quality
, convert -resize
and convert -density
?
convert -quality
does not seem to work at all, whether you give it 99, 30, 10 or 1. It changes the file size sometimes even making it bigger!
This is a file of about 10MB. I am trying to bring it down to perhaps 1MB.
I am scared that convert -resize
might do something to it such that if someone goes to print it out it will not be the size of an A4 page anymore.
cardamom
(662 rep)
Feb 22, 2024, 10:40 AM
• Last activity: Feb 22, 2024, 11:00 AM
21
votes
9
answers
11336
views
How can I split each PDF page into two pages, using the command line?
I have a few ebooks scanned from originals. They're formatted so that a single *PDF page* contains two *actual pages*: one on the left, and one on the right. I want to programmatically split each PDF page into two, so the left 50% of PDF page 1 becomes page 1 and its right becomes page 2, and so on...
I have a few ebooks scanned from originals. They're formatted so that a single *PDF page* contains two *actual pages*: one on the left, and one on the right.
I want to programmatically split each PDF page into two, so the left 50% of PDF page 1 becomes page 1 and its right becomes page 2, and so on for all the pages.
Does anyone know of a command line utility or script that could help with this?
----------
Output from
pdfimages -list -f 1 -l 1 file.pdf
:
page num type width height color comp bpc enc interp object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
1 0 image 1921 1561 rgb 3 8 jpeg no 643 0 200 200 200K 2.3%
1 1 stencil 1 1 - 1 1 image no [inline] 0.692 2 - -
1 2 stencil 1 1 - 1 1 image no [inline] 0.722 0.650 - -
1 3 stencil 1 1 - 1 1 image no [inline] 3 3 - -
Second PDF:
page num type width height color comp bpc enc interp object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
1 0 image 456 625 gray 1 8 jpx yes 251 0 72 72 11.7K 4.2%
Philip Kirkbride
(10746 rep)
Nov 19, 2017, 03:06 PM
• Last activity: Jan 3, 2024, 12:57 AM
0
votes
1
answers
171
views
Print two half-sized copies of a two-sided document at once
I have a two-sided, us-letter format document that I'd like to print two copies at once, by resizing it to half letter (a.k.a. [statement, or organizer L](https://en.wikipedia.org/wiki/Paper_size#Half_letter)). My current workflow is very inconvenient: - The original document is two-sided, A, B, C,...
I have a two-sided, us-letter format document that I'd like to print two copies at once, by resizing it to half letter (a.k.a. [statement, or organizer L](https://en.wikipedia.org/wiki/Paper_size#Half_letter)) .
My current workflow is very inconvenient:
- The original document is two-sided, A, B, C, etc.
- Using PDFSam's [mix-pdf](https://pdfsam.org/mix-pdf/) , duplicate each page (so that the document is A, A, B, B, C, C, …),
- Using pdfjam, resize the document and have two pages by page (so that I have A A on the same page, on landscape mode, one of top of the other, etc.),
- Print using
lp -o sides=two-sided-long-edge
.
I am sure there is a simpler way to achieve this, but couldn't find it. An additional hurdle of my current way of doing is that pdfjam introduces extra margin, that I have to remove using trim
.
Clément
(378 rep)
Nov 25, 2023, 03:02 PM
• Last activity: Nov 26, 2023, 08:40 PM
0
votes
0
answers
201
views
Imagemagick, how to remove a stamp and marks from an image?
The scanned document image (grayscale) have two handwritten marks on the top and seal stamp at the bottom. I want to remove these marks and seal by replace them (hide) with same size rectangles, copied from another region of the image (part of background). How to do this with Imagemagick? Most impor...
The scanned document image (grayscale) have two handwritten marks on the top and seal stamp at the bottom. I want to remove these marks and seal by replace them (hide) with same size rectangles, copied from another region of the image (part of background). How to do this with Imagemagick? Most important, how to get coordinates of these objects and rectangles? (areas where the background should be copied from).
IM v6.9 on Debian.

minto
(575 rep)
Jul 26, 2023, 11:19 PM
• Last activity: Nov 2, 2023, 07:33 PM
0
votes
4
answers
2821
views
Batch conversion of ".tif" images to ".jpg"
I am using Manjaro GNU/Linux. I have a directory named `files`; under this directory, I have around 650 subdirectories with names such as `file1`, `file2`, `file3`, etc. Under each subdirectory, there are between 2 and 11 `.tif` images. I want to write a command or script to automatically convert al...
I am using Manjaro GNU/Linux. I have a directory named
files
; under this directory, I have around 650 subdirectories with names such as file1
, file2
, file3
, etc.
Under each subdirectory, there are between 2 and 11 .tif
images.
I want to write a command or script to automatically convert all .tif
images to .jpeg
or .jpg
so they remain in the same subdirectory and have the same name as before.
I know that there is the command:
convert source.tif ~/converted.jpg
Can you please help?
Thanks!
Arun
(137 rep)
May 14, 2020, 12:44 PM
• Last activity: Oct 11, 2023, 11:16 AM
1
votes
3
answers
1417
views
How can I modify the contents of an image with a bash script?
Say I want to overwrite the top half of a file "image.png" with white pixels. Can I do that with a bash script?
Say I want to overwrite the top half of a file "image.png" with white pixels. Can I do that with a bash script?
Vishnu Prasad
(11 rep)
Oct 28, 2019, 01:48 PM
• Last activity: Oct 11, 2023, 01:24 AM
Showing page 1 of 20 total questions