This simple app shows how a Python developer can get started using GitHub Codespaces and Copilot. This demo will:
- Extract text from an image
- Translate from Italian to English
- Juxtapose the image and text
A few things to notice about the app:
.devcontainer.json
defines a custom devcontainer with:- Language dependencies (Python 3, Node LTS)
- GitHub Copilot extension pre-installed
postcreatecommand
to write credentials from the$INFERNO
secret to a temporary file
dockerfile
installs- Package dependencies (pytesseract, google-cloud-translate)
- Environment variable for the Google Cloud service account
dante.png
shows the first page of Dante's Inferno in the original Italiandemo.py
shows a completed version of the demo for reference, but you'll create your own file, inferno.py
Before running this app, you'll need to:
- Create a Google Cloud service key in JSON format.
- Visit Settings > Codespaces > Secrets > Create Secret to create an
$INFERNO
secret with your Google Cloud service key.
- Fork this repository into a GitHub Team or Enterprise account (required for Codespaces)
- Create a new Codespace by clicking Code > Create Codespace
- Once the code editor is open, create a new file in the root directory called
inferno.py
- In the editor, type the following code:
# import the image libraries
from PIL import Image, ImageDraw
# convert image to text
import pytesseract
# Google Cloud Translate package
from google.cloud import translate_v2
-
For the next several steps, try typing only the function name and see what happens. Try adding comments to see how it changes the suggestions. GitHub Copilot should be able to write the application for you with very little effort.
-
Use pytesseract to read text from the original image
def get_text(image) :
""" convert the image to text in Italian """
- Use Google Cloud Translate to translate the text to English
def translate_text(text) :
""" translate italian to english """
- Juxtapose the original image and translated text side-by-side
def juxtapose(image,text) :
"""create a new image with the original image on the left and the text on the right
Make the new image twice the width of the original, but the same height.
Display the text in white on a black background"""
- Bring it all together
def process(image) :
""" process the image """
- Try it out on a test image
# try it out on dante.png
image = Image.open('dante.png')
new_image = process(image)
# save the image
new_image.save('dante_translated.png')
- Run your app in the terminal by typing
python inferno.py
- Look for
dante_translated.png
in the root directory
Credit: @oegerikus for the original idea, @dynamicwebpaige for pairing with me