-1

First of all, I am at a complete beginner level of python, with just under a month of experience.

I am writing a simple program as part of a project whereby my two primary objectives are create a function from scratch that flips and rotates an image, and another function that essentially changes the rgb value of the image (e.g. make the image greyscale). The user will be given an choice where they choose one of the effects to apply to the image. I already have pillow installed, do I need any other libraries? I want to know how I can go about creating these from scratch.

Any help would be appreciated

Thanks

EDIT: To clarify, I will be using pillow, but I will be creating the rotate and greyscale functions myself

  • "I want to know how I can go about creating these from scratch."—why? This is what libraries are for: to provide well-tested, reusable functionality, taking advantage of the community rather than just your own efforts. You may be under-estimating how much code is in Pillow. (Feel free to read the source code. This will help you understand what the library is doing, as well as its size, and may give you an idea of how you could go about creating a new library if you still want to.) – ChrisGPT was on strike Dec 03 '18 at 20:16
  • I'm assuming you don't want to use pillow at all, not even to simply read and write individual pixel values. What image formats do you want your program to work on? bmps are fairly easy to manipulate manually, since they're more or less just a grid of bit values. More popular formats, such as jpeg, png, and gif, are considerably more difficult. Consider checking the wikipedia pages for each of these for a rundown of how their data is represented in memory. – Kevin Dec 03 '18 at 20:16
  • I'm assuming you're still importing your image with pillow, and want to just learn how to do rotations using your own functions for practice. However you need to provide more info, I think. Are you asking about how to do arbitrary rotations, or just how to rotate in 90-degree increments? Also, which part of rotation algorithm don't you understand how to implement in Python? Do you have any code from your attempts? Have you even looked up any rotation algorithms already? See, there's a lot more info that you really should include in your question, as-is it's pretty ambiguous. – Random Davis Dec 03 '18 at 20:18
  • I need to do it from scratch to demonstrate my understanding of Python as part of a college assignment. Taking a look at the source code will help me I guess but I like I said I am very inexperienced when it comes to programming - I don't even know how to do that. I just want rotation in 90 degree increments. For the record, I haven't attempted it yet, I was hoping I would find out how to start this by getting help here. – Mohammed Eita Dec 03 '18 at 20:24
  • It looks like me and Davis are operating on different assumptions here, and this leads to very different interpretations of your question. Can you clarify? Are you trying to write a program that does not use Pillow at all, or are you willing to use all of Pillow except for the specific methods that rotate images and change them to grayscale? – Kevin Dec 03 '18 at 20:27
  • Ah yeah I am using pillow, so yeah the latter of what you said is correct. – Mohammed Eita Dec 03 '18 at 20:33
  • Assistance in *starting* a project isn't something stackoverflow is especially good at - you might have better luck starting at any online programming discussion forum and then come back here when you have specific "why doesn't this work"-style questions. – lvc Dec 03 '18 at 20:47
  • In the future, you need to show what you have tried. And you need to explain where you are having problems. I solved this "issue" of not having a pillow library in a 20 second google search, from the criteria that you provided. – T.Woody Dec 03 '18 at 20:52

2 Answers2

0

Pillow provides access to the individual pixels of an image, which can help you achieve what you want. Of course, the library functions like rotate() are faster ways to do this, but you just want to explore and learn, which is half the fun of programming.

You can create a new image and then get the pixel at a particular coordinate.

im = Image.new('RGBA', (250, 250))
im.getpixel((0, 0))

getpixel() will return a tuple of the color information, containing (red, green, blue, alpha)

You can also loop through an image and 'put' a new pixel using the same tuple of color values.

for x in range(200):
  for y in range(30):
    im.putpixel((x, y), (250, 0, 250))

You can save your image when you are done.

im.save('myImage.png')

Rotation in 90 degree increments is very simple, you can just swap the x and y values in the pixels.

for x in range(200):
  for y in range(200):
    p = sourceImage.getpixel(x,y) # copy a pixel
    targetImage.getpixel(y,x,p)   # put it in the new image, rotated 90 deg

Your next visit will be to look up computer graphics techniques.

Bert
  • 2,134
  • 19
  • 19
0

You will need to analyze the image as a matrix, and then swap the columns and rows. This requires understanding of linear algebra for optimizations. And if you try to brute force it, you will be waiting about 30 minutes to rotate each image (been there, done that).

Here is a look at inplace rotating. The gist of the program is:

# Python3 program to rotate a matrix by 90 degrees
N = 4

# An Inplace function to rotate
# N x N matrix by 90 degrees in
# anti-clockwise direction
def rotateMatrix(mat):
  # Consider all squares one by one
  for x in range(0, int(N/2)):
     # Consider elements in group
     # of 4 in current square
     for y in range(x, N-x-1):
        # store current cell in temp variable
        temp = mat[x][y]

        # move values from right to top
        mat[x][y] = mat[y][N-1-x]

        # move values from bottom to right
        mat[y][N-1-x] = mat[N-1-x][N-1-y]

        # move values from left to bottom
        mat[N-1-x][N-1-y] = mat[N-1-y][x]

        # assign temp to left
        mat[N-1-y][x] = temp
# Function to pr the matrix
def displayMatrix( mat ):
  for i in range(0, N):
     for j in range(0, N):
        print (mat[i][j], end = ' ')
     print ("")

mat = [[0 for x in range(N)] for y in range(N)]  # Driver Code
# Test case 1
mat = [ [1, 2, 3, 4 ],
     [5, 6, 7, 8 ],
     [9, 10, 11, 12 ],
     [13, 14, 15, 16 ] ]
''' 
# Test case 2
mat = [ [1, 2, 3 ],
     [4, 5, 6 ],
     [7, 8, 9 ] ]

# Test case 3
mat = [ [1, 2 ],
     [4, 5 ] ]
'''

rotateMatrix(mat)
displayMatrix(mat)  # Print rotated matrix
# This code is contributed by saloni1297
T.Woody
  • 1,142
  • 2
  • 11
  • 25