def __call__(self, resolution, image_format='JPEG', name=None): filename = name or 'default.{}'.format(image_format.lower()) color = 'blue' image = Image.new('RGB', resolution, color) image_data = BytesIO() image.save(image_data, image_format) image_content = base.ContentFile(image_data.getvalue()) return images.ImageFile(image_content.file, filename)
def test_multiple_calls(self): """ Multiple calls of get_image_dimensions() should return the same size. """ img_path = os.path.join(os.path.dirname(__file__), "test.png") with open(img_path, 'rb') as fh: image = images.ImageFile(fh) image_pil = Image.open(fh) size_1 = images.get_image_dimensions(image) size_2 = images.get_image_dimensions(image) self.assertEqual(image_pil.size, size_1) self.assertEqual(size_1, size_2)
def register(request): if request.method == "GET": if request.user.is_authenticated: return HttpResponseRedirect(reverse("index")) return render(request, "register.html", {"register": True}) # This is needed to prepopulate the form with users data if there's any error message = {} message["username"] = request.POST["username"] message["first_name"] = request.POST["first_name"] message["last_name"] = request.POST["last_name"] message["email"] = request.POST["email"] #check the image meet the required spec picture = request.FILES["picture"] img = images.ImageFile(picture) if img.height != 100 and img.width != 100: message["msg"] = "only 100 by 100 px image accepted" return render(request, "register.html", message) try: #this will throw an error if not found User.objects.get(email=request.POST["email"]) # A matching email is found message[ "msg"] = f"{request.POST['email' ]} is associated with another account" return render(request, "register.html", message) except: pass try: user = User.objects.create_user( username=request.POST["username"].strip(), password=request.POST["password"]) user.email = request.POST["email"] user.first_name = request.POST["first_name"] user.last_name = request.POST["last_name"] profile = Profile.objects.create(picture=request.FILES["picture"]) user.profile = profile user.save() return render(request, "register.html", { "msg": "registration success please login", "success": True }) except IntegrityError: message["msg"] = "username already taken" return render(request, "register.html", message)
def handle(self, *args, **options): try: debug = bool(conf.settings.DEBUG) except KeyError: debug = False if not debug: raise base.CommandError( 'Error! settings.DEBUG is not defined or false. \ This command only works with a development install' ) sys.exit(1) fake = faker.Faker() path = conf.settings.BASE_DIR + 'django_artisan/management/images/' try: path = conf.settings.MANAGEMENT_IMAGE_PATH except: pass try: imagefiles = ([ f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) ]) except Exception as e: raise base.CommandError( "Error! can't get image names. {}".format(e)) if not len(imagefiles): raise base.CommandError('Error! There are no images!') sys.exit(1) user_ids = [] for i in range(options['num_of_users']): self.stdout.write( self.style.SUCCESS('Creating user number {}'.format(i + 1))) first_name = fake.unique.first_name() last_name = fake.unique.last_name() new_user = auth.get_user_model().objects.create( username=first_name + str(uuid.uuid4()), first_name=first_name, last_name=last_name, is_active=True) new_user.profile.display_name = first_name + '-' + last_name new_user.save() user_ids.append(new_user.id) for n in range(conf.settings.MAX_USER_IMAGES): pic = imagefiles[randrange(8)] try: image = new_user.profile.forum_images.create( file=images.ImageFile(file=open(path + pic, 'rb')), text=str(path) + str(pic), title=pic[:30], active=True) img = Image.open(image.file.path) #img = ImageOps.fit(img, (1024,768)) img = ImageOps.expand(img, border=10, fill='white') img.save(image.file.path) get_thumbnail(image.file, "1024x768", format="WEBP", crop='center', quality=70) get_thumbnail(image.file, "500x700", format="WEBP", crop='center', quality=70) except Exception as e: raise base.CommandError( 'Error! creating image for user {} failed! {}'.format( i, e)) break self.stdout.write( self.style.SUCCESS('Successfully created {} users.'.format(i + 1))) self.stdout.write(self.style.SUCCESS('User ids: {}'.format(user_ids)))