Saturday, February 09, 2013

How to convert(1) foo.pdf to bar.png with high resolution

Short version:use convert -density 300x300

I'm an occasional (→non-expert) user of the ImageMagick's marvelous convert program. Today I wanted to convert a nice PDF file to a high-resolution (300dpi, say) PNG file for some editing.

But I was having trouble with it; saying
convert foo.pdf bar.png
resulted in a 72dpi image. What to do? I wondered. Perhaps the help would be useful?

collin@p3:~> convert --help|grep -i resol
  -units type          the units of image resolution
  -resample geometry   change the resolution of an image
collin@p3:~> convert --help|grep -i size
  -page geometry       size and location of an image canvas (setting)
  -pointsize value     font point size
  -size geometry       width and height of image
  -adaptive-resize geometry
                       adaptively resize image using 'mesh' interpolation
  -extent geometry     set the image size
  -geometry geometry   preferred size or location of the image
  -repage geometry     size and location of an image canvas
  -resize geometry     resize the image
collin@p3:~> convert --help|grep -i dots
collin@p3:~> convert --help|grep -i dpi
collin@p3:~> 
None of those looked promising. I didn't want to change the resolution of the image, or resize the image; I just wanted the pre-existing information to be used! Well, when all else fails, Read The Fine Manual.
convert(1)                                                          convert(1)



NAME
       convert  -  convert  between  image formats as well as resize an image,
       blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much
       more.

SYNOPSIS
       convert [input-options] input-file [output-options] output-file

OVERVIEW
       The  convert  program is a member of the ImageMagick(1) suite of tools.
       Use it to convert between image formats as well  as  resize  an  image,
       blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much
       more.

       For more information about the convert command, point your  browser  to
       file:///usr/share/doc/packages/ImageMagick/www/convert.html          or
       http://www.imagemagick.org/script/convert.php.

Ah-HA! The manual is actually online. So I went to the link and looked for "resolution" -- it pointed me at resample, which I knew I didn't want, but I clicked on it anyway, which got me to http://www.imagemagick.org/script/command-line-options.php#resample, where I did a search for "dpi" (dots per inch). That led me to this entry:
-density width
-density widthxheight
Set the horizontal and vertical resolution of an image for rendering to devices.
This option specifies the image resolution to store while encoding a raster image or the canvas resolution while rendering (reading) vector formats such as Postscript, PDF, WMF, and SVG into a raster image. Image resolution provides the unit of measure to apply when rendering to an output device or raster image. The default unit of measure is in dots per inch (DPI). The -units option may be used to select dots per centimeter instead.

The default resolution is 72 dots per inch, which is equivalent to one point per pixel (Macintosh and Postscript standard).
I see that the image is interpreted as 72x72 dpi during conversion; if I resample after that, the higher-resolution info is already gone. But this "density" thing says we'll read the original image at the specified resolution, and then convert to the output format desired. Following are three commands; in the image on the right are the results.
collin@p3:/tmp> convert foo.pdf bar.png
collin@p3:/tmp> convert foo.pdf -resample 300x300 bar3-resampled.png 
collin@p3:/tmp> convert -density 300x300 foo.pdf bar3.png
The first command is the naive command to convert foo.pdf to bar.png; the top image is a screenshot, magnified, of bar.png. You can see that the image is pretty ugly.

The second command is the result of resampling the interpreted-as-72dpi image. The information was lost when foo.pdf was read (i.e., it was read at 72dpi); we then tried to expand the image to 300dpi. The edges are smoothed, so it doesn't look quite as ugly as the first image—if you don't look too close that is. But still, a lot of information was lost.

The last command retains more information from the original, and in the bottom image you can see how much nicer it looks.

openSUSE 11.3: printer won’t print some PDFs

So I have a PDF file (it's from the social security administration's BSO, but really, it could have been from anywhere). I opened it with okular and told it to print pages 1-3, then pages 5-6. I waited briefly, then started wondering what was wrong.

I ran yast2 and tried to see what was up... the test page printed successfully (my PDF print jobs were still in the queue) so I knew the printer was fine.

I'm embarrassed to tell you that I next rebooted the computer. My excuse is that this machine does have some hardware issues, but that's lame. Of course the print jobs didn't print, and lpq still showed them there...

collin@p3:/mnt/home/collin> lpq
hp is ready
Rank    Owner   Job     File(s)                         Total Size
1st     collin  246     SGV147.pdf                      852992 bytes
2nd     collin  247     SGV147.pdf                      800768 bytes
collin@p3:/mnt/home/collin> 
I wondered if something was fishy about the printer itself. After failing to remember the command "lpc", some web searching reminded me of it, but it didn't have much to say, either.
collin@p3:/mnt/home/collin> lpc status
Absolute path to 'lpc' is '/usr/sbin/lpc', so running it may require superuser privileges (eg. root).
collin@p3:/mnt/home/collin> /usr/sbin/lpc status
hp:
 printer is on device 'socket' speed -1
 queuing is enabled
 printing is enabled
 2 entries
 daemon present
simplex:
 queuing is enabled
 printing is enabled
 no entries
 daemon present
collin@p3:/mnt/home/collin>
Finally (D’oh!) it occurred to me to look at the log file. Which logfile? There's no "cups" or "lp" in /var/log/messages but there is /var/log/cups, whence
collin@p3:/var/log/cups> less error_log
…
E [09/Feb/2013:08:59:17 -0800] [Job 246] Job stopped due to filter errors; please consult the error_log file for details.
D [09/Feb/2013:08:59:17 -0800] [Job 246] The following messages were recorded from 08:59:17 to 08:59:17
…
D [09/Feb/2013:08:59:17 -0800] [Job 246] HWMargins = [ 9.000 9.000 9.000 9.000 ]
D [09/Feb/2013:08:59:17 -0800] [Job 246] Error: /undefinedresult in --stringwidth--
D [09/Feb/2013:08:59:17 -0800] [Job 246] Operand stack:
D [09/Feb/2013:08:59:17 -0800] [Job 246] ()   0   ()
D [09/Feb/2013:08:59:17 -0800] [Job 246] Execution stack:
D [09/Feb/2013:08:59:17 -0800] [Job 246] %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1878   1   3   %oparray_pop   1877   1   3   %oparray_pop   1861   1   3   %oparray_pop   1755   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--
D [09/Feb/2013:08:59:17 -0800] [Job 246] Dictionary stack:
D [09/Feb/2013:08:59:17 -0800] [Job 246] --dict:1172/3371(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--   --dict:66/75(L)--   --dict:18/25(L)--   --dict:0/15(L)--   --dict:0/15(L)--   --dict:8/15(L)--
D [09/Feb/2013:08:59:17 -0800] [Job 246] Current allocation mode is local
D [09/Feb/2013:08:59:17 -0800] [Job 246] Last OS error: 2
D [09/Feb/2013:08:59:17 -0800] [Job 246] GPL Ghostscript 8.70: Unrecoverable error, exit code 1
D [09/Feb/2013:08:59:17 -0800] [Job 246] cups_close(0x80ad2dc)
D [09/Feb/2013:08:59:17 -0800] [Job 246] prnt/hpcups/HPCupsFilter.cpp 505: cupsRasterOpen failed, fd = 0
D [09/Feb/2013:08:59:17 -0800] [Job 246] Print file sent, waiting for printer to finish...
D [09/Feb/2013:08:59:17 -0800] [Job 246] Ready to print.
D [09/Feb/2013:08:59:17 -0800] [Job 246] End of messages
D [09/Feb/2013:08:59:17 -0800] [Job 246] printer-state=3(idle)
D [09/Feb/2013:08:59:17 -0800] [Job 246] printer-state-message="Ready to print."
D [09/Feb/2013:08:59:17 -0800] [Job 246] printer-state-reasons=none
Why "Job 246"? Because that was the first print job shown in the "lpq" output above. In case you can't see the messages I marked in boldface above, they were:
  • [Job 246] Job stopped due to filter errors; please consult the error_log file for details.
  • [Job 246] GPL Ghostscript 8.70: Unrecoverable error, exit code 1
That "Execution stack:" message was actually my first clue that it was ghostscript.

So the problem lay with ghostscript, which didn't grok whatever okular was telling it. The upshot was, I printed the file using the mac's Preview program, which knew what to send to the printer (an hp officejet pro L7650, but I don't think it matters).

What if you don't have a consumer operating system and utilities? It turns out that pdf2ps works and produces ghostscript-able postscript, albeit with some ugly-looking messages:

collin@p3:/mnt/home/collin> pdf2ps SVG147.pdf tmp/x.ps

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** Warning: Invalid 0.0 font scale given for Tf ****

   **** This file had errors that were repaired or ignored.
   **** The file was produced by: 
   **** >>>> iText 2.1.5 (by lowagie.com) <<<<
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

collin@p3:/mnt/home/collin>
The resulting file (tmp/x.ps in this case) does print; I think it became a sort of bitmapped file.
collin@p3:/mnt/home/collin> ls -o SVG147.pdf tmp/x.ps
-rw-r--r-- 1 collin  309812 2013-02-09 08:54 SVG147.pdf
-rw-r--r-- 1 collin 2014259 2013-02-09 09:46 tmp/x.ps
collin@p3:/mnt/home/collin> 

Tuesday, February 05, 2013

“The reason why we do things so badly…

…is that we are not content to do what we can.”
Merton, No Man Is an Island 7.5 (p. 124)
Merton continues:
We insist on doing what is not asked of us, because we want to taste the success that belongs to somebody else.

We never discover what it is like to make a success of our own work, because we do not want to undertake any work that is merely proportionate to our powers.

Who is willing to be satisfiedd with a job that expresses all his limitations?

Merton, loc. cit.
Or as Paul wrote: “Do not think of yourself more highly than you ought, but rather think of yourself with sober judgment, in accordance with the measure of faith God has given you” (Romans 12:3). And: “If anyone thinks he is something when he is nothing, he deceives himself. Each one should test his own actions. Then he can take pride in himself, without comparing himself to somebody else” (Galatians 6:3f).

Good advice.

Saturday, January 26, 2013

Vegan apricot pie!

It only occurred to me after the pie was half gone: there were no animal-based ingredients in this pie.

Crust (source: http://allrecipes.com/recipe/apricot-pie/)
I used to use the Joy of Cooking recipe, which used 2c flour and 2/3c shortening split in halves. You'd cut half the shortening into the flour until it was the size of corn meal, then cut in the other half until "pea size." This often seemed to produce not enough pastry for two crusts, besides involving more steps. So this one is my new favorite pie-crust recipe, even though it makes a little too much pastry.

Preheat oven to 220°C/425°F.
Sift together:
  • 2½ cup flour
  • scant 1 tsp salt
Using a pastry cutter or two knives, cut in:
  • 1 cup vegetable-based shortening
until it looks like sand. Large sand.
Take
  • 6 Tbsp cold water
and sprinkle it into the flour mixtrure, a little at a time, while lifting ingredients with a fork. Stop when the dough ball just sticks together. Roll half the dough-ball into a pie pan and put the other aside.

Filling (source: http://www.tasteofhome.com/Recipes/Dutch-Apricot-Pie/Print)
In a mixing bowl, combine
  • 1 cup sugar
  • 2 Tbsp quick-cooking tapioca
Add
  • 4 cups (1 pound) fresh or frozen sliced apricots
  • 1 Tbsp lemon juice
and toss to coat. Let stand 15 minutes.
Turn fruit mixture into pie pan.
Roll out the other half of the pastry to form a top crust.
Cover the pie with top crust, pierce top crust with fork (or cut slots); flute edges.
Bake 35-45 minutes, until crust is browned and filling is bubbling.
Cool on a rack.

Praying for Indifference

I've been reading Ruth Haley Barton's marvelous Pursuing God’s Will Together, whose 2nd chapter mentions three kinds of prayer I want to remember before trying to make decisions. In other words, I want to be praying these all the time.

The first, Barton says, is the prayer of quiet trust (Psalm 131). This brings to mind the verse in Psalm 23 that says "he makes me lie down in green pastures." In A Shepherd Looks at Psalm 23, Keller says that sheep won't lie down if they're hungry, fearful, or uncomfortable. The point, then, is to quiet myself, to ask God to bring me to that place of quiet trust, like a contented child.

The second is the prayer of indifference, the attitude of Mary when the angel tells her what's about to happen to her (Luke 1:38 "Behold the handmaid of the Lord; be it unto me according to thy word.") or Jesus in the garden, when he says, Yet not my will but thine be done.

In My Utmost for His Highest, Chambers says, "To be certain of God means that we are uncertain in all our ways, we do not know what a day may bring forth." Which is a frightening concept, but doesn't the Bible already tell us that (Proverbs 27)? And Jesus told us about this too: "If anyone wants to do God's will, he will know whether I'm speaking from God, or just making all this stuff up" (John 7:17).

I need to be in a place where God's will is what I want—the place of surrender. This is really hard, because I want what I want, and I think it's a good idea. If I thought it a bad idea, I wouldn't want it.

But I need to lose the attitude of "Be reasonable; do it my way!" and instead pray sincerely, "Thy kingdom come, thy will be done on earth, and in my life, and in particular regarding this decision." This is something I can't do on my own; I need God's help in getting to that place.

The third prayer mentioned is the prayer for wisdom (James 1:5, etc.); we sometimes go straight to this third kind of prayer, without first getting peaceful or surrendered. When that happens, I may not be trusting God and I may experience unrest, because I don't really want God's will; what I want is for God to give a thumbs-up to my will.

Sorry to say, I have a great deal of experience in this area. I mean of asking for wisdom without first trusting God and surrendering to him.

If I'm considering alternatives, or arguing for why my chosen alternative is better, there's a temptation to have a perfunctory prayer and start writing down advantages (of my way) and disadvantages (of yours). And therein lies the problem: when I want my will, I can come up with all kinds of "reasons" why my chosen alternative is "better" than the way you prefer. I can be very convincing and sincere about it too—I've convinced myself that my way is better, and I'd never lie to you about something unless I'd lied to myself about it first.

Of course I can't tell I'm doing this at the time. But I have seen very smart people come up with really lame reasons why thus and such is a good idea, when it's clearly not.

Quiet trust first, then surrender, then asking for wisdom. May the Lord remind me about this sequence when I get the order wrong.

Thursday, January 24, 2013

The "Self"?

I've been thinking a lot about the "self" lately. Not myself in particular, but about what the concept means.

At Christmas, the lovely Carol gave me a copy of Hood's The Self Illusion, which doesn't quite prove what the blurb says it does.

But what I want to write about today is something I heard on NPR, maybe on Talk of the Nation: it was an interview with Daniel Levitin, who wrote this fascinating article about what remains when memories of the past are gone.

In response to something I've already forgotten, Levitin said that what we call the "self" has four, um, aspects. Or something (I don't think he said "components"). Parts, maybe? Anyway, here they are, to the extent that I remembered them:

  1. Self-awareness.
    For example, you can recognize yourself in the mirror
  2. Agency
    You recognize that you're mostly in control of your body and can decide to do things with it.
  3. Tastes, preferences
  4. Our stories.
    Our history, the struggles we endured, the obstacles overcome, this sort of thing. This is what's lost when long-term memory is lost.
If we lose our stories, Levitin said, we lose a big part of ourselves. Indeed we do! But that's not the whole story. Levitin concludes his article with this observation about "Tom" and his brain tumor:
When I saw Tom, something fundamentally Tom was still there. Some of us call it personality, or essence. Some call it the "soul." Whatever it is, the tumor that took Tom's memory had not touched it.
Amnesia and the Self That Remains When Memory Is Lost
from theatlantic.com, December 2012
Isn't it interesting that Levitin, a neuroscientist, talks about personality or essence or soul (as does Thompson in Anatomy of the Soul)—whereas people like Hood and Pinker say the self or the soul (what Pinker disparagingly calls the ghost in the machine) is an illusion?

Though some people say there's no such thing as a "self," nobody can actually live as though they're just an illusion.

I'm not sure where I was going with all that, but I wanted to share Levitin's list of four aspects of the "self" with you. And I also wanted to note that God tells his people over and over to remember—something important in many relationships. More on that in a future post.

Tuesday, January 01, 2013

New Year's Eve Party

One of my great pleasures is that of preparing food for people I love and enjoying it with them. There were 21 of us enjoying appetizers and dinner and dessert last night: neighbors from down the street, parents of one of Sheri's classmates, friends from decades ago, our small group, another family we know from church.

There was sushi from Suruki Supermarket—51 pieces (including six rolls) for $48.25, an incredible deal and (to my unsophisticated palate) delicious.

But that's not mainly what I mean by "prepare"—and neither is the antipasto pack from Costco®. And neither is cooking too much rice in the rice cooker. Let me start with

Shoyu chicken, sorta

… a variation on this recipe.
About 6½ hours before suppertime, mix
  • 1 cup brown sugar
  • 1 cup shoyu (soy sauce)
  • 1½ cup water
  • 2 tsp or so minced ginger
    (or fresh if you prefer; we have minced garlic in a jar)
  • a similar amount of garlic (not sure if I did this or just thought about it)
  • 2/3 cup vinegar (omitted from the food.com recipe)
and pour into crockpot or stock pot set over medium heat.
Optional: lightly brown in oil; alternately, just drop into the pot from the packages:
  • 15-18 chicken drumsticks and
  • a similar amount of chicken thigh meat
If you brown the chicken, you'll probably need to do it in shifts. In any case, add chicken to the stock pot. Reduce heat.
Slice thinly:
  • ½–1 bunch thinly sliced green onions
and add to the stockpot.
Simmer 5-6 hours.

Korean-style grilled beef (not really 불고기)

So I went to Whole Foods to get meat from a humanely raised cow or bull. I saw a boneless cross rib roast, about 5 pounds, and asked them to slice it for me. "We can cut it by hand, but we don't have a meat slicer," the butcher said. Well, it's a first-world problem. I don't have a machine for slicing meat, either, but I do have sharp knives. I cut it thinly, stacking the pieces in a 4-quart Pyrex® mixing bowl. Since this cow ate mostly grass, I figured it would need a little tenderizing, so I poured a little cooking sherry over the stack and a little between. And a little soy sauce. OK, now let me try to recreate the "recipe" such as it was:
Mix:
  • ~½ cup soy sauce
  • ~4 Tbsp. sesame oil
  • a similar amount of vegetable oil (if I remembered)
  • garlic? (6 cloves would not be excessive, but I think I forgot the garlic altogether)
  • 1 Tbsp. toasted crushed sesame seeds (you could certainly use more)
  • ~½ bunch green onions, sliced/chopped/whatever
Layer
  • ~5 lbs. thinly sliced beef
    (How thin? I had about 40 pieces, not all of which were perfect slices.)
in a marinating dish like this one, with at least a little of the marinade between each piece.
Let soak an hour or more if you can, turning occasionally.
Grill over charcoal.

Spinach nah-mool (sorta)

I keep qualifying these names because they're like movies whose plots were "inspired by a true story." Here's what we did for the spinach.
Boil very briefly, then cool:
  • 2 lbs. fresh spinach
Marinade
  • ~½ cup soy sauce
  • ~¼ cup vegetable oil (if I remembered)
  • ~4 Tbsp. sesame oil
  • (I probably forgot the garlic again.)
  • 1 Tbsp. toasted crushed sesame seeds (you could certainly use more)
  • ~½ bunch green onions, sliced/chopped/whatever
Mix all marinade ingredients.
At least an hour before suppertime, but probably not the day before, add about half of it to the spinach, and mix well. Let it sit in refrigerator for 30-60 minutes, and taste it. If it's bland, add a little more of the marinade.
Serve with steamed rice (brown rice if you're health-conscious) and kimchi/kim chee.

Cho jang

I didn't serve this last night, but I wanted to write down what I remembered from last time.
  • 1 Tbsp. go chu jang (hot pepper paste)
  • 1 Tbsp. white sugar
  • 1 Tbsp. white vinegar
Whisk all ingredients together. And that's all there is to it.