def create_image_model(model: models.Model, filename: str, slug: str): image_file = ImageFile(open_file(filename)) image_model = Image.objects.create(model=model, slug=slug, image=image_file) image_file.close() return image_model
class CumulusTests(TestCase): def setUp(self): "Set up tiny files to test with." image_path = os.path.join(os.path.dirname(__file__), "image_300x200.gif") document_path = os.path.join(os.path.dirname(__file__), "text_file.txt") self.image = ImageFile(open(image_path, "rb")) self.document = File(open(document_path, "r")) def test_file_api(self): """ Make sure we can perform the following using the Django File API: - Upload the test files - Access common file attributes - Delete the test files """ self.thing = Thing.objects.create(image=self.image, document=self.document) self.assertEqual(self.thing.image.width, 300) self.assertEqual(self.thing.image.height, 200) self.assertEqual(self.thing.image.size, 976) self.assert_( "cdn.cloudfiles.rackspacecloud.com" in self.thing.image.url, "URL is not a valid Cloud Files CDN URL." ) self.assertEqual(self.thing.document.size, 31) self.assert_( "cdn.cloudfiles.rackspacecloud.com" in self.thing.document.url, "URL is not a valid Cloud Files CDN URL." ) self.thing.delete() def tearDown(self): self.document.close() self.image.close()
def create_image_model(model, path, slug): image_file = ImageFile(file=open('{}{}.jpg'.format(path, slug), mode='rb')) image_model = Image.objects.create(model=model, slug=slug, image=image_file) image_file.close() return image_model
def saveImage(tempImage,nameOfMedia): """saves the raw content of files""" fOrig = open(settings.MEDIA_DIR+nameOfMedia+".jpg",'w') imgTemp = ImageFile(fOrig) imgTemp.write(tempImage) imgTemp.flush() imgTemp.close()
def create_image_model(model: models.Model, filename: str, slug: str): image_file = ImageFile(open_file(filename)) image_model = Image.objects.create( model=model, slug=slug, image=image_file ) image_file.close() return image_model
def makeThumbnail(tempImage,nameOfMedia): """makes thumbnails of max size 200x1000 from raw content and save it""" #imgTemp = Image.open("/opt/sources/code/pinry/media/pins/pin/"+nameOfMedia+".jpg") #imgTemp.thumbnail(MAX_THUMBNAIL_SIZE , Image.ANTIALIAS ) #imgTemp.save("/opt/sources/code/pinry/media/pins/pin/"+nameOfMedia+".200x1000.jpg", "JPEG") fThumb = open(settings.MEDIA_DIR+nameOfMedia+".200x1000.jpg",'w') imgTemp = ImageFile(fThumb) imgTemp.write(tempImage) imgTemp.flush() imgTemp.close()
class TestAttachment(TestCase): def setUp(self): self.owner = User.objects.create(username='******') self.dataset = DataSet.objects.create(slug='data', owner=self.owner) self.place = Place.objects.create(dataset=self.dataset, geometry='POINT(0 0)') from django.core.files.images import ImageFile image_path = os.path.join(FIXTURE_PATH, 'Automated-Testing.jpg') self.image_file = ImageFile(open(image_path, mode='rb'), name='Automated-Testing.jpg') from django.core.files.base import ContentFile self.non_image_file = ContentFile('This is not an image.') def tearDown(self): User.objects.all().delete() # Cascades. self.image_file.close() def test_save_sets_image_dims_when_none_is_provided(self): a = Attachment(thing=self.place, file=self.image_file) self.assertIsNone(a.width) self.assertIsNone(a.height) a.save() self.assertIsNotNone(a.width) self.assertIsNotNone(a.height) def test_save_leaves_image_dims_when_already_set(self): IMAGE_DIM = 23049 a = Attachment(thing=self.place, file=self.image_file, width=IMAGE_DIM, height=IMAGE_DIM) a.save() self.assertEqual(a.width, IMAGE_DIM) self.assertEqual(a.height, IMAGE_DIM) def test_save_ignores_image_dims_when_not_an_image_file(self): a = Attachment(thing=self.place, file=self.non_image_file) a.save() self.assertIsNone(a.width) self.assertIsNone(a.height)
class Coffee(models.Model): name = models.CharField(max_length=128) description = models.CharField(max_length=1024) one_pound_price = models.DecimalField(max_digits=5,decimal_places=2) two_pound_price = models.DecimalField(max_digits=5,decimal_places=2) five_pound_price = models.DecimalField(max_digits=5,decimal_places=2) image = models.ImageField(upload_to='images') active = models.BooleanField(default=True) class BadSizeException(Exception): pass def __unicode__(self): return self.name def __str__(self): return self.__unicode__() def value(self, pounds, quantity): if pounds == 1: return quantity * self.one_pound_price elif pounds == 2: return quantity * self.two_pound_price elif pounds == 5: return quantity * self.five_pound_price else: raise Coffee.BadSizeException def set_image(self, image_data): iof = BytesIO(image_data) self.image = ImageFile(iof) self.image.name = os.path.join('images', '%s.png' % self.name) def image_data_url(self): self.image.open() b64 = b64encode(self.image.read()).decode('utf-8') self.image.close() return 'data:image/png;base64,%s' % b64
def create_overview_image( video_id, source, duration, nb_img, image_width, overviewimagefilename, overviewfilename ): msg = "\ncreate overview image file" for i in range(0, nb_img): stamp = "%s" % i if nb_img == 99: stamp += "%" else: stamp = time.strftime('%H:%M:%S', time.gmtime(i)) cmd_ffmpegthumbnailer = "ffmpegthumbnailer -t \"%(stamp)s\" \ -s \"%(image_width)s\" -i %(source)s -c png \ -o %(overviewimagefilename)s_strip%(num)s.png" % { "stamp": stamp, 'source': source, 'num': i, 'overviewimagefilename': overviewimagefilename, 'image_width': image_width } # subprocess.getoutput(cmd_ffmpegthumbnailer) subprocess.run( cmd_ffmpegthumbnailer, shell=True) cmd_montage = "montage -geometry +0+0 %(overviewimagefilename)s \ %(overviewimagefilename)s_strip%(num)s.png \ %(overviewimagefilename)s" % { 'overviewimagefilename': overviewimagefilename, 'num': i } # subprocess.getoutput(cmd_montage) subprocess.run( cmd_montage, shell=True) if os.path.isfile("%(overviewimagefilename)s_strip%(num)s.png" % { 'overviewimagefilename': overviewimagefilename, 'num': i }): os.remove("%(overviewimagefilename)s_strip%(num)s.png" % {'overviewimagefilename': overviewimagefilename, 'num': i}) if check_file(overviewimagefilename): msg += "\n- overviewimagefilename :\n%s" % overviewimagefilename # Overview VTT overview = ImageFile(open(overviewimagefilename, 'rb')) image_height = int(overview.height) overview.close() image_url = os.path.basename(overviewimagefilename) image = { 'image_width': image_width, 'image_height': image_height, 'image_url': image_url } msg += create_overview_vtt( video_id, nb_img, image, duration, overviewfilename) msg += save_overview_vtt(video_id, overviewfilename) # else: msg = "overviewimagefilename Wrong file or path : "\ + "\n%s" % overviewimagefilename add_encoding_log(video_id, msg) change_encoding_step(video_id, -1, msg) send_email(msg, video_id) return msg
def main(): base = "http://www.nolafront.org/pages/" req = urllib.urlopen('%sarchive.htm' % base) html = req.read() pq = PyQuery(html) ass = pq("a.style2") a_iter = ass.items() a = a_iter.next() count = 0 last_year = "2013" while count < len(ass) - 1: href = "%s%s" % ( base, a.attr('href') ) name = a.text() if len(name) > 100: name = name[:14] artists_info = a.parent().text() artists_info = artists_info[len(name)+1:] print href nq = PyQuery(urllib.urlopen(href).read()) imgs = nq.find('img') n_iter = imgs.items() img = n_iter.next() text = "" i = 0 news, created = NewsArticle.objects.get_or_create(name=name) news.save() while i < len(imgs) - 1: i += 1 img_orig = n_iter.next() src = "%s%s" % (base, img_orig.attr('src')) img_name = "%s-%d.jpg" % (name, i) if len(NewsMedia.objects.filter(news_article=news,name=img_name)) > 0: continue try: img = cStringIO.StringIO(urllib.urlopen(src).read()) img = Image.open(img) img_path = "%s/front_media/%s" % (settings.MEDIA_ROOT, img_name) portrait = float(img.size[0] / img.size[1]) < 1.0 img.save(img_path, "JPEG", quality=90, optimize=True) img = ImageFile(open(img_path)) media, created = NewsMedia.objects.get_or_create(news_article=news, full_res_image=img, portrait=portrait, name=img_name) img_orig.attr('src', "%s%s" % (settings.MEDIA_URL, media.full_res_image.name)) img.close() except: print "error on saving: %s" % src html = nq.find("body").html() if href.find("archive-december 13") >= 0: print html is_content = False lines = html.split("\n") news.text = "" for line in lines: if line.find("LINKS") >= 0: is_content = True continue if is_content: if line.find("</body>") >= 0: is_content = False continue news.text += line news.name = name news.is_old_news = True news.artists_info = artists_info news.name = name date = name.split('-') date = date[0] if date.endswith(" "): date = date.rstrip(" ") try: date.index(',') except: try: date = "%s, %s" % (date, name.split(", ")[1]) except: pass try: date = datetime.strptime(date, "%B %d, %Y") last_year = "%d" % date.year except: print 'didnt get a date 1' print date try: date = "%s, %s" % (date, last_year) date = datetime.strptime(date, "%B %d, %Y") last_year = "%d" % date.year except: print 'didnt get a date 2' date = datetime.strptime(last_year, "%Y") news.date = date news.save() count += 1 a = a_iter.next() #href = archive[0]['href'] #name = archive[0]['name'] #text = archive[0]['text'] #print text '''
def save(self, *args, **kwargs): if not self.pdf: # output path output_path = os.path.join(settings.MEDIA_ROOT, 'output') # empty the output directory for entity in os.listdir(output_path): file_path = os.path.join(output_path, entity) if os.path.isfile(file_path) and not '.empty' in file_path: # don't remove .empty files os.remove(file_path) # initial save super(Edition, self).save(*args, **kwargs) # open zip, extract and close zip = ZipFile(self.output.path) zip.extractall(output_path) zip.close() # take pdf file pdf_file = File(open(os.path.join(output_path, 'file.pdf'), 'rb')) # save pdf file and edition object self.pdf.save( self.date.strftime("%Y-%m-%d.pdf"), pdf_file, save=False ) super(Edition, self).save(*args, **kwargs) pdf_file.close() # remove pdf file from output path os.remove(pdf_file.name) i = 1 for entry in os.listdir(output_path): if entry.endswith('.txt'): # create new Page object page = Page() page.edition = self page.number = i # get txt file with page text contents txt_file = open(os.path.join(output_path, entry)) page.text = txt_file.read() txt_file.close() # attach screenshot of pdf page img_file = ImageFile( open( os.path.join( output_path, 'scr-%d.jpg' % (i - 1) ), 'rb' ) ) page.screenshot.save( self.date.strftime("%Y-%m-%d.jpg")[:-4] + '-%d.jpg' % i, img_file ) img_file.close() # take pdf file pdf_page_file = File(open(os.path.join(output_path, 'file-%d.pdf' % (i - 1)), 'rb')) # save pdf file and edition object page.pdf.save( self.date.strftime("%Y-%m-%d.pdf")[:-4] + '-%d.pdf' % i, pdf_page_file ) pdf_page_file.close() # increase counter i = i + 1 # empty the output directory for entity in os.listdir(output_path): file_path = os.path.join(output_path, entity) print file_path if os.path.isfile(file_path) and not '.empty' in file_path: # don't remove .empty files os.remove(file_path) else: # super(Edition, self).save(*args, **kwargs) pass
def create_overview_image( video_id, source, duration, nb_img, image_width, overviewimagefilename, overviewfilename, ): """Create image overview for video navigation.""" msg = "\ncreate overview image file" cmd_ffmpegthumbnailer = "" cmd_montage = "" for i in range(0, nb_img): stamp = "%s" % i if nb_img == 99: stamp += "%" else: stamp = time.strftime("%H:%M:%S", time.gmtime(i)) cmd_ffmpegthumbnailer = 'ffmpegthumbnailer -t "%(stamp)s" \ -s "%(image_width)s" -i %(source)s -c png \ -o %(overviewimagefilename)s_strip%(num)s.png' % { "stamp": stamp, "source": source, "num": i, "overviewimagefilename": overviewimagefilename, "image_width": image_width, } # subprocess.getoutput(cmd_ffmpegthumbnailer) subprocess.run(cmd_ffmpegthumbnailer, shell=True) cmd_montage = ("montage -geometry +0+0 %(overviewimagefilename)s \ %(overviewimagefilename)s_strip%(num)s.png \ %(overviewimagefilename)s" % { "overviewimagefilename": overviewimagefilename, "num": i }) # subprocess.getoutput(cmd_montage) subprocess.run(cmd_montage, shell=True) if os.path.isfile("%(overviewimagefilename)s_strip%(num)s.png" % { "overviewimagefilename": overviewimagefilename, "num": i }): os.remove("%(overviewimagefilename)s_strip%(num)s.png" % { "overviewimagefilename": overviewimagefilename, "num": i }) if check_file(overviewimagefilename): msg += "\n- overviewimagefilename:\n%s" % overviewimagefilename # Overview VTT overview = ImageFile(open(overviewimagefilename, "rb")) image_height = int(overview.height) overview.close() image_url = os.path.basename(overviewimagefilename) image = { "image_width": image_width, "image_height": image_height, "image_url": image_url, } msg += create_overview_vtt(video_id, nb_img, image, duration, overviewfilename) msg += save_overview_vtt(video_id, overviewfilename) # else: msg = "overviewimagefilename Wrong file or path:" + "\n%s" % overviewimagefilename msg += "\nthumbnailer command: \n- %s\n" % cmd_ffmpegthumbnailer msg += "\nmontage command: \n- %s\n" % cmd_montage msg += "\nduration %s - nb_img %s - image_width %s \n" % ( duration, nb_img, image_width, ) add_encoding_log(video_id, msg) change_encoding_step(video_id, -1, msg) send_email(msg, video_id) return msg