@staticmethod<EOL><INDENT>def _pack_image(filename, max_size, form_field="<STR_LIT:image>", f=None):<DEDENT>
<EOL>if f is None:<EOL><INDENT>try:<EOL><INDENT>if os.path.getsize(filename) > (max_size * <NUM_LIT>):<EOL><INDENT>raise TweepError('<STR_LIT>' % max_size)<EOL><DEDENT><DEDENT>except os.error as e:<EOL><INDENT>raise TweepError('<STR_LIT>' % e.strerror)<EOL><DEDENT>fp = open(filename, '<STR_LIT:rb>')<EOL><DEDENT>else:<EOL><INDENT>f.seek(<NUM_LIT:0>, <NUM_LIT:2>)  <EOL>if f.tell() > (max_size * <NUM_LIT>):<EOL><INDENT>raise TweepError('<STR_LIT>' % max_size)<EOL><DEDENT>f.seek(<NUM_LIT:0>)  <EOL>fp = f<EOL><DEDENT>file_type = mimetypes.guess_type(filename)<EOL>if file_type is None:<EOL><INDENT>raise TweepError('<STR_LIT>')<EOL><DEDENT>file_type = file_type[<NUM_LIT:0>]<EOL>if file_type not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise TweepError('<STR_LIT>' % file_type)<EOL><DEDENT>if isinstance(filename, six.text_type):<EOL><INDENT>filename = filename.encode("<STR_LIT:utf-8>")<EOL><DEDENT>BOUNDARY = b'<STR_LIT>'<EOL>body = []<EOL>body.append(b'<STR_LIT>' + BOUNDARY)<EOL>body.append('<STR_LIT>'<EOL>'<STR_LIT>'.format(form_field, filename)<EOL>.encode('<STR_LIT:utf-8>'))<EOL>body.append('<STR_LIT>'.format(file_type).encode('<STR_LIT:utf-8>'))<EOL>body.append(b'<STR_LIT>')<EOL>body.append(fp.read())<EOL>body.append(b'<STR_LIT>' + BOUNDARY + b'<STR_LIT>')<EOL>body.append(b'<STR_LIT>')<EOL>fp.close()<EOL>body = b'<STR_LIT:\r\n>'.join(body)<EOL>headers = {<EOL>'<STR_LIT:Content-Type>': '<STR_LIT>',<EOL>'<STR_LIT>': str(len(body))<EOL>}<EOL>return headers, body<EOL>
Pack image from file into multipart-formdata post body