Jump to content

Combine Text File and Image


  • Please log in to reply
2 replies to this topic

#1
michaelfisk

michaelfisk

    InsanelyMac Protégé

  • Members
  • Pip
  • 2 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.

Attached Files



#2
TH3L4UGH1NGM4N

TH3L4UGH1NGM4N

    (~_~)

  • News Team
  • 1,156 posts
  • Gender:Male
  • Location:Wonderland
  • Interests:(~_^)
eh script or program?

#3
Airr

Airr

    InsanelyMac Protégé

  • Members
  • Pip
  • 17 posts
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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

© 2014 InsanelyMac  |   News  |   Forum  |   Downloads  |   OSx86 Wiki  |   Mac Netbook  |   PHP hosting by CatN  |   Designed by Ed Gain  |   Logo by irfan  |   Privacy Policy