Jump to content
  • Announcements

    • Allan

      Forum Rules   04/13/2018

      Hello folks! As some things are being fixed, we'll keep you updated. Per hour the Forum Rules don't have a dedicated "Tab", so here is the place that we have our Rules back. New Users Lounge > [READ] - InsanelyMac Forum Rules - The InsanelyMac Staff Team. 
michaelfisk

Combine Text File and Image

3 posts in this topic

Recommended Posts

I am needing a script that will take a text file that has an image URL and 2 lines of text and create one image out of the URL image with the 2 lines of text underneath it all in one image.

 

So the text file would contain:

 

http://www.url.com/to/image

Text Line 1

Text Line 2

 

-----

 

I need the final image to look like the file I have attached.

post-1029734-0-17816400-1340409937_thumb.png

Share this post


Link to post
Share on other sites

You could do this using Python, but first you'll need to install the pil module.

 

Below is a bash script to install the module and libs that you need (you'll need Xcode/Xcode Command Line Tools installed):

 

#!/bin/bash
JPEG_LION="http://rudix.googlecode.com/files/libjpeg-8d-0.pkg"
JPEG_SNOW="http://rudix-snowleopard.googlecode.com/files/libjpeg-8d-0.pkg"

echo "Select MacOS Version:"
echo "[1] Lion"
echo "[2] Snow Leopard"
read selection

case $selection in
1) OPTION=$JPEG_LION;;
2) OPTION=$JPEG_SNOW;;
*) Echo "No Selection Made"
exit 0
;;
esac
curl -O $OPTION

sudo installer -pkg libjpeg-8d-0.pkg -target /
sudo easy_install pip
sudo pip install pil
sudo pip install pillow

 

Then you could try something like this in Python:

 

#!/usr/bin/env python
import os,sys,urllib2,Image,ImageDraw,ImageFont
fname = raw_input("Enter Filename: ")
txt = open(fname).readlines()
asset,text1,text2,blank = txt
savedFilename = os.path.basename(asset).strip()
imgfile = urllib2.urlopen(asset)
savedfile = open(savedFilename,'wb')
savedfile.write(imgfile.read())
savedfile.close()

im = Image.open(savedFilename)
font = ImageFont.truetype('/Library/Fonts/Verdana Bold.ttf',14)
box = im.getbbox()
im2=im.crop((box[0], box[1], box[2], box[3]+50))
draw=ImageDraw.Draw(im2)
draw.text((box[1]+10,box[3]+10), text1.strip(),font=font)
draw.text((box[1]+10,box[3]+30), text2.strip(), font=font)
im2.save('output.jpg','JPEG')

 

The Python script reads a single text file that has the 3 entries you listed, terminated with a blank line.

 

HTH,

 

AIR.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

×