how to save GhostScript output - bash

I am creating an image on the fly using ghostscript. How do I write the my_image.png to /home/pi/Desktop location. I can get the image to print to screen, but not save the output to a location.
gs -sDEVICE=pngalpha -sOutputFile=- -q -r100 -g600x100 - >my_image.png

I changed the OutputFile line to
gs -sDEVICE=pngalpha -sOutputFile=/home/pi/my_image.png -q -r100 -g600x100 -

Related

issue of converting .eps file to .png file with gs

I tried to use the command to convert the .eps file to .png file
gs -dSAFER -dEPSCrop -r300 -sDEVICE=pngalpha -o test.png test.eps
But the figure has the wrong bbox, just like viewing it in the letter size in gv.
If I used
gs -dEPSCrop -r300 -sDEVICE=bbox -o test.png test.eps
gs is able to find the correct bbox as (well close, the exact BoudingBox in the .eps file is "21 54 1101 558"):
%%BoundingBox: 35 62 1083 554
%%HiResBoundingBox: 35.040001 62.400002 1082.640041 553.440021
Is there something I miss to convert the .eps file to .png file with the bbox? Thank you.

gdal_translate only translating first three bands from .vrt to .tif

I'm trying to translate a set of .tif images to one multiband .tif following this tutorial. I'm not getting any errors but every time I run the script it generates a three-band image when I have a lot more input .tifs than that. I have tried the -r flag and it didn't change anything.
I have:
wdir="/Users/<mydir>"
creoleLA="$wdir/data/s2l2a_tiffs/T15RVP"
dhaka="$wdir/data/s2l2a_tiffs/T45QZG"
easthouston="$wdir/data/s2l2a_tiffs/T15RTN"
kolkata="$wdir/data/s2l2a_tiffs/T45QXF"
murcia="$wdir/data/s2l2a_tiffs/T30SXH"
wuhan="$wdir/data/s2l2a_tiffs/T50RKU"
#=============================================
# Main script
#=============================================
read -p 'ROI: ' roi
indir=${!roi}
outdir="$wdir/data/s2l2a_tiffs"
ls ${indir}*"_B"*.tif > "$outdir/btif_${roi}.txt"
gdalbuildvrt -separate -overwrite -input_file_list "$outdir/btif_${roi}.txt" "$outdir/S2L2A_${roi}.vrt"
gdal_translate -strict -ot uint16 "$outdir/S2L2A_${roi}.vrt" "$outdir/S2L2A_${roi}_mb.tif"
rm "$outdir/S2L2A_${roi}.vrt"
rm "$outdir/btif_${roi}.txt"
Where "$outdir/btif_${roi}.txt" is a textile of the GeoTIFF file paths like this:
/Users/<mydir>/data/s2l2a_tiffs/T15RVP_20200831T164849_B01_60m.tif
/Users/<mydir>/data/s2l2a_tiffs/T15RVP_20200831T164849_B02_60m.tif
...
I am processing Sentinel-2 imagery and using OSX 11.6.

ghostscript pdfwrite specify jpeg quality

I'm trying to concatenate multiple pdf files which basically are the pages of a photobook containing jpg images. For my output pdf file I wish to adjust the image resolution to 300 dpi and I want to keep the best quality. The commands I'm using are:
gswin64c.exe -dNOPAUSE -dBATCH ^-dDownsampleColorImages=true -dColorImageResolution=300 ^-dDownsampleGrayImages=true -dGrayImageResolution=300 ^-dDownsampleMonoImages=true -dMonoImageResolution=300 ^-sDEVICE=pdfwrite -dJPEGQ=100 -sOutputFile=out.pdf in1.pdf in2.pdf
However, it seems that -dJPEGQ=100 has no effect on the output. Changing this parameter leads to the same filesize and artifacts are visible in the images for all values. Running the command with the option -dPDFSETTINGS=/printer I get better results without artifacts, however this option should also result in 300 dpi. So what is the correct command to specify the quality of the jpg images in the output file?
The solution is adjusting the DCTEncode filter with follwing commands:
gswin64c.exe -sOutputFile=out.pdf -dNOPAUSE -dBATCH ^-sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -c "<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor 0.08 /Blend 1 >> /ColorImageDownsampleType /Bicubic /ColorConversionStrategy /LeaveColorUnchanged >> setdistillerparams" -f in1.pdf
which lead to a compressed file with satisfactory quality for me and can be adjusted to each individual needs.
Edit:
the .setpdfwrite argument is deprecated for recent ghostscript releases (> 9.50), so I removed it in the answer

Use exiv2 or imagemagick to remove EXIF data from stdin and output to stdout

How can I pipe an image into exiv2 or imagemagick, strip the EXIF tag, and pipe it out to stdout for more manipulation?
I'm hoping for something like:
exiv2 rm - - | md5sum
which would output an image supplied via stdin and calcualte its md5sum.
Alternatively, is there a faster way to do this?
Using exiv2
I was not able to find a way to get exiv2 to output to stdout -- it only wants to overwrite the existing file. You could use a small bash script to make a temporary file and get the md5 hash of that.
image.sh:
#!/bin/bash
cat <&0 > tmp.jpg # Take input on stdin and dump it to temp file.
exiv2 rm tmp.jpg # Remove EXIF tags in place.
md5sum tmp.jpg # md5 hash of stripped file.
rm tmp.jpg # Remove temp file.
You would use it like this:
cat image.jpg | image.sh
Using ImageMagick
You can do this using ImageMagick instead by using the convert command:
cat image.jpg | convert -strip - - | md5sum
Caveat:
I found that stripping an image of EXIF tags using convert resulted in a smaller file-size than using exiv2. I don't know why this is and what exactly is done differently by these two commands.
From man exiv2:
rm Delete image metadata from the files.
From man convert:
-strip strip image of all profiles and comments
Using exiftool
ExifTool by Phil Harvey
You could use exiftool (I got the idea from https://stackoverflow.com/a/2654314/3565972):
cat image.jpg | exiftool -all= - -out - | md5sum
This too, for some reason, produces a slightly different image size from the other two.
Conclusion
Needless to say, all three methods (exiv2, convert, exiftool) produce outputs with different md5 hashes. Not sure why this is. But perhaps if you pick a method and stick to it, it will be consistent enough for your needs.
I tested with NEF file. Seems only
exiv2 rm
works best. exiftool and convert can't remove all metadata from .nef FILE.
Notice that the output file of exiv2 rm can no longer be displayed by most image viewers. But I only need the MD5 hash keeps same after I update any metadata of the .NEF file. It works perfect for me.

add a .pdf file(.png basically) to the end of every page in another .pdf file

I have a code.ps file converted to code.pdf , which I want to add to the end of every page in my test.pdf , i.e shrink the test.pdf's every page and add an image to the end of it .
I have written the following shell script to it , but it appends the code.pdf as a new page after every page of test.pdf ! ...Kindly help . Here is my code :-
#!/bin/sh
filename=test.pdf
pages="`pdftk $filename dump_data | grep NumberOfPages | cut -d : -f2`"
numpages=`for ((a=1; a <= $pages; a++)); do echo -n "A$a B1 "; done`
pdftk A=$filename B=code.pdf cat $numpages output $filename-alternated.pdf
exit 0
A simple example of stamping an image (image.[pdf,png] onto a multipage pdf (text.pdf) allowing for manual tweaking of the scaling and offsets using pdfjam and pdftk could be:
# scale and offset the text part
pdfjam --scale 0.8 --frame True --offset '0cm 2.5cm' text.pdf
# scale and offset the image
pdfjam --paper 'a4paper' --scale 0.3 --offset '7cm -12cm' image.pdf
# combine both
pdftk text-pdfjam.pdf stamp image-pdfjam.pdf output combined.pdf
This might look like
If you start with an image file (png, jpg) you can convert it to pdf using imagemagick like
convert image.png image.pdf
Of course, the scale factors and offsets have to be adjusted to your needs. I included the --frame option to highlight the scaling of the text.pdf part. The stamp option overlays the image, whereas the background option would underlay the image.

Resources