Replacing Functionality of PIL (ImageDraw) in Google App Engine (GAE) - python

So, Google App Engine doesn't look like it's going to include the Python Imaging Library anytime soon. There is an images api, but it's paltry, and inadequate for what I need.
I'm wondering what Python only (no C-extensions) there are that can replace the Image.paste and the ImageDraw modules. I don't want to write them myself, but that is an option. I'm also open to other solutions, such as "do the processing somewhere else, then call via api", if they're not too ugly. (For the record, the solution I just suggested seems pretty ugly to me.)
How have others gotten around this?
(I'm not wedded to GAE, just exploring, and this looks like a deal breaker for my app.)
Notes:
For me, crop, resize is not enough. In particular I need
paste (replace part of an image with another.... can be faked with "compose")
draw (for drawing gridlines, etc. Can be faked as well)
text (write text on an image, much harder to fake, unless someone wants to correct me)

My skimpygimpy.sourceforge.net will do drawing and text, but it won't edit existing images (but it could be modified for that, of course, if you want to dive in). It is pure python. see it working on google apps, for example at
http://piopio.appspot.com/W1200_1400.stdMiddleware#Header51,
That's an experimental site that I'll be messing with. The link may not work forever.

Your assumption is wrong. If you use the Python 2.7 runtime, you can use PIL (version 1.1.7) as documented here: https://developers.google.com/appengine/docs/python/tools/libraries27.
This article also explains how to enable PIL for your app.
BTW, the last comment in the bug you referenced also mentions it.

I don't know if it has all features you want, but I have been messing with PNGCanvas, and it does some things I have done before with PIL

Now according to this ticket "On the Python 2.7 runtime, you can import PIL and use it directly. It's the real PIL, not a wrapper around the images API."

Related

How to save an image to Gtk Clipboard without creating a window?

I am trying to take a pixbuf image and drop it into the user's clipboard for pasting into any program that accepts images. If I don't run Gtk.main(), nothing happens. If I do run Gtk.main(), it works, but the program never exits (I don't want to open a window).
# pixbuf is a pixbuf image
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_image(pixbuf)
clipboard.store()
Gtk.main()
I have bad news, and I suggest you use something other than gtk to set the clipboard. I recommend trying something like wl-clipboard, xsel, or xclip. I am sure you know how to chose one or two of these and call it from a python script.
If you are looking for a gtk-only solution, I have spent quite some time on this and given up though I am not yet an expert in any of this.
The traditional solution is documented here:
https://github.com/bstpierre/gtk-examples/blob/master/c/clipboard_simple.c.
For reasons beyond my knowledge it doesn't currently work, but the comments at the bottom would be important in creating a gtk-only solution. The problem could be for a number of reasons including a bug in gtk or glibc. Or it might be part of a poorly explained migration to wayland.
Another potential solution is documented here: https://docs.gtk.org/gtk3/func.events_pending.html. This also doesn't work for unknown reasons.
The next avenues for testing are to test historical versions of linux distributions, and once the library version is found try to pinpoint the exact commit to the exact library and make a change. I made a decent effort to debug gtk, but it is not yet my area of expertise.
Even if the the change could be maid to facilitate one of the above solutions, it would be to much to ask for a version that works on wayland. Although xwayland may offer residual support and wl-clipboard offers a hack, any clipboard read or write requires a window and for your window to be focused as mentioned here: https://gitlab.gnome.org/GNOME/gtk/-/issues/1874. There seams to be some exeption for non-gnome wayland implied in this discussion: https://github.com/bugaevc/wl-clipboard/issues/90. Although I never found the offending part of the wayland standard, I expect that it is present.

Templatizing images using gimp

My wife recently started a business making soap bars and the soap labels have quickly spiraled out of control into tons of diverging Gimp .xcf files. The only difference between the files are the names of the product, description, and ingredients. I'd like to make a template and produce the labels from a .xcf file and maybe a .xml or .ini file that has the text data. I've been eyeing python-fu, but I'm not quite sure if it does what I'd like. I'd like to stick to python where possible because it's simple.
Are there facilities in python + gimp to do what I'd like? Could something similar be done in anything other than gimp?
Edit: Additionally, I have to make pages of stickers to print, so some means of optimizing the number of stickers that can fit on a page (2d bin pack?) would be a big plus.
The Python Imaging Library (PIL) can quite easily read in an image, put some text on it, and write it back out. Use the Image module to read and write, and the ImageDraw module to add the text.
I doubt that it can use the .xcf format though, you'll probably want to convert to .png.
You can script GIMP in Python, and pretty much everything you can do o n the prgoram can be done via the API -- you can check for the available API functions in help->procedure browser.
To enable Python scripting in gimp 2.6 under Windows, you have to google for it -- Python, python gtk and one other package have to be installed before GIMP.
Python's PIL is fine for simple images, but it is weak in arttistic pixel manipulation, which is available with GIMP.

Using Imagemagick without making files?

I'm working in Python to create images from text. I've already been back and forth with PIL and frankly, its font and alignment options need a lot of work.
I can subprocess Imagemagick and it works great, except that it seems to always need to write a file to disk. I would like to subprocess the image creation and just get the data returned to Python, keeping everything in memory.
I've looked into a number of supposed Python wrappers for ImageMagick, but they're all hopelessly years out of date or not documented whatsoever. Even searching extensively on SO doesn't see to clearly point to a defacto way to use ImageMagic with Python. So I think going for subprocessing is the best way forward.
convert and the other ImageMagick commands can output image data to stdout if you specify format:- as the output file. You can capture that output in Python using the subprocess module.
For instance:
cmd = ["convert", "test.bmp", "jpg:-"]
output_stream = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
It would be a lot more work than piping data to ImageMagick, but there are several Pango based solutions. I used pango and pygtk awhile back, and I am pretty sure you could develop a headless gtk or gdk application to render text to a pixbuf.
A simpler solution might be to use the python cairo bondings.
Pango works at a pretty low level, so simple stuff can be a lot more complicated, but rendering quality is hard to beat, and it gives you a lot of fine grained control over the layout.

How to display a PostScript file in a Python GUI application

I would like to build a cross-platform GUI application in Python that displays PostScript files I generate, among some other stuff. What is the best way to accomplish this? Ideally I would be able to do things like zoom and pan the displayed graphic.
Do any/some/all of the GUI toolkits have something I can drop in to do this, and if so what are they called and how do they work? If necessary, I can convert the postscript file to PDF or a raster format behind the scenes, but I'd rather not do the latter.
I asked pretty much the same question a little time ago. Here it is. Hope it helps.
Note: Poppler is highly undocumented. If you use Gtk for your GUI there are a few working examples. In Qt things are a little harder, and I haven't figured out a way myself yet.
In a word Ghostscript. It's been a while since I used it, but it's cross-platform and you can use it to generate image files which your app could then display, pan, and zoom. I used it to develop and test some pretty involved commercial Postscript code for my own products and under contract for others. It's open source, which came in handy for a couple of use cases I had. Nowdays I believe it does PDF, too.

Editing Photoshop PSD text layers programmatically

I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:
load the PSD
edit the text in said layer
flatten all layers in the image
save as a web-friendly format like PNG or JPG
I immediately thought of ImageMagick, but I don't think I can edit the text layer through IM. If I can accomplish the first two steps some other programmatic way, I can always use ImageMagick to perform the last two steps.
After a couple of hours of googling and searching CPAN and PyPI, I still have found nothing promising. Does anyone have advice or ideas on the subject?
If you don't like to use the officially supported AppleScript, JavaScript, or VBScript, then there is also the possibility to do it in Python. This is explained in the article Photoshop scripting with Python, which relies on Photoshop's COM interface.
I have not tried it, so in case it does not work for you:
If your text is preserved after conversion to SVG then you can simply replace it by whatever tool you like. Afterwards, convert it to PNG (eg. by inkscape --export-png=...).
The only way I can think of to automate the changing of text inside of a PSD would be to use a regex based substitution.
Create a very simple picture in Photoshop, perhaps a white background and a text layer, with the text being a known length.
Search the file for your text, and with a hex editor, search nearby for the length of the text (which may or may not be part of the file format).
Try changing the text, first to a string of the same length, then to something shorter/longer.
Open in Photoshop after each change to see if the file is corrupt.
This method, if viable, will only work if the layer in question contains a known string, which can be substituted for your other value. Note that I have no idea whether this will work, as I don't have Photoshop on this computer to try this method out. Perhaps you can make it work?
As for converting to png, I am at a loss. If the replacing script is in Python, you may be able to do it with the Python Imaging Library (PIL, which seems to support it), but otherwise you may just have to open Photoshop to do the conversion. Which means that it probably wouldn't be worth it to change the text pragmatically in the first place.
Have you considered opening and editing the image in The GIMP? It has very good PSD support, and can be scripted in several languages.
Which one you use depends in part on your platform, the Perl interface didn't work on Windows the last I knew. I believe Scheme is supported in all ports.
You can use Photoshop itself to do this with OLE. You will need to install Photoshop, of course. Win32::OLE in Perl or similar module in Python. See http://www.adobe.com/devnet/photoshop/pdfs/PhotoshopScriptingGuide.pdf
If you're going to automate Photoshop, you pretty much have to use Photoshop's own scripting systems. I don't think there's a way around that.
Looking at the problem a different way, can you export from Photoshop to some other format which supports layers, like PNG, which is editable by ImageMagick?
You can also try this using Node.js. I made a PSD command-line tool
One-line command install (needs NodeJS/NPM installed)
npm install -g psd-cli
You can then use it by typing in your terminal
psd myfile.psd -t
You can check out the code to use it from another node script or use it through your shell is from another Bash/Perl/whatever script.

Categories

Resources