#๐ create new image from pixel coordinates of another image
579 messages ยท Page 1 of 1 (latest)
@real heath
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
There's a lot of missing information here. What kind of image? What image library are you using? What code have you tried so far?
like image
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308
but i want like the pixel
the look lot
from PIL import Image
img = Image.open('hey.png')
img_resized = img.resize((42, 42))
with open('hey.txt', 'r') as file:
for line in file:
coordinates = line.strip().replace(',', ' ').split()
if len(coordinates) != 2:
coordinates = line.strip().split('x')
x, y = map(int, coordinates)
img_resized.putpixel((x, y), (255, 0, 0))
# Save the resized image
img_resized.save('output.png')
print("New image saved as output.png")
here @bronze musk :>
So, you're tyring to save it as 666x666?
no
my image size 666x666
so i want the 21x21
so like
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308
long but i need the pixel
so only one pixe 335x501
just like that
but only one
@bronze musk did you unstertaled?
So, you're trying to get a 21x21 pixel out of a 666x666 image?
Is that a list of all the pixels you're trying to extract?
ya
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308,break
this one
whats the actual grid size of the qr?
is there a better way to work with qr other than resizing the image to a grid and reduce it to a bool array?
oh wait its not a qr code, my bad
i've not used pil, but opencv reads images are a numpy array
it should be easy to access particular pixels
you can send me it's ok?
import cv2
img = cv2.imread("<path to file>")
img should be the array of the image pixels
english not your first language?
little english
i speak arabic
cv2 ?
but why?
question why the cv2?
i just saved like that
yes cv2 is opencv's module name
read up
so like
but long
this save 21x21
i want the save image 21x21
no
you want to resize the image to 21x21?
resize and save?
english is not my first language too, but i manage
yup
cv2.resize should do it
import cv2
img = cv2.imread("original_image.jpg")
img_resized = cv2.resize(img, (10, 10))
cv2.imwrite("new_image.jpg", img_resized)
so txt?
original_image is the original image
its being reduced to 10x10
but there txt
and written as new image
what is the txt for though?
this pixel
this like
Ok, but I want to put pixel in txt, so now if the image is the same
bro bad the transalte
this different
@zenith linden
yo
hiii
you mind explaining what you're tryna achieve?
i want the pixel the size only one
so like
the one convert the image
long
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308,break
so i need 21x21 save image
@fathom burrow
i meant explain it in arabic and use google translator
can't hear you though

im good, are you trying to get a 1x1 pixel from an image?
yes!
but long like
size
but this 21
all the 21
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308,break
347x477,195x313,423x363,118x361,372x660,100x24,92x526,604x173,593x315,614x479,631x108,637x535,547x127,629x372,363x588,441x356,282x98,28x217,119x250,61x292,279x281,break
433x484,31x440,409x361,483x367,61x405,566x93,453x293,643x135,649x446,476x31,599x344,87x302,147x295,401x360,258x288,305x219,361x479,204x481,284x650,194x335,122x324,break
this txt
but need save image
the pixel
you want all pixels that are located at either x 21 or located at y21?
im having trouble understanding the problem, apologies
yup
yup
337x501 so like the pixel 1x1
im confused how that has anything to do with the number 21?
this doesnt contain the number 21?
maybe try and use a translator to tranlate what youre trying to say in english. It might make it easier to understand
or im missing something here 
exactly what i told ๐
so like
only one pixel
i want give pixel
so next
like that
so i want the save image 21x21

^^
sorry the bad transalate
@real heath bro tell what you want to tell in arabic, then use google translator to convert it to english
ูุฐุง ุฎุฑุจุงู ุชุฑู ุฌุฉ
Google's service, offered free of charge, instantly translates words, phrases, and web pages between English and over 100 other languages.
max
ok so first
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308,break
but the long
But it's a messed up translation, sorry, but I'm sorry. I'm trying this. The first thing is to have a pixel size, but like one dot of a pixel, but because I want a txt of that size.
the problem sadly
i nkow you confusion ๐ฆ
i hope you unstertaled
So you want to store your pixel size in a txt file?
or are you trying to create a txt file if a pixel is black or white?
ya
yup
just only one pixel the 21x21 the txt
i need saved image
did you unstertaled?
@zenith linden
maybe explain your problem to chat gpt and then ask it to translate the problem into english
: (
no, im saying explain your problem to chat gpt and then tell it to translate the problem youre having into english. Thall make it easier for us to understand and help you :)
i want the save image
you want to save every single pixel seperately?
maybe

i think
but maybe this
hold on
see?
so 35x25 just cut like pixel
like only one
and you want to save that?
and how will you decide what pixel to save?
so you want to crop the image to a chosen pixel?
Cropping an image means cutting out parts of it to make it smaller or to focus on a specific part of the picture. It's like using scissors to cut out the parts of a photo you don't want.
i need this like that cut out
yup that's txt
i need for save txt to png 21x21
what do you mean with txt
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308,break
?
337x501,120x469,520x603,467x15,447x655,312x161,517x396,403x421,617x335,300x562,419x221,129x78,31x566,22x526,149x124,200x47,146x638,464x227,75x494,399x92,365x308,break
347x477,195x313,423x363,118x361,372x660,100x24,92x526,604x173,593x315,614x479,631x108,637x535,547x127,629x372,363x588,441x356,282x98,28x217,119x250,61x292,279x281,break
433x484,31x440,409x361,483x367,61x405,566x93,453x293,643x135,649x446,476x31,599x344,87x302,147x295,401x360,258x288,305x219,361x479,204x481,284x650,194x335,122x324,break
81x569,535x144,91x303,9x13,585x464,307x181,567x52,281x480,176x224,24x493,63x197,124x324,279x658,514x232,330x129,27x384,253x115,558x353,613x279,80x542,434x487,break
309x377,215x234,546x458,663x456,579x637,355x599,255x511,524x508,169x20,88x346,523x193,365x337,180x35,599x603,293x293,167x176,94x271,220x429,508x456,26x380,73x216,break
662x272,292x563,649x550,291x165,246x572,607x522,585x614,455x423,476x573,347x562,306x419,286x115,173x482,612x598,426x240,359x376,88x564,29x88,344x7,587x367,424x470,break
237x165,461x526,87x197,410x167,554x245,205x257,553x151,296x38,622x84,609x293,194x339,448x472,346x231,131x404,357x232,320x555,597x622,451x391,421x283,418x315,318x169,break
141x303,361x227,224x355,424x151,157x395,146x4,526x525,619x115,135x360,196x448,618x337,569x330,480x571,617x294,262x28,353x96,446x270,449x467,401x414,453x8,169x1,break
this txt
okay so youre trying to select a pixel, and the dimensions of the selection from the top left to that pixel should be stored in a text file?
yup
that's
okay, rename the title of this thread to "I'm trying to select a pixel, and the dimensions of the selection from the top left to that pixel should be stored in a text file"
trying to select a pixel, and the dimensions of the selection from the top left to that pixel should
okay nevermind thats too long of a name lol, change it to Store dimensions of cropped image in text file
def save_pixel_coordinates(x, y, file_path):
"""
Saves the coordinates of a selected pixel to a text file.
:param x: The x coordinate of the selected pixel.
:param y: The y coordinate of the selected pixel.
:param file_path: The path to the text file where the coordinates will be saved.
"""
with open(file_path, 'w') as file:
file.write(f'{x},{y}')
# Example usage
x_coordinate = 50
y_coordinate = 50
file_path = 'your_path'
save_pixel_coordinates(x_coordinate, y_coordinate, file_path)
something like this?
you want to create a new QR code that translates the text to an image?
png and txt to png
the secret
?
idk the somethng
so you want to convert text like "Hello World" to an image?
no
;-;
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
same 
damn
im sorry, its the language barrier
this open "hey.png"
have you tried explaining your problem in your language to https://chat.openai.com ?
this image 666x666
i hope you unstertaled
@feral temple
img = Image.open('moredots.png')
with open('hey.txt', 'r') as file:
but this like
bro not work
@feral temple
@zenith linden
i think ?
right?
not that
bruh
i want the pixel txt to png
from PIL import Image
# Open the image
image = Image.open("hey.png")
# Load pixel coordinates from pixel.txt
with open("pixel.txt", "r") as f:
pixel_coordinates = f.read().strip().split(",")
# Define the size for the cropped image
crop_size = (21, 21)
# Loop through each pixel coordinate and crop the image
for coord in pixel_coordinates:
if coord == "break":
continue
x, y = map(int, coord.split("x"))
# Define a bounding box for cropping
left = x - crop_size[0] // 2
upper = y - crop_size[1] // 2
right = left + crop_size[0]
lower = upper + crop_size[1]
# Crop the image
cropped_image = image.crop((left, upper, right, lower))
# Save the cropped image with the pixel coordinate as the filename
cropped_image.save(f"cropped_{x}x{y}.png")
print("All cropped images saved successfully.")
i tired :<
Hmm, you want to split the image into 21x21 crop based on the coordinates in the pixel.txt file?

hm wdym?
Idk, I'm grasping at straws here
like cut
maybe
You get as many images as there are coordinates in the pixel.txt file?
Each image is 21x21 crop of the original image?
what error were you getting?
huh?
when you run this code?
what error were you getting?
bruh?
cropped_image.save(f"cropped_{x}x{y}.png")
but no need
just the pixel cut
1x1
from PIL import Image
# Open the image
image = Image.open("hey.png")
crop_size = (21, 21)
# Load pixel coordinates from pixel.txt
with open("pixel.txt", "r") as f:
for line in f:
for coord in line.strip().replace("break", "").split(","):
if coord:
print(coord)
x, y = map(int, coord.split("x"))
# Define a bounding box for cropping
left = x - crop_size[0] // 2
upper = y - crop_size[1] // 2
right = left + crop_size[0]
lower = upper + crop_size[1]
# Crop the image
cropped_image = image.copy().crop((left, upper, right, lower))
# Save the cropped image with the pixel coordinate as the filename
cropped_image.save(f"cropped_{x}x{y}.png")
print("All cropped images saved successfully.")
try this
hold on let me
this is dividing the original image into 21x21 crop based on the coord in the pixel.txt file
hope unstetaled
why is this relevant for anything i'm not getting at all
look the pixel only one
what is > crop_size = (21, 21)
here
what is this for anyway?
which?
no i mean what do you intend to do with this problem?
it's not problem
i need help
but how the this lik 21x21
sorry my bad it's notp crop :<
like this
ah i give up bro, i'm sorry, i tried but the language is the barrier ๐
SORRY :<
I'M REALLY SORRY
can call show screen but you unstertaled
its alright, talk with someone locally
Let me understand this and I now have a guess. Those are coordinates. You want to save the pixels at those coordinates?
ya
So, each pixel is a tuple containing coordinates?
Or you want the value at these coordinates?
no just like cut pixel
So, you want each pixels to be equal to the value found at those coordinates?
so all the white and block just each like 21x21
That is what cutting a pixel look like
Yes, I know they're 1x1 appended together
I can't do much with Python in this aspect, but I can be open to PM though for solving this in other language
Oh, I think I understand now... You have your image with all the pixels in it. The 666x666 image. You are making a new image, and that image will be 21x21. And each pixel in the new image are single pixels cut out from the main picture.
and that list there is what pixel from the main image will be in the new image.
maybe
i think so
can ?
I'll see what I can do, I'm currently working on a work project, but if anyone else can help you with this, I'm pretty sure that's what you're trying to do now, though.
Change topic name to create new image from pixel coordinates of another image.
create new image from pixel coordinates of another image
and anyone who jumps in, I can explain what you're trying to do for you.
are you sure ๐ ?
went to have dinner
Yeah, this image here #1211284437870968872 message That's how the new image is suppose to look. 21x21, and each pixel is a coord to another pixel from the 666x666 image. the break moves to the next line, which is why the last line doesn't have a break, it's the end of the image.
can you send me code?
yep i get it
@real heath do you have numpy installed?
import numpy as np
from PIL import Image
image = Image.open("hey.png")
res_image_array = []
with open("pixel.txt", "r") as f:
for line in f:
res_image_line = []
for coord in line.strip().replace("break", "").split(","):
if coord:
# print(coord)
x, y = map(int, coord.split("x"))
res_image_line.append(image.getpixel((x, y))[0])
res_image_array.append(res_image_line)
res_image = Image.fromarray(np.array(res_image_array).astype("uint8"))
res_image.save("res_image.png")
@bronze musk not too good with pil, this alright?
line 14, in <module>
res_image_line.append(image.getpixel((x, y))[0])
~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not subscriptable
@real heath Can you send the main image?
wdym?
the hey.png image
this is my result:
it becomes this when i run it
your image contains a lot of noises. i'm simply copying pixels from only the coordinates provided in pixel.txt. so, it's like cutting 1x1 and then painting it .
wtf this a qr
wow, that's not a result i expected.
๐ฎ
yes it is
but wrong....
and the qr code just makes a note called deeperdotpng
ya
wtf is happening
send me LOL
this
running just this
finally thank you โค๏ธ
res_image_line.append(image.getpixel((x, y))[0])
~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not subscriptable
bro?
res_image_line.append(image.getpixel((x, y))[0])
~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not subscriptable
but what is this
getpixel is returning a tuple of (r,g,b,a) for me, so just taking the first element
huh?
thats what i want to know too
it's not work ;-;
change this line to, maybe res_image_line.append(image.getpixel((x, y)))
It worked for me, so maybe a version difference?
wdym?
wtf is happening lol
precognition?
asttokens==2.4.1
comm==0.2.1
debugpy==1.8.1
decorator==5.1.1
duckdb==0.10.0
executing==2.0.1
ipykernel==6.29.2
ipython==8.22.1
jedi==0.19.1
jupyter_client==8.6.0
jupyter_core==5.7.1
matplotlib-inline==0.1.6
nest-asyncio==1.6.0
numpy==1.26.4
opencv-python==4.9.0.80
packaging==23.2
pandas==2.2.1
parso==0.8.3
pexpect==4.9.0
pillow==10.2.0
platformdirs==4.2.0
prompt-toolkit==3.0.43
psutil==5.9.8
ptyprocess==0.7.0
pure-eval==0.2.2
pyarrow==15.0.0
Pygments==2.17.2
python-dateutil==2.8.2
pytz==2024.1
pyzmq==25.1.2
six==1.16.0
stack-data==0.6.3
tornado==6.4
traitlets==5.14.1
tzdata==2024.1
wcwidth==0.2.13
this is what i had in the environment
asgiref==3.7.2
Django==5.0.2
numpy==1.26.4
pillow==10.2.0
sqlparse==0.4.4
this is all I had.
pip freeze
so how the update numpy?
pip instal numby --upgrade
You're not running the right code. That image isn't 21x21
Try saving the file, and re-running the code.
That file is 733x521
Show the code you're trying to run.
I got 21x20 too
21x20 isn't 21x21, so you're still running into a problem somewhere.
import numpy as np
from PIL import Image
image = Image.open("hey.png")
res_image_array = []
with open("pixel.txt", "r") as f:
for line in f:
res_image_line = []
for coord in line.strip().replace("break", "").split(","):
if coord:
# print(coord)
x, y = map(int, coord.split("x"))
res_image_line.append(image.getpixel((x, y)))
res_image_array.append(res_image_line)
res_image = Image.fromarray(np.array(res_image_array).astype("uint8"))
res_image.save("res_image.png")
This works
Yeah, now that you have numpy updated, try the original code again
res_image_line.append(image.getpixel((x, y))[0])
~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not subscriptable
why the weird?
res_image_line.append(image.getpixel((x, y))[0])
~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not subscriptable
init?
That's earlier, now it should work
hmm?
Running the original code gives the error?
res_image_line.append(image.getpixel((x, y))[0])
~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not subscriptable
bro
you can send me txt?
I used this
With this image
Could be your executing code in a different environment?
import os;os.system("pip freeze")
wha?
run this
copy paste this into a .py file and run it
huh?
annotated-types==0.6.0
asgiref==3.7.2
audioread==3.0.1
av==11.0.0
blis==0.7.11
catalogue==2.0.10
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
cloudpathlib==0.16.0
colorama==0.4.6
confection==0.1.4
contourpy==1.2.0
cycler==0.12.1
cymem==2.0.8
decorator==5.1.1
Django==5.0
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl#sha256=86cc141f63942d4b2c5fcee06630fd6f904788d2f0ab005cce45aadb8fb73889
ffmpeg==1.4
ffmpeg-python==0.2.0
fonttools==4.46.0
future==0.18.3
greenlet==3.0.3
idna==3.6
imageio==2.33.1
imageio-ffmpeg==0.4.9
iso8601==2.1.0
Jinja2==3.1.2
joblib==1.3.2
kiwisolver==1.4.5
langcodes==3.3.0
lazy_loader==0.3
librosa==0.10.1
llvmlite==0.42.0
MarkupSafe==2.1.3
matplotlib==3.8.3
mendeleev==0.15.0
MouseInfo==0.1.3
mpmath==1.3.0
msgpack==1.0.7
murmurhash==1.0.10
nltk==3.8.1
numba==0.59.0
numpy==1.26.4
opencv-python==4.8.1.78
packaging==23.2
pandas==2.1.4
pillow==10.2.0
platformdirs==4.2.0
pooch==1.8.0
preshed==3.0.9
psutil==5.9.7
PyAutoGUI==0.9.54
pycparser==2.21
pycryptodome==3.19.0
pydantic==2.5.3
pydantic_core==2.14.6
pydub==0.25.1
pyfiglet==0.8.post1
pygame==2.5.2
PyGetWindow==0.0.9
Pygments==2.17.2
PyMsgBox==1.0.9
pyparsing==3.1.1
pyperclip==1.8.2
PyRect==0.2.0
PyScreeze==0.1.30
python-dateutil==2.8.2
pytweening==1.0.7
pytz==2023.3.post1
PyYAML==6.0.1
regex==2023.10.3
requests==2.31.0
scikit-learn==1.4.0
scipy==1.12.0
serial==0.0.97
setuptools==69.0.2
six==1.16.0
smart-open==6.4.0
soundfile==0.12.1
soxr==0.3.7
spacy==3.7.2
spacy-legacy==3.0.12
spacy-loggers==1.0.5
SQLAlchemy==2.0.25
sqlparse==0.4.4
srsly==2.4.8
sympy==1.12
thinc==8.2.2
threadpoolctl==3.3.0
tqdm==4.66.1
typer==0.9.0
typing_extensions==4.9.0
tzdata==2023.3
urllib3==2.1.0
wasabi==1.1.2
weasel==0.3.4
Try closing the editor and reopening it. Sometimes that helps.
what are you even trying to do
import numpy as np
from PIL import Image
image = Image.open("hey.png")
res_image_array = []
with open("pixel.txt", "r") as f:
for line in f:
res_image_line = []
for coord in line.strip().replace("break", "").split(","):
if coord:
# print(coord)
x, y = map(int, coord.split("x"))
res_image_line.append(str(image.getpixel((x, y))[0]))
res_image_array.append(res_image_line)
res_image = Image.fromarray(np.array(res_image_array).astype("uint8"))
res_image.save("res_image.png")
dude try the original code
original code?
this one
where is the str coming from?
bro i did but not work
import numpy as np
from PIL import Image
image = Image.open("hey.png")
res_image_array = []
with open("pixel.txt", "r") as f:
for line in f:
res_image_line = []
for coord in line.strip().replace("break", "").split(","):
if coord:
# print(coord)
x, y = map(int, coord.split("x"))
res_image_line.append(image.getpixel((x, y))[0])
res_image_array.append(res_image_line)
res_image = Image.fromarray(np.array(res_image_array).astype("uint8"))
res_image.save("res_image.png")
!d PIL.Image
1 sec
what are you even trying to do? x2
Okay, so he has this image. #1211284437870968872 message He wants to create a new 21x21 image with these coords #1211284437870968872 message
I meant more about stringing that and getting the first index, but thanks, that helps
The code @zenith linden wrote works for him and myself, but not for him
that doesn't work for him either ๐
can you print the type of image.getpixel?
me?
@bronze musk
yeah, one second
its a tupel of (r,g,b,a) for me
could be when the image is sent in discord, it got changed somehow?
don't send images in discord
i don't see any other possibility
<class 'tuple'>
yup that's it
.
zip?
I got it, one second.
convert on image works for all types?
wdym
Image.convert("L") should give just the greyscale
I can't remember what the formats meant
this makes it a greyscale, so like just b/w images with an int for each pixel
getpixel becomes and int after that
import numpy as np
from PIL import Image
image = Image.open("hey.png").convert("L")
res_image_array = []
with open("pixel.txt", "r") as f:
for line in f:
res_image_line = []
for coord in line.strip().replace("break", "").split(","):
if coord:
x, y = map(int, coord.split("x"))
res_image_line.append(image.getpixel((x, y)))
res_image_array.append(res_image_line)
res_image = Image.fromarray(np.array(res_image_array).astype("uint8"))
res_image.save("res_image.png")
try this
make sure to open the res_image.png file after
alright, enjoy the rest of the day!!
Alright guys, I'm out for a bit, got some running around to do. 
cya @zenith linden and thank you for your help. even though we will never know what it's for, lol
thanks @bronze musk & @regal ivy
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
