Import many images at once

Is it possible to import multiple images at once?

Let’s say i have 100 images with a unicode value as a name and want to import each image into its corresponding glyph. Is an automated method possible?

Multiple images will be imported into glyphs with the same name.

1 Like

Yes, i noticed that, but perhaps i could write a script to do that for me?
I hoped i could get a hint where to start…

Take a peek into my Images scripts in my GitHub rep.

1 Like

I was trying to figure out what’s going on in your scripts but it’s a little overwhelming for now.

I try to learn the basics but i’m stuck at chosing a specific layer.

for layer in Glyphs.font.glyphs['f'].layers:
    layer.backgroundImage = GSBackgroundImage('/Users/dyb/Desktop/file.png')

This inserts an image but in all of the layers.
Should i somehow put this in another loop or what’s the technique to chose a specific layer?

My general idea for this script is:

  1. Create a list of values (unicode code points) of all the glyphs in the current font.
  2. Pass that list through a loop which will pick a value from the list and then:
    2a. access a glyph with a code point from the list
    2b. access a chosen layer 2c. insert an image from a calculated path
  3. Pick another unicode value from the list and go back to step 2.

I’m not sure if i can access a glyph by it’s code point but the rest seems fairly simple to me except the layers…

Am i going in the right direction?

Why do you think you need to first generate the list of unicode points? Why not go through all glyphs and check if they meet your criteria. That would look like this:

  1. loop through all glyph
  2. check if you need to deal with that glyph
  3. access the layer
  4. insert the image from a calculated path

I thought i need the list to calculate the path…
So having the list i thought i’d use it to access glyphs from the list.

I generate my glyphs from a list that i generate from my database from which i also export my images and name them. Thus the list = all the glyphs in a font in a layer = files in a folder.

Well… It looks like sometimes you just need to sleep it through and then it somehow gets to you…

picklayer = 'Light Italic'
pickpath = '/Users/dyb/Desktop/'
pickextension = 'png'
x = []
for glyph in Glyphs.font.glyphs:
    x.append(glyph.unicode)
for x in x:
    for glyph in Glyphs.font.glyphs:
        if glyph.unicode == x:
            for layer in glyph.layers:
                if layer.name == picklayer:
                    layer.backgroundImage = GSBackgroundImage(pickpath + x + '.' + pickextension)
                    print glyph.unicode
                    print layer.name

This works great although a little setup is necessary.
If anyone has an idea how to expand that with a pop-up for chosing the folder or see some other improvements i humbly ask: please share your knowlegde!
It’s my first day with Python and Scripting for Glyphs :wink:

picklayer = 'Light Italic'
pickpath = '/Users/dyb/Desktop/'
pickextension = 'png'
masterID = None
for master in Glyphs.font.masters:
    if master.name == picklayer:
        masterID = master.id
for glyph in Glyphs.font.glyphs:
    if glyph.unicode:
        print glyph.unicode
        layer = glyph.layers[masterID]
        layer.backgroundImage = GSBackgroundImage(pickpath + glyph.unicode + '.' + pickextension)

or you could get the current master:

masterID = Glyphs.font.masters[Glyphs.font.masterIndex].id

that would look like this:

pickpath = '/Users/dyb/Desktop/'
pickextension = 'png'
masterID = Glyphs.font.masters[Glyphs.font.masterIndex].id
for glyph in Glyphs.font.glyphs:
    if glyph.unicode:
        print glyph.unicode
        layer = glyph.layers[masterID]
        layer.backgroundImage = GSBackgroundImage(pickpath + glyph.unicode + '.' + pickextension)
1 Like

To pick a path:

pickpath = GetFolder("Please choose a folder with images")

Thank you for the master modification!
Will the GetFolder run from Macro Panel?

I tried all that only in the macro panel.

Doesn’t work for me in the Macro Panel. Although i saved it as a file and it works well in the script menu.

Implemented the currently selected Master and GetFolder:

import GlyphsApp

pickpath = GetFolder("Please choose a folder with images") + '/'
pickextension = 'png'
masterID = Glyphs.font.masters[Glyphs.font.masterIndex].id
for glyph in Glyphs.font.glyphs:
    if glyph.unicode:
        print glyph.unicode
        layer = glyph.layers[masterID]
        layer.backgroundImage = GSBackgroundImage(pickpath + glyph.unicode + '.' + pickextension)

What version of Glyphs do you have?

It was 2.1.1 that wasn’t working. I updated to the latest version and the script now works in Macro Panel.

Sorry for joining the conversation late, but you can select multiple glyphs and then import multiple images (assuming image names correspond to glyph names). Exactly why do you need a script for it?

Personally I can think of one reason, that OSX isn’t case sensitive and you need to store lowercase and uppercase images in separate folders, so a script that imports all images under a folder could be useful/

What I do is keep lowercase and uppercase in separate subfolders. Or, use .png for the one, and .jpeg for the other. :wink:

Thank you for joining in Tosche.

Maybe i’m missing something and i don’t really need a script. I’m not sure. My setup looks like this:

I have a database where i collect images of glyphs and export them from there. I already have a naming convention for those and would like to keep it. I also export a list in a format “uni####,” and create all the glyphs i need. Importing multiple images without the script only worked on glyphs that were already created – it doesn’t add new glyphs.

The script in it’s current state doesn’t add new glyphs automatically yet but i’d really like to do that by fetching a list from files in a folder, creating the glyphs that are needed and then import the images. I understand it won’t solve every problem and you need to name your images in a certain way but the first script already improved my workflow.

I am not very original and have put this script for future reference on github :wink:


A humble start for me and actually most of the work on that was done by Georg but i hope i get better at this!

@dyb So, 1. file names are in unicode, 2. glyphs need to be added if absent. Neither of these can be done by default, and do need a script. Thanks for the explanation.

@mekkablue The problem is that it doesn’t accept a folder as an input (thus it doesn’t read subfolders either). Changing file format makes sense too, but I want to avoid it. I think I’ll make a script that’s based on Marcin’s but interprets file names as glyph names too.

1 Like

@Tosche I think i made it although probably not in the most elegant way…
Still a lot to learn. I’d be glad if you take a look at my script!

1 Like