How to compress PDF when creating from GIF files? - pdf

I have ~200 gif black-and-white files, 600-900 Kb each. The overall size is about 102 Mb.
I create pdf from them -
convert *.gif result.pdf
result.pdf file size is about 98 Mb. But I need a PDF file of size less than 25 Mb.
I tried different options - compress, quality, density - but none of them helped.
What else could I try?

Try
convert 150_orig.gif -compress FAX -type bilevel tiff:- | convert - result.pdf
This reduces the file size from 613448B to 157477B.

Related

ImageMagick converts PDF into tiny images despite setting density and resize options

I'm using ImageMagick to convert the following PDF to an PNG file: PDF from IMSLP (Permalink)
In a PDF viewer it looks nice (even though it needs quite a bit of zooming):
but when converting with
convert "file.pdf" "/tmp/file.png"
the produced image gets an extremely low resolution:
when adding density and resize information, I get somewhat bigger images, but still not the original resolution that is stored within the PDF (certainly not 300 DPI)
convert -density "300" -resize "3000x3000>" "file.pdf" "/tmp/file.png"
When using Poppler-Utils' pdfimages, I'm getting the appropriate image:
My question is: Is there any way to tell ImageMagick to extract the images in the "correct" resolution (as is stored in the PDF document)? In other words, ignore the zoom that is necessary to view the PDF properly, thus extracting the correct image resolution?
I'm using ImageMagick 7.1.0.16 with Ghostscript 9.55.0 inside an Alpine Linux docker image.
Very unusual structure you have there its been through many changes but we can guess some pages may have been converted to 300 dpi or 600 dpi since they all render at roughly the same size.
Note that graphics dpi is subjective it is not that value that's used inside a PDF it is the the pixels per default of 72 point units that relate to a graphics working dpi. the image may have been 75 dpi but stored at 300 pixels per 72 points.
1st Analysis says images are
image-0028 = 714 X 900 dots nominally 600 dpi
image-0002 = 726 X 900 dots nominally 600 dpi
image-0005 = 674 x 900 dots nominally 600 dpi
image-0008 = 674 x 900 dots nominally 600 dpi
image-0011 = 674 x 900 dots nominally 600 dpi
image-0014 = 674 x 900 dots nominally 600 dpi
but all have been down-sampled to various sizes approx. 1.2" x 1.5" so a sensible source size to match all those reductions is possibly
9.6" x 12" with some cropping.
Thus to get the nearest original quality extract pages # 600 dpi (lossless png would be best to keep those lossy jpeg flaws)
Then reconvert them to 75 dpi should give you the closest to the poor quality inputs.
You need to increase your density much larger and put your resize after reading the input in Imagemagick.
This will be 5800 × 7200 pixels:
convert -density 4800 IMSLP358086-PMLP578359-Ehr_OP_20_5.pdf[1] x.png
This will be 2417 × 3000 pixels:
convert -density 4800 IMSLP358086-PMLP578359-Ehr_OP_20_5.pdf[1] -resize "3000x3000>" y.png

Reading avi file, getting single frames with cv2.VideoCapture and video.read, and the png files of the single frames are much bigger than the avi

I'm reading an avi file with approx 2MB size, 301 frames, 20 frames/sec (15 sec long video) and a size of 1024 * 1096 per frame.
When I'm reading the single frames with cv2 and resaving them in original size as png, then I'm getting a size of approx 600KB per picture/frame. So, I have in total 301 * 600KB = 181MB (original avi had 2MB).
Any idea why this is happening and how to reduce the file size of the single frames without changing the resolution? Idea is to somehow generate single frames from the original video, do detections with CNN and to resave the original video again with included detections and the output video shall be somehow very similar to input video (approx same file size, must not be avi format)
PNG files or single frames are in the most cases always larger than the original video file (compressed in the most cases by a codec https://www.fourcc.org/codecs.php). Use for example the following command on Linux to create a compressed avi:
ffmpeg -i FramePicName%d.png -vcodec libx264 -f avi aviFileName
You can get the used codec to create the original video file by the following python cv2 code
cap = cv2.VideoCapture(videoFile)
fourcc = cap.get(cv2.CAP_PROP_FOURCC) # or cv2.cv.CV_CAP_PROP_FOURCC
"".join([chr((int(fourcc) >> 8 * i) & 0xFF) for i in range(4)])

Why the cropped raster tiff generated from gdal_tranlaste is uncompressed and of very big size?

I am cropping a tif image file using gdal_translate but the resulting file is of bigger size compared to the original file.
Here is the sample command that I am using to crop the image
gdal_translate -srcwin 4000 4500 2000 3000 Ortho.tif Ortho_cropped.tif
You can compress the output too with the following
gdal_translate -srcwin 4000 4500 2000 3000 -co COMPRESS=DEFLATE -co PREDICTOR=2 Ortho.tif Ortho_cropped.tif
This uses deflate compression which is often quite effective. A predictor of 2 is often nice for integer values, while you can change the predictor to 3 if your values are floats.
You can use other compressions too like ZSTD if you are using gdal >= 2.3 which should be faster and archive similar compression rates. If you do not care about lossy compression, you can even use JPEG.
If you do not know the compression of your original image you can see what it is using the command gdalinfo Ortho.tif where the compression is described under Image Structure Metadata

Decrease pdf-filesize in ImageMagick

I am using GIMP to convert grayscale PNM-files (scanned documents) to PDF.
My goal is a small filesize. (Ideally: viewable on different devices without any problems and maybe suitable long-term preservation - PDF/A?)
So far, so good. Trying to reproduce that process with ImageMagick in a batch script doesn't give me that same small filesize as in GIMP.
GIMP (Ver. 2.8.14) workflow:
Open File
Change resolution (density) to 300x300 Pixel/in
Set threshold to 127 (=50%)
Export as OutGIMP.pdf
ImageMagick (Ver. 6.7.9-0 2012-09-16 Q16) workflow:
convert Scan.pnm -density 300x300 -threshold 50% -monochrome OutA.pdf
convert Scan.pnm -density 300x300 -threshold 50% -monochrome OutB.png
convert OutB.png OutC.pdf
Using an example File this results in:
OutGIMP.pdf: 141.195 Byte
OutA.pdf: 684.245 Byte
OutB.png: 137.246 Byte
OutC.pdf: 217.860 Byte
How can I get a PDF with ImageMagick that is at least as small as the GIMP-PDF?
Edit
Continuing the GIMP (Ver. 2.8.14) workflow from above with:
Scale to 100x100 Pixel/in while keeping the Imagesize
Export as OutGIMP_100ppi.pdf
strangely results in:
OutGIMP_100ppi.pdf: 179.123 Byte

Image Magick: Image optimization for websites

I have a camera which produces photographs of 3008x2000 pixels. I use Image Magick to scale and resize the photos to be put up on my website. The size of the images I am using on the website is 602x400. I use this command to reduce the size:
convert DSC_0124.JPG -scale 20% -size 24% img1.jpg
This produces an image which is 602x400 pixels in size. But the file size will be always above 250KB. More images on a single html page means the page will be heavier and loading time will be longer. Are there any features in image magic that will help me to keep the file size as small as possible, possibly, below 100KB. But the image size should be the same, that is, 602x400px. I have achieved similar optimisation with SEAMonster tool for MS Windows. As it doean't have a commandline alternative, it wouldn't be of much help when there are hundreds of images to be converted.
Use command as Delan proposed with additional "-strip" flag to remove EXIF data, this have reduced the size of some of my images drastically. Here is a bash script for unix platforms, but you can use the second part only for individual images.
for X in *.jpg; do convert "$X" -resize 602x400 -strip -quality 86 "$X"; done
This will convert all images in the directory.
Use -quality to set the compression level:
convert DSC_0124.JPG -scale 20% -size 24% -quality [0..100] img1.jpg
You can define the maximum size of the output image at 100KB like this:
convert DSC_0124.JPG -resize 602x400! -strip -define jpeg:extent=100KB img1.jpg
If you are running your website on PHP, you might want to consider the SLIR image resizing script, it does a great job resizing to various constraints (see below) and caches the results.
Parameters:
w Maximum width
h Maximum height
c Crop ratio
q Quality
b Background fill color
p Progressive
http://shiftingpixel.com/2008/03/03/smart-image-resizer/
http://code.google.com/p/smart-lencioni-image-resizer/