public static void ResizeImageToWithinAnalysisLimitsTest(string filename)
        {
            Bitmap sourceImage = Program.LoadImage(Program.LoadImageType.Test, filename);

            Assert.False(ImageTransforms.IsWithinAnalysisLimits(sourceImage));

            Bitmap resizedImage = ImageTransforms.ResizeToWithinAnalysisLimits(sourceImage);

            Assert.True(ImageTransforms.IsWithinAnalysisLimits(resizedImage));
        }
        public static void ComposeOntoBackgroundAcceptanceTest(string fileName)
        {
            Bitmap sourceImage = Program.LoadImage(Program.LoadImageType.Test, fileName);

            Bitmap actualImage = ImageTransforms.ComposeImageOntoPhoto(sourceImage);

            Program.SaveTestImage(actualImage, "actual/" + fileName);

            Bitmap expectedImage = Program.LoadImage(Program.LoadImageType.Test, "expected/" + fileName);

            TestUtils.AssertImagesAreTheSame(expectedImage, actualImage);
        }
Example #3
0
        public static Bitmap ComposeImage(Bitmap sourceImage, string descriptionText, float confidence, bool isOld, bool isBlackAndWhitePhoto, float expensiveMultiplier, bool isPainting, bool isSign, int?extractedYear, string extractedLocale, Random random)
        {
            Bitmap drawnBitmap = null;

            if (isPainting || isSign)
            {
                drawnBitmap = ImageTransforms.ComposeImageOntoPhoto(sourceImage);
            }
            else
            {
                drawnBitmap = sourceImage.Clone();
            }

            PriceRange priceRange = GetPriceRange(descriptionText, confidence, expensiveMultiplier, random);
            int        year       = GetYear(drawnBitmap, isOld, isBlackAndWhitePhoto, extractedYear);

            string localePhrase = (extractedLocale != null ? (", " + extractedLocale) : "");

            string fullCaption = descriptionText + String.Format(" (ca. {0}{1})\n ${2}-${3}", year, localePhrase, PriceRange.FormatPrice(priceRange.lowPrice), PriceRange.FormatPrice(priceRange.highPrice));

            Bitmap footerImage = LoadImage(LoadImageType.Source, "footer.png");

            float scale         = (float)drawnBitmap.Width / (float)footerImage.Width;
            float footerHeight  = scale * footerImage.Height;
            float footerOriginY = drawnBitmap.Height - footerHeight;

            float textOriginY = footerOriginY + 10.0f * scale;
            float textOriginX = 200.0f * scale;

            int fontSize = (int)(33 * scale);

            FontFamily family = GetCaptionFontFamily();

            Font font = new Font(family, fontSize, FontStyle.Bold);

            TextGraphicsOptions textGraphicsOptions = new TextGraphicsOptions();

            textGraphicsOptions.TextOptions.WrapTextWidth = drawnBitmap.Width - textOriginX - 10;

            AffineTransformBuilder footerTransformBuilder = new AffineTransformBuilder()
                                                            .AppendScale(new SizeF(scale, scale));

            footerImage.Mutate(x => x.Transform(footerTransformBuilder));

            drawnBitmap.Mutate(x => x.DrawImage(footerImage, new SixLabors.ImageSharp.Point(0, (int)footerOriginY), new GraphicsOptions())
                               .DrawText(textGraphicsOptions, fullCaption, font, NamedColors.White, new PointF(textOriginX + 1, textOriginY + 1)));

            return(drawnBitmap);
        }
        public static void TransformImageAcceptanceTest()
        {
            Bitmap sourceImage = Program.LoadImage(Program.LoadImageType.Test, "perspectiveTransformSource.jpg");

            Bitmap actualImage = sourceImage.Clone();

            ImageTransforms.PerspectiveTransform(
                sourceImage,
                actualImage,
                new SixLabors.ImageSharp.Point(10, 10),
                new SixLabors.ImageSharp.Point(250, 50),
                new SixLabors.ImageSharp.Point(30, 500),
                new SixLabors.ImageSharp.Point(400, 300));

            Program.SaveTestImage(actualImage, "actual/perspectiveTransform.jpg");

            Bitmap expectedImage = Program.LoadImage(Program.LoadImageType.Test, "expected/perspectiveTransform.jpg");

            TestUtils.AssertImagesAreTheSame(expectedImage, actualImage);
        }
Example #5
0
        static void CreateAppraisals(PostToTwitterMode postToTwitterMode, int numberOfAppraisals)
        {
            Console.WriteLine("Getting collection listing");

            Art art = new Art(new IArtSource[] { new HarvardArtMuseum(), new MetropolitanMuseumOfArt() });

            IEnumerable <Art.Object> responseObjects = art.GetRandomObjects(numberOfAppraisals);

            Console.WriteLine("Found " + responseObjects.Count() + " results");

            int objectCounter = 0;

            foreach (Art.Object responseObject in responseObjects)
            {
                Console.WriteLine("-----------------------------------------------------------------------");

                string imageUrl = responseObject.imageURL;
                Console.WriteLine("image url: " + imageUrl);

                string fullListingURL = responseObject.listingURL;
                Console.WriteLine("Listing page: " + fullListingURL);

                Bitmap originalImage = Web.DownloadImage(imageUrl);

                if (originalImage != null)
                {
                    Bitmap resizedImage = ImageTransforms.ResizeToWithinAnalysisLimits(originalImage);

                    if (Directory.Exists("images"))
                    {
                        string originalDestinationFilePath = @"images/sourceImage" + objectCounter + ".jpg";
                        originalImage.Save(originalDestinationFilePath);

                        string resizedDestinationFilePath = @"images/resizedImage" + objectCounter + ".jpg";
                        resizedImage.Save(resizedDestinationFilePath);
                    }

                    ComputerVisionService.AnalysisBlob analysisBlob = ComputerVisionService.GetAnalysisBlob(resizedImage);

                    string tagString = "";
                    foreach (string tag in analysisBlob.generalAnalysisResult.Description.Tags)
                    {
                        tagString += tag + ", ";
                    }
                    Console.WriteLine("Tags: " + tagString);

                    string accentColor = ColorTable.GetClosestColorName(analysisBlob.generalAnalysisResult.Color.AccentColor);

                    Console.WriteLine("Foreground: " + analysisBlob.generalAnalysisResult.Color.DominantColorForeground + " Background: " + analysisBlob.generalAnalysisResult.Color.DominantColorBackground + " Accent: " + accentColor);

                    if (analysisBlob.generalAnalysisResult.Categories != null)
                    {
                        string categoryString = "";
                        foreach (Category category in analysisBlob.generalAnalysisResult.Categories)
                        {
                            categoryString += category.Name + ", ";
                        }
                        Console.WriteLine("Categories: " + categoryString);
                    }

                    Appraisal appraisal     = CreateAppraisal(originalImage, analysisBlob);
                    string    tweetableText = GetTweetableText(appraisal.comment, responseObject.listingURL, responseObject.artSourceHashTag);

                    if (Directory.Exists("images"))
                    {
                        string destinationFilePath = @"images/finalImage" + objectCounter + ".jpg";
                        appraisal.image.Save(destinationFilePath);

                        using (StreamWriter file = File.CreateText(@"images/comment" + objectCounter + ".txt"))
                        {
                            file.WriteLine(tweetableText);
                        }
                    }

                    if (postToTwitterMode == PostToTwitterMode.Yes)
                    {
                        TweetAppraisal(appraisal.image, tweetableText);
                    }
                }

                objectCounter++;
            }
        }