changzhi
changzhi

Reputation: 2709

Openstack Permission denied when I generate a picture by python

I use a program which can generate a picture. I saved it by

img.save("/usr/lib/python2.6/site-packages/openstackdashboard/static/dashboard/img/validate.jpeg")
return strs # strs is picture's data

Everything goes right when run it alone . But " IOError " occured when I call it by

from .auth_code import Create_Validate_Code
auth_code_str = Create_Validate_Code()

And horizon says " [Errno 13] Permission denied: '/usr/lib/python2.6/site-packages/openstack-dashboard/static/dashboard/img/validate.jpeg' ". Could someone help me ? Thanks a lot .

This is all code to create a picture

#!/usr/bin/env python
import random
import Image, ImageDraw, ImageFont, ImageFilter

_letter_cases = "1234567890" 
_upper_cases = _letter_cases.upper() 
_numbers = ''.join(map(str, range(3, 10))) 
init_chars = ''.join((_letter_cases, _upper_cases, _numbers))
fontType="/usr/share/fonts/lohit-tamil/Lohit-Tamil.ttf"

def create_lines(draw,n_line,width,height):
  line_num = random.randint(n_line[0],n_line[1]) 
  for i in range(line_num):
    begin = (random.randint(0, width), random.randint(0, height))
    end = (random.randint(0, width), random.randint(0, height))
    draw.line([begin, end], fill=(0, 0, 0))

def create_points(draw,point_chance,width,height):
  chance = min(100, max(0, int(point_chance))) 

  for w in xrange(width):
    for h in xrange(height):
      tmp = random.randint(0, 100)
      if tmp > 100 - chance:
        draw.point((w, h), fill=(0, 0, 0))

def create_strs(draw,chars,length,font_type, font_size,width,height,fg_color):
  c_chars = random.sample(chars, length)
  strs = ' %s ' % ' '.join(c_chars) 

  font = ImageFont.truetype(font_type, font_size)
  font_width, font_height = font.getsize(strs)

  draw.text(((width - font_width) / 3, (height - font_height) / 3),strs, font=font, fill=fg_color)

  return ''.join(c_chars)



def Create_Validate_Code(size=(120, 30),
                             chars=init_chars,
                             img_type="GIF",
                             mode="RGB",
                             bg_color=(255, 255, 255),
                             fg_color=(0, 0, 255),
                             font_size=18,
                             font_type=fontType,
                             length=4,
                             draw_lines=True,
                             n_line=(1, 2),
                             draw_points=True,
                             point_chance = 2):

  width, height = size 
  img = Image.new(mode, size, bg_color) 
  draw = ImageDraw.Draw(img) 
  if draw_lines:
    create_lines(draw,n_line,width,height)
  if draw_points:
    create_points(draw,point_chance,width,height)
  strs = create_strs(draw,chars,length,font_type, font_size,width,height,fg_color)

  params = [1 - float(random.randint(1, 2)) / 100,
            0,
            0,
            0,
            1 - float(random.randint(1, 10)) / 100,
            float(random.randint(1, 2)) / 500,
            0.001,
            float(random.randint(1, 2)) / 500
            ]
  img = img.transform(size, Image.PERSPECTIVE, params) 

  img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) 

  img.save("/usr/lib/python2.6/site-packages/openstack-dashboard/static/dashboard/img/validate.jpeg")

  return strs

Upvotes: 0

Views: 380

Answers (3)

abarnert
abarnert

Reputation: 365707

The code to create and save the file is inside the function Create_Validate_Code. In your initial version, you never call that function anywhere. Therefore, it never tries to create and save the file, so it never fails.

When you add this:

from .auth_code import Create_Validate_Code
auth_code_str = Create_Validate_Code()

… now you're calling the function. So now it fails. It has nothing whatsoever to do with the third-party module you're using; you could do the same thing with just this:

Create_Validate_Code()

Meanwhile, the reason that creating the file fails is that you don't have write access to directories in the middle of your system's site-packages. This is by design. This is why operating systems have permissions in the first place—to stop some buggy or malicious code run as a normal user from screwing up programs and data needed by the entire system.

Create the file somewhere you do have access to, like some place in your home directory, or the temporary directory, or whatever's appropriate to whatever you're trying to do, and the problem will go away.

Upvotes: 2

Max Noel
Max Noel

Reputation: 8910

You shouldn't save data deep within your Python installation. It's really bad practice, which is why the OS is preventing you from doing it. Save the picture somewhere in your home folder.

Upvotes: 0

Elias Benevedes
Elias Benevedes

Reputation: 391

Have you tried running the final app as Administrator/root? That usually fixes any "Permission denied" errors while programming.

Upvotes: 0

Related Questions