Showing posts with label laziness. Show all posts
Showing posts with label laziness. Show all posts

2014-04-20

Uniform Server Zero + mod_wsgi + Falcon Framework on Windows 7

Setting up a local test environment before deployment on shared hosting (Dreamhost / Phusion Passenger) (assumes python already properly installed):
  • Install UniServerZ: Uniform Server - A lightweight mobile WAMP Server Solution.
  • Download1: mod_wsgi library, extract,  and copy .so to UniserverZ\core\apache2\modules
  • Install Falcon: python -m pip install falcon
  • Edit httpd.conf:
  • add:
       LoadModule wsgi_module modules/mod_wsgi.so
       WSGIScriptAlias /app "${US_ROOTF_WWW}/passenger_wsgi.py"
  • Edit passenger_wsgi.py:
  • import falcon
    class Test:
       def on_get(self, req, resp):
          resp.status = falcon.HTTP_200
          resp.body = ('<h1>Test.</h1>')
    app = application = falcon.API('text/html')
    app.add_route('/', Test())
    
  • Hit local server(assumes default port): http://127.0.0.1/app/
1 Choosing the right library: match the version of python installed with downloads. Failure to do so results in the following errors:
\UniServerZ>httpd_z -t
httpd_z: Syntax error on line 187 of x:/xxx/UniServerZ/core/apache2/conf/httpd.conf: Cannot load modules/mod_wsgi.so into server: The specified module could not be found.
Occurs if the mod_wsgi.so version is different from python version installed. (What, the error should actually help you know that? ...)

\UniServerZ>httpd_z -t
httpd_z: Syntax error on line 187 of x:/xxx/UniServerZ/core/apache2/conf/httpd.conf: Cannot load modules/mod_wsgi.so into server: %1 is not a valid Win32 application.
Occurs if the .so is 64bit and installed UniServer is 32bit.

2013-03-12

Notes To Self: NFS / ReadyNAS 600 / Windows 7

Microsoft's done the usual outstanding job of quality work with their NFS client.

ReadyNAS end settings:
Frontview: Share Listings->Share Name NFS

Default Access: Read-Only
Write Enabled Hosts: Self explanatory.

Root privilege-enabled hosts: Blank (unneeded)

Advanced Options: nobody/nogroup
Set ownership and permissions ... -> Apply

Squashing isn't needed on the ReadyNAS...
Via ssh to check /etc/exports:

NAS:/etc# cat exports

"/pub" *(insecure,insecure_locks,ro,async)
x.x.x.1(insecure,insecure_locks,rw,async)
x.x.x.250(insecure,insecure_locks,rw,async)

ReadyNAS's default nobody/nogroup = 65534.

Windows end settings:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default\AnonymousGid = FFFE
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default\AnonymousUID = FFFE

To check next time I screw with the Windows side: right click, properties, "NFS Attributes"-> see if setting attributes here actually work and take.

To figure out:
Network Error - 85 "The Local device name is already in use." (doesn't go away restarting NFS client (easy: nfsmgmt.msc) or workstation/server. (With mountpoints2 key removed in registry).  Where is Windows keeping the drive name until reboot, and is there a way of making it forget, so as to remount?

Further notes: Windows 7 needs -o nolock with FreeNAS, else copying gives error 0x80070021.

References:
NFS Overview and Gotchas
Configure Windows NFS Client With Write Permissions
Steps To Disable Client For NFS Cache
Client For NFS - Configuration And Performance
Windows Mount Command syntax
NFS Share Setup For Windows 7 and Freenas

2012-03-21

Batch Straightening/Deskewing and Autocropping Scanned Images

Recently, I had the need to scan over 300 hand drawn "line art" pictures.

Vuescan's 'autocrop' is beyond worthless: it lacks the capability to properly crop where it should, resulting in me just scanning at 'maximum' and ending up with a mass of images flanked by black on all sides... and many of them, just slightly off center (some, more so than others, very noticeably skewed.)

For such a common task (take [x] amount of images, automatically do [y] and [z] to them), there seems to be precious little out there that'll do what I needed.

In my searching, Adobe Photoshop is fabled to have a batch 'Straighten and Crop' function. I don't have Photoshop, so I don't know if it does.

I DO have gimp, though, and got to the point of stopping just shy of copying a random script off the Internet into my plugins directory to try.

Prior to that, I verified:
- Irfanview couldn't do this in a batch,
- nor could Xnview,
- neither could Paintshop Pro 9
and based on google searches, both
- Windows Live Gallery,
- and Paint.net can't do it, either.

Many programs, of course, make it somewhat easy to select a given color, invert selection, crop to selection, and then slightly adjust the rotation. None (unless Photoshop's hype is correct) actually allow this to be done in a batch.

That brings us to ImageMagick (and not, sadly, its supposed faster cousin, GraphicsMagick, which vulgarly lacks its own deskew option.)

ImageMagick will happily let you batch what you want, and comes with the facility to autocrop and deskew.

Like all good opensource apps, though, ImageMagick's documentation leaves much to be desired: I never did figure out what the various percentages to deskew do (from a few tests, I couldn't see any difference between 1%, 40% (default), and 100%), or how to make the crop option for deskew actually work the way I wanted it to (ie, -set option:deskew:auto-crop [x]).

Also, in an effort to be as lazy as possible, I tried Fred Weinhaus' 'unrotate' script. Unfortunately, it was worse than junk; even after filling the black borders to be white, it still did the 'wrong thing' 100% of the time. Perhaps it's a version/configuration issue --- whatever the case, had it worked, it'd have been exactly what I needed. Since it didn't, I ended up with the following, which worked spectacularly well for me:

for a in *.tif; 
do 
convert $a -set filename:f "%t" -background black -fuzz 75% -deskew 50% -trim +repage out/%[filename:f]_cropped.png;
done;

Some notes for my fellow IM noobs: The -background black is to fill the areas created with the -deskew option. By default, they're white.  My initial (very naive) solution was to do this:

-fill black -floodfill +0+0 white -floodfill +0+90% white -floodfill +90%+1% white -floodfill +90%+98% white

which worked for all but 6 images in a batch of 142.  The -fuzz 75% is for trim, and because the 'black' from around the image's sides is more of a 'mostly black with random coloured pixels thrown in'.  I'm not positive the +repage is needed (again, ImageMagick's documentation is... lacking) but it seems like a good idea to make sure the canvas is resized when the image is (?).  Also, for half my images, I needed to change from portrait to landscape orientation, which was easily accomplished with: -rotate 90.

Further note: mogrify, on my setup, seems to always ignore -path.  After the batching of deskew/trim, I needed to up the DPI for publication: (tiff pixel reduction gave me 150dpi from 300dpi scans) mogrify -path jpg/ -units PixelsPerInch -resample 300 -format jpg *.png -> still results in the jpegs in the same directory.

Reference:
GIMP Plugin Registry - Deskew - Batch script in comments. (No auto-crop) - Didn't try this...
Fred Weinhaus' Unrotate Script - Tried this, couldn't get it to produce worthwhile results with any input I gave it.
ImageMagick Documentation - Some of the commandline parameters even work!
Scan Tailor - A neat, opensource program I ran across searching for a solution; it has an 'automatic batch mode', but unfortunately, its automatic deskew/content selection isn't really there yet. However, it is very useful for tweaking the few images too skewed for IM to properly handle.

2012-01-30

Generating PDF Bookmarks Automatically When Combining PDFs

A quick script to generate a proper bookmark file for jpdfbookmarks from a list of PDFs.  I find myself needing this enough, the "evince on one monitor, vim on the other" method became tedious.

autobookmark.sh:


#!/bin/sh
p=1;
for f in *.pdf
do
   fn=`printf "$f" | sed 's/\.pdf$//i'`
   printf "%02d - %s/%d,FitPage\n" $p "$fn" $p
   p=$(($p+`pdfinfo "$f" | awk '/Pages/ {print $2}'`))
done

So, given this input:
Chipsets_And_Datasheets/Initio_Corporation $ ls -1
Initio-2009_Bridge_Controller_Roadmap.pdf
Initio-INIC1430-P1394_to_ATA_Bridge_Spec.pdf
Initio-INIC1511-USB_to_PATA_Bridge_Spec.pdf
Initio-INIC1606-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1608-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1610-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1611-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1622-PCI_to_SATA.pdf
Initio-INIC2430-Firewire_to_ATA_Bridge_IC.pdf


The output would be (with some slight cleanup specific to this input):
$ autobookmark.sh | sed 's/Initio-//;s/_/ /g;s/-/ - /2' > bookmarks.txt
$ cat bookmarks.txt
01 - 2009 Bridge Controller Roadmap/1,FitPage
10 - INIC1430 - P1394 to ATA Bridge Spec/10,FitPage
47 - INIC1511 - USB to PATA Bridge Spec/47,FitPage
73 - INIC1606 - USB to SATA Bridge Spec/73,FitPage
88 - INIC1608 - USB to SATA Bridge Spec/88,FitPage
123 - INIC1610 - USB to SATA Bridge Spec/123,FitPage
159 - INIC1611 - USB to SATA Bridge Spec/159,FitPage
174 - INIC1622 - PCI to SATA/174,FitPage
257 - INIC2430 - Firewire to ATA Bridge IC/257,FitPage


And then it's a simple matter to make the pdf compilation and apply:
$ pdftk *.pdf cat output Initio_Chipset_Datasheets.pdf
$ jpdfbookmarks Initio_Chipset_Datasheets.pdf --apply bookmarks.txt --force

Requirements:
pdftk - The PDF Toolkit
JPdfBookmarks - Excellent util for adding bookmarks to PDFs.
pdfinfo - XPDF viewer package.